Summary of Matplotlib Basic Methods

Preface

Tip: This article is suitable for quick review:


Tip: The following is the main content of this article, and the following examples are for reference.

---------Basic Format

Create Canvas

. figure.

Create allocation drawing area

. add_Axes ([,]).

Load the three attributes of x, y, and line separately

. plot().

How to use multiple images in a region

l1 = ax.plot(x1,y1,'rd--')
l2 = ax.plot(x2,y2,'rd--')
l3 = ax.plot(x3,y3,'rd--')
ax.legend(labels = ('Smartphone', 'tv','test'), loc = 'lower right')

Set_Title set_Xlabel set_Ylabel sets annotations for the x-axis and y-axis of the head separately
. grid (color= 'b', ls= '-.', lw= 0.25) Set grid color linetype 2 attribute width ''
''.

--------Canvas area division method

1.

Axes.

2.

Canvas allocation can also be done using. subplot (nrows, ncols, index)
If you want to overlap the display, you can add_Subplot (111) add_Subplot (221, facecolor='y ')
Assign subgraphs when creating a canvas, a= Plt. subplots (2,2) a [0] [0]. plot (x, x * x).

3.

Plt. subplot2grid (shape, location, rowspan, colspan)
Shape: Use the grid area specified by the parameter value as the drawing area
Location: Draw the graph at the given position, with the initial position (0,0) representing the first row and first column
Rowspan/colspan: These two parameters are used to set the number of rows and columns that the subregion spans.

---------Coordinate Axis Settings

1. Scale

#. set_Yscale ("") Set coordinate y-axis scale type. set_Xlabel ("x axis") sets the type of x-axis scale.

2. Color

# ax.spines['bottom'].set_color('blue')set coordinate axis color 
# ax.spines['left'].set_color('red')
# ax.spines['left'].set_linewidth(2)

3. Value range

1. Default automatic value
2. # Set y-axis a1. set_Ylim (010000) # Set x-axis a1. set_Xlim (0,10). set_Zlim.

--------Scale Style

.set_xticks([0,2,4])
.set_xtickslabels(['zero','two','four',six])
.set_yticks([-1,0,1])

---------Using Chinese
The drawback of the method is that the same code needs to be added every time a drawing program is written
Import matplotlib. yplot as plt
Plt. rcParams ["font. sans serif"]= ["SimHei"] # Set Font
Plt. rcParams ["axes. unicode_minus"]= False # This statement solves the problem of garbled characters with negative signs in images
Method 2 1. View the directory where matplotlibrc is located, import matplotlib matplotlib. matplotlib_Fname()
2. # font. serif: DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century...
Remove #
3. Copy C: Windows Fonts Microsoft YaHei UI
Paste to D: python python37 lib site packages matplotlib mpl data fonts ttf
After pasting, an MSYH.ttc font file will appear.