Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
(1) Import library
import matplotlib.pyplot as plt
import numpy
(2) Simple use of figure objects and subplot
#figureObject
fig = plt.figure() #figure is an image object
ax1 = fig.add_subplot(2,2,1) #Create a 2*2 subplot and put it in the first position
ax2 = fig.add_subplot(2,2,2) #Create a 2*2 subplot and place it in the second position
ax3 = fig.add_subplot(2,2,3) #Create a 2*2 subplot and place it in the third position
from numpy.random import randn
plt.plot(randn(50).cumsum(),'k--') #'k--' tells python to draw a black dotted line
ax1.hist(randn(100),bins=20,color='k',alpha=0.3)
Insert picture description here
(3) Adjust the spacing around the subplot
#Adjust the spacing around the subplot
fig, axes = plt.subplots(2,2,sharex=True,sharey=True) #Directly generate fiure objects and Axes instances (arrays)
for i in range(2):
for j in range(2):
axes[i, j ].hist(numpy.random.randn(500),bins = 50,color='k',alpha=0.5) #Traverse to create a graph
plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0,hspace=0) #Used to adjust the spacing around the subplot
Insert picture description here
(4) Color, marking and line style
#Colors, marks and line styles
plt.figure()
##linestyleSet line style, color set color, marker set connection point
plt.plot(numpy.random.randn(30).cumsum(),linestyle='--',color='g',marker='o')
Insert picture description here
(5) Set the title, axis label, scale and scale label
#Set title, axis label, scale and scale label
fig = plt.figure() #Create figure object
ax = fig.add_subplot(1,1,1) #Get Axes object
ax.plot(numpy.random.randn(1000).cumsum()) #Generate random numbers
ax.set_xticks([0,250,500,750,1000]) #Set the x-axis scale
ax.set_yticks([-20,-10,0,10,20]) #Set y-axis scale
ax.set_title('My first matplotlib plot') #Set title
ax.set_xlabel('Xtages') #Set the x-axis label
ax.set_ylabel('Ytages') #Set y-axis label
Insert picture description here
(6) Add legend
#Add legend
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label label is used to add a legend
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='two')
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='three')
ax.legend(loc='best') #loc option can choose the position of the legend
Insert picture description here
(7) Add notes
#add notes
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label label is used to add a legend
plt.annotate("Important value", (55,20), xycoords='data', #Method for adding annotations
xytext=(5, 38),
arrowprops=dict(arrowstyle='->'))
Insert picture description here
(8) Draw commonly used graphics
#Draw common graphics
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) #rectangle
circ = plt.Circle((0.7,0.2),0.15,color='b',alpha=0.3) #ellipse
ax.add_patch(rect) #Add to the board
ax.add_patch(circ)
Insert picture description here
(9) Export of charts
#Graphic save
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) #rectangle
ax.add_patch(rect) #Add to the board
fig.savefig('figpath.png', dpi = 400, bbox_inches='tight') #dpi can control the resolution of the image, bbox_inc