我想绘制几个不同比例的 Y 轴,这是我基于在网上找到的示例的解决方案,但它只创建了一个额外的 Y 轴,即使我的数据中还有更多。
fig, ax = plt.subplots()
plt.subplots_adjust(right=0.75)
x, y, c, m, l = self.mData[0]
ax.plot(x, y, color=c)
ax.set_ylabel(l, color=c)
for tl in ax.get_yticklabels():
tl.set_color(c)
ax.set_xlabel('time')
for i in range(1, len(self.mData)):
ax_other = ax.twinx()
if i == 1:
offset = 0
else:
offset = 60 * (i - 1)
ax_other.set_frame_on(True)
ax_other.patch.set_visible(False)
ax_other.yaxis.set_label_position('right')
ax_other.yaxis.set_ticks_position('right')
ax_other.spines['right'].set_position(('outward', offset))
x, y, c, m, l = self.mData[i]
ax_other.plot(x, y, color=c)
ax_other.set_ylabel(l, color=c)
for tl in ax_other.get_yticklabels():
tl.set_color(c)
plt.gcf().autofmt_xdate()
在代码中,我绘制了第一组数据,然后如果还有其他要绘制的数据集,我为每个数据集创建一个新轴。X 轴显示日期,这对于我可以拥有的每组数据都是相同的。这段代码的问题是它只显示我拥有的额外轴的 1 个。
mData 是 namedtuples 的列表:
data_tuple = namedtuple('Data', ['x', 'y', 'color', 'marker', 'label'])
其中 x 是日期时间对象列表,y 是浮点数列表,颜色、标记和标签是字符串。
Data(x=[datetime.datetime(2011, 1, 1, 0, 0, 25, 135000), datetime.datetime(2011, 2, 1, 0, 0, 57, 386000), datetime.datetime(2011, 3, 1, 0, 0, 59, 579000), datetime.datetime(2011, 4, 1, 0, 0, 27, 676000), datetime.datetime(2011, 5, 1, 0, 0, 25, 135000), datetime.datetime(2011, 6, 1, 0, 0, 26, 414000), datetime.datetime(2011, 7, 1, 0, 0, 28, 145000), datetime.datetime(2011, 8, 1, 0, 0, 26, 432000), datetime.datetime(2011, 9, 1, 0, 0, 27, 301000), datetime.datetime(2011, 10, 1, 0, 0, 27, 643000), datetime.datetime(2011, 11, 1, 0, 0, 27, 673000), datetime.datetime(2011, 12, 1, 0, 0, 28, 294000)], y=[-10.246537396121893, 1.3039215686274515, 1.007462686567164, -0.12771084337349403, -0.1932367149758454, -0.3196125907990315, -0.04128440366972498, -0.19950738916256117, -1.6319018404907975, -1.1532258064516134, -1.666666666666668, -2.3958333333333326], color='Red', marker='x', label='Mean')
Data(x=[datetime.datetime(2011, 1, 1, 0, 0, 25, 135000), datetime.datetime(2011, 2, 1, 0, 0, 57, 386000), datetime.datetime(2011, 3, 1, 0, 0, 59, 579000), datetime.datetime(2011, 4, 1, 0, 0, 27, 676000), datetime.datetime(2011, 5, 1, 0, 0, 25, 135000), datetime.datetime(2011, 6, 1, 0, 0, 26, 414000), datetime.datetime(2011, 7, 1, 0, 0, 28, 145000), datetime.datetime(2011, 8, 1, 0, 0, 26, 432000), datetime.datetime(2011, 9, 1, 0, 0, 27, 301000), datetime.datetime(2011, 10, 1, 0, 0, 27, 643000), datetime.datetime(2011, 11, 1, 0, 0, 27, 673000), datetime.datetime(2011, 12, 1, 0, 0, 28, 294000)], y=[-8.0, 19.0, 11.0, 6.0, 6.0, 6.0, 6.0, 6.0, 2.0, 2.0, 2.0, 2.0], color='Red', marker='x', label='Max')
Data(x=[datetime.datetime(2011, 1, 1, 0, 0, 25, 135000), datetime.datetime(2011, 2, 1, 0, 0, 57, 386000), datetime.datetime(2011, 3, 1, 0, 0, 59, 579000), datetime.datetime(2011, 4, 1, 0, 0, 27, 676000), datetime.datetime(2011, 5, 1, 0, 0, 25, 135000), datetime.datetime(2011, 6, 1, 0, 0, 26, 414000), datetime.datetime(2011, 7, 1, 0, 0, 28, 145000), datetime.datetime(2011, 8, 1, 0, 0, 26, 432000), datetime.datetime(2011, 9, 1, 0, 0, 27, 301000), datetime.datetime(2011, 10, 1, 0, 0, 27, 643000), datetime.datetime(2011, 11, 1, 0, 0, 27, 673000), datetime.datetime(2011, 12, 1, 0, 0, 28, 294000)], y=[-12.0, -7.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -7.0, -7.0], color='Yellow', marker='o', label='Min')