绘制几个Y轴

时间:2013-10-07 11:44:57

标签: python matplotlib

我想用不同的比例绘制几个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是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')

0 个答案:

没有答案