Python 绘制多个Y轴

Python 绘制多个Y轴,python,matplotlib,Python,Matplotlib,我想用不同的比例绘制几个Y轴,这是基于我在网上找到的示例的解决方案,但它只创建了一个额外的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

我想用不同的比例绘制几个Y轴,这是基于我在网上找到的示例的解决方案,但它只创建了一个额外的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是一个命名的元组列表:

data_tuple = namedtuple('Data', ['x', 'y', 'color', 'marker', 'label'])
其中x是datetime对象的列表,y是float、color、marker和label是字符串的列表

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')

这是您正在尝试做的事情吗?不,这更像是,但在x轴中使用日期格式。好的,那么您是否尝试过使用
mpl\u工具包。axes\u grid1.host\u子图
,如该示例所示?我尝试过,但找不到一种方法来对x轴进行日期格式设置,如该示例中的autofmt\xduate。我所尝试的与该示例完全相同,除了我不知道如何在x轴上格式化日期。我尝试了几种方法,但都不管用,现在我不再使用上面发布的代码了。