matplotlib study diary (X) - shared axis of the drawing area

(1) sharing a single axis of the drawing area

'''
Last lecture describes the division of the canvas, sometimes you want to various figures in the same drawing area,
I do not want to draw a pattern only once per drawing area, by which time shared axis method is implemented in a drawing area
The purpose of the several figures of the drawing.
'''
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl


mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False

fig, ax1 = plt.subplots()
t = np.arange(0.05, 10, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, c="b", ls="-")

ax1.set_xlabel ( " X axis " )

ax1.set_ylabel ( " base e exponential function " , Color = " R & lt " )
ax1.tick_params ( " y " , Color = " B " )
 # y-axis labels, and tick labels primary lines disposed 
ax2 = ax1.twinx ()
 # Example Example ax2 spindle x axis ax1 are shared, examples ax2 of tick and tick labels ridge plotted on the right axis 
S2 = np.cos (T ** 2 )
ax2.plot(t, s2, c="r", ls=":")

ax2.set_ylabel ( " cosine " , Color = " R & lt " )
ax2.tick_params("y", colors="r")

plt.show()

(2) sharing the axis of the drawing area different sub-regions

'''
The method of drawing area shared by different sub-regions of the coordinate axes is subplots (2, 2, sharey = True),
sharey = True is a region shared y-axis, there are other parameters, row, col, all, none II,
And none, respectively, wherein all equivalents True and False.
'''
import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace (0, 2 * np.pi, 400 )
y1 = np.cos(x1)

x2 = np.linspace(0.01, 10, 100)
y2 = np.cos(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)
#分成4个子区
ax1 = ax[0, 0]
ax1.plot(x1, y1)
#ax[0, 0]访问第一个子区
ax2 = ax[0, 1]
ax2.plot(x2, y2)

ax3 = ax[1, 0]
ax3.scatter(x3, y3)

ax4 = ax[1, 1]
ax4.plot(x4, y4)

plt.show()

(3)将(2)中的plt.subplots(2, 2)改成plt.subplots(2, 2, sharex="all")-所有子区共享x轴

(4)参数sharex=“none”

与(2)相同

(5)参数sharex=“row”------->每一行x轴取值范围实现共享

(6)sharex="col"------------->每列共享x

(7)将共享坐标轴的子区之间的空隙去掉,似乎出了点问题,还是有空隙

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.0, 10.0, 200)
y = np.cos(x)*np.sin(x)
y2 = np.exp(-x)*np.sin(x)
y3 = 3*np.sin(x)
y4 = np.power(x, 0.5)

fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex="all")

fig.subplots_adjust(hspace=0)

ax1.plot(x, y, ls="-", lw=2)
ax1.set_yticks(np.arange(-0.6, 0.7, 0.2))
ax1.set_ylim(-0.7, 0.7)

 (8)共享个别子区绘图区域的坐标轴

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)
ax2.plot(x2, y2)

ax3 = plt.subplot(223)
ax3.plot(x3, y3)

ax4 = plt.subplot(224, sharex=ax1)
#与子区1共享x轴
ax4.plot(x4, y4)

plt.show()

 

Guess you like

Origin www.cnblogs.com/ai-bingjie/p/11094483.html