1, a single shared axis of the drawing area
. 1 Import matplotlib 2 Import matplotlib.pyplot AS PLT . 3 Import numpy AS NP . 4 . 5 # Display Chinese identification . 6 matplotlib.rcParams [ " font.sans serif- " ] = [ " SimHei " ] . 7 matplotlib.rcParams [ " axes.unicode_minus " ] = False . 8 . 9 # returns a canvas object instance fig and a coordinate axis ax. 10 Fig, AX1 = plt.subplots () . 11 12 is # argument parameter 13 is T = np.arange (0.05,10.0,0.01) 14 15 S1 = np.exp (T) # exponential function 16 ax1.plot (T, S1, C = " B " , LS = " - " ) # plotted ax1 exponential function among the color blue . 17 ax1. set_xlabel ( " X axis " ) # set the X-axis label 18 is ax1.set_ylabel ( " base e exponential function " , Color = " B " ) # set the Y-axis labels . 19 ax1.tick_params (axis = " Y " ,color="B " ) # set the Y axis scale of blue 20 is 21 is # Create a shared with the X axis ax1 axis Examples ax2, but do not share Y-axis 22 is AX2 of = ax1.twinx () 23 is 24 S2 = np.cos ( 2 ** T) # cosine function 25 ax2.plot (T, S2, C = " R & lt " , LS = " : " ) # draw exponential function ax2 among red color 26 is ax2.set_ylabel ( " cosine " , = Color " R & lt " ) # set the Y-axis labels, because the X-axis has shared 27ax2.tick_params (Axis = " Y " , Color = " R & lt " ) # set the Y axis scale of red 28 29 plt.show ()
2, the drawing area shared axis different subareas
1 import matplotlib.pyplot as plt 2 import numpy e.g. as 3 4 x1 = np.linspace (0.2 * np.pi, 400 ) 5 y1 = np.cos (x1 ** 2 ) 6 7 x2 = np.linspace ( 0.01,10,100 ) 8 y2 = np.sin (x2) 9 10 x3 = np.random.rand (100 ) 11 y3 = np.linspace (0,3,100 ) 12 13 x4 = np.arange (0,6,0.5 ) 14 y4 = np.power (x4,3 ) 15 16 FIG. plt.subplots x = (2,2 ) 17 18 AX1 = ax[0,0] 19 ax1.plot(x1,y1) 20 21 ax2 = ax[0,1] 22 ax2.plot(x2,y2) 23 24 ax3 = ax[1,0] 25 ax3.scatter(x3,y3) 26 27 ax4 = ax[1,1] 28 ax4.scatter(x4,y4) 29 30 plt.show()
(1) 当fig,ax = plt.subplots(2,2,sharex="all")时。
If sharex = "all", 4 figures abscissa have adopted the same coordinate ranges; if sharey = "all", the ordinate in FIG. 4 have adopted the same range coordinates
(2) When the fig, ax = equivalent to fig when plt.subplots (2,2, sharex = "none"), ax = plt.subplots (2,2) itself, does not share any coordinates.
(3) When the fig, ax = plt.subplots (2,2, sharex = "row"), each row of the sub-partitions share abscissa.
(4) When the fig, ax = plt.subplots (2,2, sharex = "col"), the sub partition each column sharing the abscissa.
3, the gap between the sub-regions shared axis removed
. 1 Import matplotlib.pyplot AS PLT 2 Import numpy AS NP . 3 . 4 X = np.linspace (0.0,10.0,200 ) . 5 Y1 = np.cos (X) * np.sin (X) . 6 Y2 = np.exp (- X) * np.sin (X) . 7 Y3 =. 3 * np.sin (X) . 8 Y4 = np.power (X, 0.5 ) . 9 10 # achieve shared by X-axis coordinate = sharex "All" . 11 Fig, (AX1 , AX2 of, AX3 of, AX4) plt.subplots = (4,1, sharex = " All " ) 12 is 13 is # a void region 4 of FIG horizontal removed 14 fig.subplots_adjust (hspace = 0) 15 16 ax1.plot(x,y1,ls="-",lw=2,c="b") 17 ax1.set_yticks(np.arange(-0.6,0.7,0.2)) 18 ax1.set_ylim(-0.7,0.7) 19 20 ax2.plot(x,y2,ls="-",lw=2,c="r") 21 ax2.set_yticks(np.arange(-0.05,0.36,0.1)) 22 ax2.set_ylim(-0.1,0.4) 23 24 ax3.plot(x,y3,ls="-",lw=2,c="g") 25 ax3.set_yticks(np.arange(-3,4,1)) 26 ax3.set_ylim(-3.5,3.5) 27 28 ax4.plot(x,y4,ls="-",lw=2,c="c") 29 ax4.set_yticks(np.arange(0.0,3.6,0.5)) 30 ax4.set_ylim(0.0,4.0) 31 32 plt.show()
4, the shared axis of the drawing area of individual sub-regions
import matplotlib.pyplot as plt import numpy as np x1 = np.linspace (0.2 * np.pi, 400 ) y1 = np.cos (x1 ** 2 ) x2 = np.linspace (0.01,10,100 ) y2 = np.sin (x2) x3 = np.random.rand(100) y3 = np.linspace (0,3,100 ) x4 = np.arange(0,6,0.5) y4 = np.power(x4,3) fig,ax = plt.subplots(2,2) Ax1 = plt.subplot (221 ) ax1.plot(x1,y1) ax2 = plt.subplot(222) ax1.plot(x2,y2) AX3 = plt.subplot (223 ) ax3.scatter(x3,y3) AX4 = plt.subplot (224, sharex = AX1) # shared with the abscissa of the first sub-region ax4.scatter (x4, y4) plt.show()