在python中实现多坐标轴的绘制
实现的效果如下图所示:
其中注意点:
-
ax.set_frame_on(True) #frame代表边框
-
ax.patch.set_visible(False) #patch代表填充
-
for sp in ax.spines.values(): #spines代表坐标轴
sp.set_visible(False) #spines.values代表坐标轴上的值
下面是代码:
import matplotlib.pyplot as plt
def make_patch_spines_invisible(ax):
ax.set_frame_on(True) #frame代表边框
ax.patch.set_visible(False) #patch代表填充
for sp in ax.spines.values(): #spines代表坐标轴
sp.set_visible(False) #spines.values代表坐标轴上的值
fig, host = plt.subplots()
fig.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
# Offset the right spine of par2. The ticks and label have already been
# placed on the right by twinx above.
par2.spines["right"].set_position(("axes", 1.2))
# Having been created by twinx, par2 has its frame off, so the line of its
# detached spine is invisible. First, activate the frame but make the patch
# and spines invisible.
#激活边框,让par1与par2重合,但是取消par2的填充(否则会出现覆盖),并且使par2坐标轴不可见
make_patch_spines_invisible(par2)
# Second, show the right spine.
#使新建的par2的坐标轴显示
par2.spines["right"].set_visible(True)
p1, = host.plot([0, 1, 2], [0, 1, 2], "b-", label="Density"