听起来您想要的只是子图…您现在正在做的事情没有多大意义(或者我对您的代码片段感到非常困惑,无论如何……)。
尝试更多类似这样的方法:
import matplotlib.pyplot as pltimport numpy as npfig, axes = plt.subplots(nrows=3)colors = ('k', 'r', 'b')for ax, color in zip(axes, colors): data = np.random.random(1) * np.random.random(10) ax.plot(data, marker='o', linestyle='none', color=color)plt.show()
编辑:
如果您不想使用子图,则您的代码段会更有意义。
您正在尝试在彼此的正上方添加三个轴。Matplotlib正在认识到图中的大小和位置已经存在一个子图,因此每次都返回 相同的
轴对象。换句话说,如果您查看列表
ax,就会发现它们都是 同一对象 。
如果 确实 要这样做,则
fig._seen每次添加轴时都需要将其重置为空字典。但是,您可能真的不想这样做。
与其将三个独立的图放在彼此之上,不如看看使用方法
twinx。
例如
import matplotlib.pyplot as pltimport numpy as np# To make things reproducible...np.random.seed(1977)fig, ax = plt.subplots()# Twin the x-axis twice to make independent y-axes.axes = [ax, ax.twinx(), ax.twinx()]# Make some space on the right side for the extra y-axis.fig.subplots_adjust(right=0.75)# Move the last y-axis spine over to the right by 20% of the width of the axesaxes[-1].spines['right'].set_position(('axes', 1.2))# To make the border of the right-most axis visible, we need to turn the frame# on. This hides the other plots, however, so we need to turn its fill off.axes[-1].set_frame_on(True)axes[-1].patch.set_visible(False)# And finally we get to plot things...colors = ('Green', 'Red', 'Blue')for ax, color in zip(axes, colors): data = np.random.random(1) * np.random.random(10) ax.plot(data, marker='o', linestyle='none', color=color) ax.set_ylabel('%s Thing' % color, color=color) ax.tick_params(axis='y', colors=color)axes[0].set_xlabel('X-axis')plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)