【问题标题】:How do I get multiple scales for multiple subplots?如何获得多个子图的多个比例?
【发布时间】:2017-08-20 22:17:55
【问题描述】:

我知道要为单个图获得多个比例,您可以使用:

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(range(0,20,2))
ax2.plot(range(10))

但是,我需要为四个子图执行此操作。所以当我尝试这样的事情时:

fig, ax = plt.subplots(nrows=2, ncols=2)
for row in ax:
    for col in row:
        fig1, ax1 = col.subplots()
        ax2 = ax2.twinx()
        ax1.plot(range(0,20,2)
        ax2.plot(range(10))

我得到了错误

'AxesSubplot' object has no attribute 'subplots'

,这是有道理的,但我不知道如何解决它。

【问题讨论】:

    标签: python matplotlib plot subplot


    【解决方案1】:

    在问题的代码中,col 将是为其创建双轴的轴,即ax2 = col.twinx()

    fig, ax = plt.subplots(nrows=2, ncols=2)
    for row in ax:
        for col in row:
            ax2 = col.twinx()
            col.plot(range(0,20,2)
            ax2.plot(range(10))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多