In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python how to use matplotlib to achieve drawing visualization, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
The usual introduction conventions for matplotlib are:
In [11]: import matplotlib.pyplot as plt
Run% matplotlib notebook in Jupyter (or% matplotlib in IPython) to create a simple graph. If everything is set up correctly, you will see figure 9-1:
In [12]: import numpy as npIn [13]: data = np.arange (10) In [14]: dataOut [14]: array ([0,1,2,3,4,5,6,7,8,9]) In [15]: plt.plot (data)
While libraries like seaborn and pandas's built-in drawing functions can handle many common drawing tasks, you must learn matplotlib API if you need to customize some advanced features.
❝
Notes: although this book does not discuss the various functions of matplotlib in detail, it is enough to introduce you to the door. Matplotlib's sample library and documentation are the best resources for learning advanced features.
❞
1.1 Figure and Subplot
The images of matplotlib are located in the Figure object. You can create a new Figure with plt.figure:
In [16]: fig = plt.figure ()
If you are using IPython, an empty window will pop up, but in Jupyter, you must enter more commands to see it. Plt.figure has some options, especially figsize, which is used to ensure that the picture has a certain size and aspect ratio when saved to disk.
You cannot draw through an empty Figure. You must create one or more subplot with add_subplot:
In [17]: ax1 = fig.add_subplot (2,2,1)
This code means that the image should be 2 × 2 (that is, a maximum of 4 images), and the first of the 4 subplot is currently selected (the number starts with 1). If you also create the last two subplot, the resulting image is shown in figure 9-2:
In [18]: ax2 = fig.add_subplot (2,2,2) In [19]: ax3 = fig.add_subplot (2,2,3)
❝
Tip: using Jupyter notebook is a little different, that is, after each window is re-executed, the drawing will be reset. Therefore, for complex graphics, you must store all the drawing commands in a small window.
❞
Here, we run all the commands in the same small window:
Fig = plt.figure () ax1 = fig.add_subplot (2,2,1) ax2 = fig.add_subplot (2,2,2) ax3 = fig.add_subplot (2,2,3)
If a drawing command is executed at this time (such as plt.plot ([1.5,3.5,- 2,1.6]), matplotlib will draw on the last used subplot (if not, create one), hiding the process of creating figure and subplot. Therefore, if we execute the following command, you will get the result shown in figure 9-3:
In [20]: plt.plot (np.random.randn (50). Cumsum (), 'Kmurmuri')
"Kmuri -" is a linetype option that tells matplotlib to draw a black dotted chart. The objects returned by fig.add_subplot above are AxesSubplot objects, and you can directly call their instance methods to draw pictures in other empty squares, as shown in figure 9-4:
In [21]: ax1.hist (np.random.randn, bins=20, color='k', alpha=0.3) In [22]: ax2.scatter (np.arange (30), np.arange (30) + 3 * np.random.randn (30))
You can find various chart types in the matplotlib documentation.
Creating a figure containing a subplot grid is a very common task, and matplotlib has a more convenient method, plt.subplots, which creates a new Figure and returns an array of NumPy containing the subplot objects that have been created:
In [24]: fig, axes = plt.subplots (2,3) In [25]: axesOut [25]: array ([[,], [,]], dtype=object)
This is very practical because the axes array can be easily indexed as if it were a two-dimensional array, such as axes [0L1]. You can also specify that subplot should have the same X or Y axis through sharex and sharey. This is also useful when comparing the same range of data, otherwise matplotlib will automatically scale the boundaries of each chart. For more information about this method, see Table 9-1.
1.2 adjust the spacing around the subplot
By default, matplotlib leaves a margin around the subplot and a gap between the subplot. The spacing is related to the height and width of the image, so if you resize the image (whether programmatically or manually), the spacing will be adjusted automatically. You can easily modify the spacing using Figure's subplots_adjust method, which is also a top-level function:
Subplots_adjust (left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
Wspace and hspace are used to control the percentage of width and height, and can be used as spacing between subplot. Here is a simple example where I narrowed the spacing to 0 (as shown in figure 9-5):
Fig, axes = plt.subplots (2,2, sharex=True, sharey=True) for i in range (2): for j in range (2): axes [I, j] .hist (np.random.randn, bins=50, color='k', alpha=0.5) plt.subplots_adjust (wspace=0, hspace=0)
It is not difficult to see that the axis labels overlap. Matplotlib does not check whether the tags overlap, so in this case, you can only set your own scale position and scale label. This will be covered in more detail in the following sections.
1.3 colors, markings and linetypes
Matplotlib's plot function accepts a set of X and Y coordinates, as well as a string abbreviation for color and linetype. For example, to draw green dotted lines based on x and y, you can execute the following code:
Ax.plot (x, y, 'gmurmuri')
This is a convenient way to specify colors and linetypes in a string. In practice, if you are drawing in code, you may not want to get the format you want by processing strings. The same effect can be achieved in a more explicit way:
Ax.plot (x, y, linestyle='--', color='g')
Commonly used colors can be abbreviated, or you can specify a color code (for example,'# CECECE'). You can view a collection of all linetypes by looking at plot's document string (using plot? in IPython and Jupyter).
Diagrams can use markers to emphasize data points. Because matplotlib can create continuous diagrams and interpolate between points, it may sometimes be difficult to see the location of real data points. Tags can also be placed in a format string, but the tag type and linetype must be placed after the color (see figure 9-6):
In [30]: from numpy.random import randnIn [31]: plt.plot (randn (30). Cumsum (), 'ko--')
It can also be written in a more explicit form:
Plot (randn 30). Cumsum (), color='k', linestyle='dashed', marker='o')
In a linetype graph, non-actual data points are interpolated linearly by default. You can modify it with the drawstyle option (see figure 9-7):
In [33]: data = np.random.randn (30). Cumsum () In [34]: plt.plot (data, 'Kmurmura, label='Default') Out [34]: [] In [35]: plt.plot (data,' KMurray, drawstyle='steps-post', label='steps-post') Out [35]: [] In [36]: plt.legend (loc='best')
You may have noticed that there is output when running the above code. Matplotlib returns an object that references the newly added child component. Most of the time, you can safely ignore these outputs. Here, because we passed the label parameter to plot, we can create a plot legend that indicates each line that uses plt.legend.
❝
Note: you must call plt.legend (or use ax.legend, if an axis is referenced) to create a legend, whether or not you pass the label tag option when you draw.
❞
1.4 ticks, labels and legends
For most chart decoration items, there are two main ways to implement them:
(1) use a procedural pyplot interface (for example, matplotlib.pyplot)
(2) more object-oriented native matplotlib API.
The pyplot interface is designed for interactive use and contains methods such as xlim, xticks, and xticklabels. They control the range of the chart, the position of the scale, the scale label, and so on. It is used in the following two ways:
When called without arguments, the current parameter value is returned (for example, plt.xlim () returns the current X-axis drawing range).
When called with parameters, the parameter value is set (for example, plt.xlim ([0jin10]) sets the range of the X axis to 0 to 10).
All of these methods work on the current or recently created AxesSubplot. Each of them corresponds to two methods on the subplot object. In the case of xlim, they are ax.get_xlim and ax.set_xlim. I prefer to use the instance method of subplot (because I like to be specific, and it's clearer when dealing with multiple subplot). Of course, you can choose the one that you find convenient.
1.5 set title, axis label, scale and scale label
To illustrate the custom axis, I will create a simple image and draw a random walk (as shown in figure 9-8):
In [37]: fig = plt.figure () In [38]: ax = fig.add_subplot (1,1,1) In [39]: ax.plot (np.random.randn (1000) .cumsum ())
The easiest way to change the x-axis scale is to use set_xticks and set_xticklabels. The former tells matplotlib where to place the scale in the data range, which is, by default, the scale label. But we can use any other value as a label through set_xticklabels:
In [40]: ticks = ax.set_xticks ([0250,500,750,1000]) In [41]: labels = ax.set_xticklabels (['one',' two', 'three',' four', 'five'],....: rotation=30, fontsize='small')
The rotation option sets the x scale label to tilt 30 degrees. Finally, set a name for the X axis with set_xlabel and a title with set_title (see the results in figure 9-9):
In [42]: ax.set_title ('My first matplotlib plot') Out [42]: In [43]: ax.set_xlabel ('Stages')
The Y axis is modified in a similar way, just replace x in the above code with y. The axis class has a collection method that allows you to set drawing options in batches. The previous example can also be written as:
Props = {'title':' My first matplotlib plot', 'xlabel':' Stages'} ax.set (* * props) 1.6 add legend
Legend (legend) is another important tool for identifying chart elements. There are several ways to add legends. The simplest thing is to pass in the label parameter when adding subplot:
In [44]: from numpy.random import randnIn [45]: fig = plt.figure () Ax = fig.add_subplot (1, 1, 1) In [46]: ax.plot (randn (1000). Cumsum (), 'kink, label='one') Out [46]: [] In [47]: ax.plot (randn (1000). Cumsum (),' Kantha Mustang, label='two') Out [47]: [] In [48]: ax.plot (randn (1000). Cumsum (), 'k.hammer, label='three') Out [48]: []
After that, you can call ax.legend () or plt.legend () to automatically create the legend (see figure 9-10 for the result):
In [49]: ax.legend (loc='best')
The legend method has several other options for loc position parameters. Please check the document string (using ax.legend?).
Loc tells matplotlib where to put the legend. If you are not nitpicking, "best" is a good choice, because it will choose the most inconvenient position. To remove one or more elements from the legend, do not pass in label or label='_nolegend_'.
1.7 Notes and drawing on Subplot
In addition to the standard drawing type, you may also want to draw some subset of annotations, which may be text, arrows, or other graphics. Comments and text can be added through the text, arrow, and annotate functions. Text can draw the text at the specified coordinates of the chart (XMagol y), and can also add some custom formatting:
Ax.text (x, y, 'Hello worldview, family='monospace', fontsize=10)
Comments can contain both text and arrows. For example, we draw a graph based on the most recent S & P 500 prices (from Yahooplastics Finance) and mark some important dates during the financial crisis from 2008 to 2009. You can experiment with this code in a small window in Jupyter notebook (figure 9-11 is the result):
From datetime import datetimefig = plt.figure () ax= fig.add_subplot (1,1,1) data = pd.read_csv ('examples/spx.csv', index_col=0, parse_dates=True) spx = data [' SPX'] spx.plot (ax=ax, style='k-') crisis_data = [(datetime (2007, 10, 11), 'Peak of bull market'), (datetime (2008, 3, 12),' Bear Stearns Fails'), (datetime (2008, 9, 15)) 'Lehman Bankruptcy')] for date, label in crisis_data: ax.annotate (label, xy= (date, spx.asof (date) + 75), xytext= (date, spx.asof (date) + 225), arrowprops=dict (facecolor='black', headwidth=4, width=2, headlength=4), horizontalalignment='left' Verticalalignment='top') # Zoom in on 2007-2010ax.set_xlim (['1 Important dates in the 2008-2009 financial crisis']) ax.set_ylim ([600, 2011])
There are several important points to emphasize in this diagram: the ax.annotate method can draw labels on specified x and y axes. Instead of using the default method of matplotlib, we use set_xlim and set_ylim to manually set the start and end boundaries. Finally, add the icon title with ax.set_title.
For more examples of annotations, visit matplotlib's online sample library.
The drawing of the figure is more troublesome. Matplotlib has some objects that represent common graphics. These objects are called patch. Some of these, such as Rectangle and Circle, can be found in matplotlib.pyplot, but the complete collection is located in matplotlib.patches.
To add a graph to the diagram, you need to create a block object, shp, and then add it to the subplot through ax.add_patch (shp) (see figure 9-12):
Fig = plt.figure () ax = fig.add_subplot (1,1,1) rect = plt.Rectangle ((0.2,0.75), 0.4,0.15, color='k', alpha=0.3) circ = plt.Circle ((0.7,0.2,0.15) color='b', alpha=0.3) pgon = plt.Polygon ([[0.15,0.15], [0.35,0.4], [0.2,0.6]] Color='g', alpha=0.5) ax.add_patch (rect) ax.add_patch (circ) ax.add_patch (pgon)
"if you look at the specific implementation code of many common chart objects, you will find that they are actually assembled from block patch. "
1.8 Save the chart to a file
Using plt.savefig, you can save the current chart to a file. This method is equivalent to the instance method savefig of the Figure object. For example, to save the chart as a SVG file, you simply type:
Plt.savefig ('figpath.svg')
The file type is inferred from the file extension. So, if you use .pdf, you will get a PDF file. The two most important options I use when publishing pictures are dpi (which controls the "dots per inch" resolution) and bbox_inches (which can cut out the white space around the current chart). To get a PNG image with the smallest white edge and a resolution of 400DPI, you can:
Plt.savefig ('figpath.png', dpi=400, bbox_inches='tight')
Savefig does not have to be written to disk, but can also be written to any file-type object, such as BytesIO:
From io import BytesIObuffer = BytesIO () plt.savefig (buffer) plot_data = buffer.getvalue () after reading the above, have you mastered how to use matplotlib to achieve drawing visualization in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.