Canvas Configuration
plt.figure()
figsize:canvas size, width and height
#Import the two libraries numpy and matplotlib.pyplot import numpy as np import matplotlib.pyplot as plt #Created a graphics window with a size of 5x3 inches. plt.figure(figsize=(5, 3)) # Draw sine curve x = np.linspace(0, 2*np.pi) y = np.sin(x) plt.plot(x, y) plt.show()
If the canvas size is changed to 8×3 inches, the code changes and display effects are as follows:
plt.figure(figsize=(8, 3))
dpi: resolution, pixel density
If the canvas size is changed to 8×3 inches and the resolution is 200, the code changes and display effects are as follows
plt.figure(figsize=(8, 3),dpi=200)
facecolor: background color
If the canvas size is changed to 8×3 inches, the resolution is 200, and the red background is used, the code changes and display results are as follows
Draw multiple images on one canvas
#Import the two libraries numpy and matplotlib.pyplot import numpy as np import matplotlib.pyplot as plt #Create a graphics window with a size of 8x3 inches, a resolution of 200, and a background color of red. import matplotlib.pyplot as plt plt.figure(figsize=(8, 3),dpi=200,facecolor="r") x = np.linspace(0,8) #Draw a curve with x as the independent variable and sin(x) as the dependent variable. plt.plot(x, np.sin(x)) #Draw a curve with x as the independent variable and cos(x) as the dependent variable, and use the red line curve. plt.plot(x, np.cos(x),'r') #Draw a curve with x as the independent variable and -sin(x) as the dependent variable, and use the green dotted curve. plt.plot(x,-np.sin(x),'g--') plt.show()
If plt.show() is written in the middle, the code before plt.show() will draw a picture, and the code after plt.show() will draw a new picture.
#Import numpy and matplotlib.pyplot libraries import numpy as np import matplotlib.pyplot as plt #Create a graphics window with a size of 8x3 inches, a resolution of 200, and a background color of red. import matplotlib.pyplot as plt plt.figure(figsize=(8, 3),dpi=200,facecolor="r") x = np.linspace(0,8) #Draw a curve with x as the independent variable and sin(x) as the dependent variable. plt.plot(x, np.sin(x)) plt.show() #Draw a curve with x as the independent variable and cos(x) as the dependent variable, and use the red line curve. plt.plot(x, np.cos(x),'r') #Draw a curve with x as the independent variable and -sin(x) as the dependent variable, and use the green dotted curve. plt.plot(x,-np.sin(x),'g--') plt.show()
Multiple Picture Layout
Uniform distribution
subplot() function
#Import numpy and matplotlib.pyplot libraries import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rc("font", family='STXingkai') fig = plt.figure(figsize=(10,6)) x = np.linspace(-np.pi, np.pi, 30) y = np.sin(x) # Sub-picture 1 ax1 = plt.subplot(221)#1st picture in row 2 and column 2 ax1.plot(x,y) ax1.set_title('Subpicture 1') # Subpicture 2 ax2 = plt.subplot(222)#The second picture in row 2 and column 2 ax2.plot(x,y) ax2.set_title('Subpicture 2') # Sub-picture 3 ax3 = plt.subplot(212)#3rd picture in row 2 and column 2 ax3.plot(x,y) ax3.set_title('Subpicture 3') plt.show()
Graphic nesting
add_subplot() function
#Import numpy and matplotlib.pyplot libraries import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rc("font", family='STXingkai') fig = plt.figure(figsize=(8,5)) # Sub-picture 1 axes1 = fig.add_subplot(1,1,1) axes1.plot([0,1],[1,3]) # Subfigure 2: Nested figure axes2 = fig.add_subplot(2,2,1,facecolor='pink') axes2.plot([0,1],[1,3]) plt.show()
Use axes() function
Use add axes() function
#Import numpy and matplotlib.pyplot libraries import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rc("font", family='STXingkai') fig = plt.figure(figsize=(8,5)) #图一 x = np.linspace(0,2*np.pi,30) y = np.sin(x) plt.plot(x,y) #Nested Figure 1 #[left,bottom,width,height] axes1 = plt.axes([0.55,0.55,0.3,0.3]) axes1.plot(x,y,color='r') #Nested Figure 2 axes2 = fig.add_axes([0.18,0.18,0.3,0.3]) axes2.plot(x,y,color='g') plt.show()
Dual-axis display
# Import numpy and matplotlib.pyplot libraries import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rc("font", family='STXingkai') #Create a graphics window with a size of 6x4 plt.figure(figsize=(6,4)) # Generate 100 equally spaced values between 0 and 10 as x-axis data x = np.linspace(0,10,100) # Draw the first graph axes1 = plt.gca() # Get the current axis domain axes1.plot(x,np.exp(x),c='r') # Draw an exponential function curve, the color is red axes1.set_xlabel('time') # Set the x-axis label to "time" axes1.set_ylabel('exp',c='r') # Set the left y-axis label to "exp" and the color to red axes1.tick_params(axis='y',labelcolor='red') # Set the left y-axis scale label color to red # Draw the second graph axes2 = axes1.twinx() # Share the x-axis with the first graph axes2.plot(x,np.sin(x),c='b') # Draw a sine function curve, the color is blue axes2.set_ylabel('sin',c='b') # Set the right y-axis label to "sin" and the color to blue axes2.tick_params(axis='y',labelcolor='b') # Set the right y-axis scale label color to blue #Adjust the graphics layout so that the graphics do not overlap plt.tight_layout() # Display graphics plt.show()
This code draws two graphs. The first graph draws an exponential function curve (red) and sets the left y-axis label to “exp”. The second graph draws a sine function curve (blue) and sets the right The side y-axis label is “sin”. The two graphs share the x-axis and use different colors to distinguish the left and right y-axis tick labels. Finally, use plt.tight_layout() to adjust the graphics layout so that the graphics do not overlap, and display the graphics through plt.show()
The knowledge points of the article match the official knowledge archives, and you can further learn relevant knowledge. Python entry skill tree Drawing library MatplotlibMatplotlib quick introduction 388437 people are learning the system