matplotlib (d)

Previous: matplotlib (c)

Saves the chart to a file

You can use plt.savefig current chart saved to a file. This method corresponds to a method savefig Figure object instance. To save a chart as an SVG file
input

plt.savefig('figpath.svg') 

=====================================
file type is inferred by file extension. Using .pdf, you get a PDF file. The most commonly used two important options when publishing images is dpi (Control "dots per inch" resolution) and bbox_inches (blank portion may be cut off around the current chart). To get the most with a white side and a resolution of 400DPI PNG images

plt.savefig('figpath.png', dpi=400, bbox_inches='tight') 

=====================================
savefig not necessarily have to be written to disk, you can also write any file type object

from io import BytesIO 
buffer = BytesIO() 
plt.savefig(buffer) 
plot_data = buffer.getvalue() 

matplotlib Configuration

Understanding all of the customization options, please refer to matplotlib profile matplotlibrc located matplotlib / mpl-data directory if the file is a custom, and place your own .matplotlibrc directory, each use matplotlib this paper will load

=====================================
matplotlib comes with a number of color schemes, as well as to generate publication-quality pictures and set the default configuration information. Fortunately, almost all of the default behavior can be customized by a set of global parameters, they can manage the image size, subplot margins, the color scheme, font size, grid type.

=====================================
method of Python programming system is arranged to use the method rc .
The global default image size is set to 10 × 10, you can perform

plt.rc('figure', figsize=(10, 10)) 

=====================================
first argument of rc is to custom objects, The 'figure', 'axes', 'xtick', 'ytick', 'grid', 'legend' and the like. After that you can keep up with the number of keyword arguments. A simple approach is to write a dictionary of these options.

font_options = {'family' : 'monospace', 'weight' : 'bold', 'size' : 'small'} 
plt.rc('font', **font_options) 

and drawing pandas seaborn

matplotlib is actually a relatively low-level tool. To draw a chart, you assemble some of the basic components of the line: the data show, legends, titles, scale labels and other annotation type information.

=====================================
the pandas, we have multiple columns of data, as well as the line and column labels. pandas have their own built-in methods, to simplify the drawing and graphics from DataFrame Series. Another library Seaborn , static graphics library created by Michael Waskom. Seaborn simplifies many common types of visual creation.

=====================================
introduced seaborn matplotlib modify the default color scheme and a drawing type, to improve readability and aesthetics. Even if you do not use seaborn API, you may also be introduced seaborn, as to improve the aesthetics and simplified method of rendering common matplotlib graphics.

Line chart

Series and DataFrame have a method for generating a plot of various types of charts. By default, they generate is a line chart
Here Insert Picture Description
Here Insert Picture Description
of the index Series matplotlib object will be passed, and to draw the X-axis. Use_index = False can disable this feature. And the X-axis scale limit can be adjusted by xticks and xlim options, Y-axis and to use yticks ylim.

=====================================
Most pandas drawing method has an optional ax parameters, it can be a matplotlib the subplot object. This allows you more flexibility to deal with the position subplot in a grid layout.

=====================================
DataFrame the plot method will be drawn in each column in a subplot a line, and automatically creates a legend
Here Insert Picture Description
Here Insert Picture Description
Other key parameters plot will be passed to the appropriate matplotlib drawing functions, so more in-depth custom charts, we must learn more about matplotlib API's.

++++++++++++++++++++++++++++++++++++

Next: matplotlib (V)

Published 75 original articles · won 117 Like · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42893334/article/details/104097917