catalogue
3.1 setting the style of graphics
4 adjust the scale of the coordinate axis
6.1 method 1: global setting (valid under Windows and Linux)
6.2 method 2: set Chinese font
7. Setting description information
8 draw multiple figures in one figure
8.1 adding legends for different graphics
Draw a line chart
1 import pyplot
from matplotlib import pyplot as plt
2. Set the drawing size
plt.figure(figsize=(20,8),dpi=80)
Figure represents the graphic icon, that is, the drawing. By instantiating a figure and passing parameters, the figure instance can be used automatically in the background.
figsize parameter: a tuple, used to set the size of the picture, indicating width and height;
dpi: indicates the number of pixels per inch. It is used to set the picture definition.
3 drawing
plt.plot(x,y) passes in X and y to draw a line chart
x: The position of the data on the x-axis is an iteratable object
y: The position of the data on the y-axis is an iterative object
3.1 setting the style of graphics
plt. Add parameters color (line color), linestyle (line style), linewidth (line thickness), alpha (transparency, 0-1), etc. to plot(), such as:
plt.plot(x,y,color="orange",linestyle=':',linewidth=5,alpha=0.5)
4 adjust the scale of the coordinate axis
Call PLT xticks()
1) Adjust spacing: pass in a parameter (iterative object containing numbers) with appropriate step size;
2) Add string: pass in two parameters, namely two iteratable objects (numeric data and string data). The length of the two groups of data must be the same, and the numeric value and string correspond one by one. Finally, only string data is displayed.
5. Display and save graphics
Call PLT show(),plt.savefig()
6 Display Chinese in graphics
6.1 method 1: global setting (valid under Windows and Linux)
import matplotlib matplotlib.rc("font",family='Microsoft YaHei',weight='bold',size=12)
6.2 method 2: set Chinese font
Find the file path of the specified font, and add the fontproperties parameter to receive the font where Chinese needs to be displayed (use the parameter prop to receive the font in the legend plt.legend())
from matplotlib import font_manager my_font = font_manager.FontProperties(fname="C:\WINDOWS\FONTS\MSYH.TTC") plt.xlabel("time", fontproperties=my_font)
7. Setting description information
plt.xlabel("time", fontproperties=my_font)
plt.ylabel("temperature (℃)", fontproperties=my_font)
plt. Title ("time change per minute from 10:00 to 12:00", fontproperties=my_font)
import random from matplotlib import pyplot as plt import matplotlib from matplotlib import font_manager #Display Chinese # font = {'family' : 'Microsoft YaHei', # 'weight' : 'bold', # 'size' : 12} # # matplotlib.rc("font",**font) # matplotlib.rc("font",family='Microsoft YaHei',weight='bold',size=12) my_font = font_manager.FontProperties(fname="C:\WINDOWS\FONTS\MSYH.TTC") x = range(0,120) y = [random.randint(20,35) for i in range(120)] #Set picture size plt.figure(figsize=(20,8),dpi=80) #Draw a line chart plt.plot(x,y) #Set the coordinate axis scale to correspond the numeric data to the string data one by one x_ticks = ["10 spot{}branch".format(i) for i in range(60)] x_ticks += ["11 spot{}branch".format(i) for i in range(60)] plt.xticks(list(x)[::5],x_ticks[::5],rotation=45,fontproperties=my_font) #Add description information plt.xlabel("time",fontproperties=my_font) plt.ylabel("temperature(℃)",fontproperties=my_font) plt.title("10 Time change per minute from 0:00 to 12:00",fontproperties=my_font) #Save the picture in svg vector image format plt.savefig("./t1.svg") #display graphics plt.show()
8 draw multiple figures in one figure
Call PLT multiple times Just plot()
8.1 adding legends for different figures
1) In PLT Set the parameter label in plot(), such as: plot(label = "1");
2) Call PLT Legend (prop, loc): specify the font of the legend through the {prop parameter; Set the location of the legend through the loc parameter, which defaults to the upper right corner.
9 draw grid
plt.grid(), such as PLT grid(alpha=0.4,linestyle=":")
from matplotlib import pyplot as plt import matplotlib from matplotlib import font_manager my_font = font_manager.FontProperties(fname="C:\WINDOWS\FONTS\MSYH.TTC") x = range(11,31) y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1] y_2 = [1,0,3,1,2,2,3,3,2,1 ,2,1,1,1,1,1,1,1,1,1] plt.figure(figsize=(20,8),dpi=80) #Call PLT multiple times Plot (), draw multiple figures and add legends for different figures plt.plot(x,y_1,lable="Number one",color="orange",linestyle=':') plt.plot(x,y_2,lable="Number two",color="cyan",linestyle='--') xtick_lables = ["{}year".format(i) for i in x] plt.xticks(x,xtick_lables,fontproperties=my_font) #Draw mesh plt.grid(alpha=0.3) #Set the font and position of the legend plt.legend(prop=my_font,loc=1) plt.show()