【问题标题】:Multiple overlapping plots with independent scaling in MatplotlibMatplotlib 中具有独立缩放的多个重叠图
【发布时间】:2010-10-13 10:39:27
【问题描述】:

我目前有代码多次调用matplotlib.pylab.plot 以在同一屏幕上显示多组数据,Matplotlib 将每个数据都缩放到全局最小值和最大值,考虑到所有绘图。有没有办法让它独立地将每个图缩放到该特定图的最小值和最大值?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    没有对此的直接支持,但这里有一些来自mailing list posting 的代码,它说明了两个独立的垂直轴:

    x=arange(10)
    y1=sin(x)
    y2=10*cos(x)
    
    rect=[0.1,0.1,0.8,0.8]
    a1=axes(rect)
    a1.yaxis.tick_left()
    plot(x,y1)
    ylabel('axis 1')
    xlabel('x')
    
    a2=axes(rect,frameon=False)
    a2.yaxis.tick_right()
    plot(x,y2)
    a2.yaxis.set_label_position('right')
    ylabel('axis 2')
    a2.set_xticks([])
    

    【讨论】:

    • 原始问题的范围似乎是针对 N 而不是 2 个缩放图 - 当我尝试使用 3 个或更多时它会死掉(使用 a2、a3 等用于附加轴实例。)知道如何一次正确缩放一大堆数据集吗?
    【解决方案2】:

    这是创建单个图 (add_subplot(1,1,1)) 并限制 y 轴上的比例的方法。

    myFig = figure()
    myPlot = self.figure.add_subplot(1,1,1)
    myPlot.plot([1,2,3,4,5], [5,4,3,2,1], '+r')
    myPlot.set_ylim(1,5) # Limit y-axes min 1, max 5
    

    【讨论】:

      【解决方案3】:

      我需要这样的东西,但想创建一个示例,您可以将其复制并粘贴到交互式 shell 中并查看它。这里适合那些需要有效解决方案的人:

      from numpy import arange
      from math import sin, cos
      import matplotlib.pyplot as plt
      
      x = arange(10)
      y1 = [sin(i) for i in x]
      y2 = [10*cos(i) for i in x]
      
      rect = [0.1, 0.1, 0.8, 0.8]
      a1 = plt.axes(rect)  # Create subplot, rect = [left, bottom, width, height] in normalized (0, 1) units
      a1.yaxis.tick_left()  # Use ticks only on left side of plot
      plt.plot(x, y1)
      plt.ylabel('axis 1')
      plt.xlabel('x')
      
      a2 = plt.axes(rect, frameon=False)  # frameon, if False, suppress drawing the figure frame
      a2.yaxis.tick_right()
      plt.plot(x, y2)
      a2.yaxis.set_label_position('right')
      plt.ylabel('axis 2')
      a2.set_xticks([])
      
      plt.show()
      

      在 python 2.7.6、numpy 1.8.1、matpotlib 1.3.1 中测试并工作。我将继续使用它,寻找一种巧妙的方式来处理重叠日期图。我会发回我的发现。

      【讨论】:

        【解决方案4】:

        这是一个使用日期图的解决方案,我认为它是使用 twinx() 的最优化解决方案,它是添加第二个 y 轴的简写。

        import matplotlib.pyplot as plt
        import matplotlib.dates as md
        import datetime
        import numpy
        numpy.random.seed(0)
        t = md.drange(datetime.datetime(2012, 11, 1),
                    datetime.datetime(2014, 4, 01),
                    datetime.timedelta(hours=1))  # takes start, end, delta
        x1 = numpy.cumsum(numpy.random.random(len(t)) - 0.5) * 40000
        x2 = numpy.cumsum(numpy.random.random(len(t)) - 0.5) * 0.002
        fig = plt.figure()
        ax1 = fig.add_subplot(111)
        fig.suptitle('a title', fontsize=14)
        fig.autofmt_xdate()
        plt.ylabel('axis 1')
        plt.xlabel('dates')
        ax2 = ax1.twinx()
        ax1.plot_date(t, x1, 'b-', alpha=.65)
        ax2.plot_date(t, x2, 'r-', alpha=.65)
        plt.ylabel('axis 2')
        plt.show()
        

        来自文档,matplotlib.pyplot.twinx(ax=None) 制作共享 x​​ 轴的第二个轴。新轴将覆盖 ax(如果 ax 为 None,则覆盖当前轴)。 ax2 的刻度将放置在右侧,并返回 ax2 实例。更多here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-22
          • 1970-01-01
          • 2021-11-27
          • 2013-10-05
          • 2021-10-23
          相关资源
          最近更新 更多