Python extension library 2-matplotlib, pythonmatplotlib
1 load the matplotli drawing module and rename it to plt
import matplotlib.pyplot as plt
2 line chart
Import matplotlib. pyplot as pltimport numpy as npx = np. arange (9) y = np. sin (x) z = np. cos (x) # marker data point style, linewidth, linestyle, color plt. plot (x, y, marker = "*", linewidth = 3, linestyle = "--", color = "orange") plt. plot (x, z) plt. title ("y and z") plt. xlabel ("x") plt. ylabel ('height') # sets the legend plt. legend (["y", "z"], loc = "upper right") plt. grid (True) plt. show ()
3 scatter plot
x = np.random.rand(10)y = np.random.rand(10)plt.scatter(x,y)plt.show()
4 bar chart
x = np.arange(20)y = np.random.randint(0,30,20)plt.bar(x, y)plt.show()
5. Pie Chart
x = np.random.randint(1,10,4)plt.pie(x)plt.show()
6. Histogram
Mean, sigma = 0, 1x = mean + sigma * np. random. randn (1000) # randn generates a normal distribution plt. hist (x, 50) plt. show ()
7 subgraphs
Subplot (numRows, numCols, plotNum) # Row, column, region number
# Figsize width and height of the drawing object, in inches. dpi: the resolution of the drawing object, that is, the number of pixels per inch. The default value is 80 PLT. figure (figsize = (100), dpi =) # subplot (numRows, numCols, plotNum) # A Figure object can contain multiple subgraphs Axes, subplot divides the entire drawing area into sub-areas in the * numCols column of the numRows row, which are left to right, number each subarea in the order from top to bottom # subplot creates a subgraph AxesA = plt in the region specified by plotNum. subplot (2, 2, 1) plt. plot ([0, 1], [0, 1], color = "red") plt. subplot (2, 2) plt. title ("B") plt. plot ([0, 1], [0, 1], color = "green") plt. subplot (2, 1, 2) plt. title ("C") plt. plot (np. arange (10), np. random. rand (10), color = "orange") # select the subgraph Aplt. sca (A) plt. title ("A") plt. show ()
8. display Chinese characters in the graph
Matplotlib cannot display Chinese characters by default. You can modify the font directly in the program.
From matplotlib. font_manager import FontPropertiesimport matplotlib. pyplot as pltimport numpy as npfont = FontProperties (fname = r "c: \ windows \ fonts \ simsun. ttc ", size = 14) t = np. linspace (0, 10,100 0) y = np. sin (t) plt. plot (t, y) plt. xlabel (u "time", fontproperties = font) plt. ylabel (u "Amplitude", fontproperties = font) plt. title (u "Sine Wave", fontproperties = font) plt. show ()