【问题标题】:Python & Matplotlib: creating two subplots with different sizes [duplicate]Python和Matplotlib:创建两个不同大小的子图[重复]
【发布时间】:2014-01-16 11:47:01
【问题描述】:

我有一个脚本,它根据是否满足一个特定条件来创建一个或两个图表。基本上,到目前为止,我正在做的事情如下:

import matplotlib.pyplot as plt

list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
ax = plt.subplot(211) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
if somecondition == True:
   ax = plt.subplot(212) #create the second subplot, that MIGHT be there
   ax.plot(list2) #populate the second subplot
plt.show()

这段代码(使用正确的数据,但我所做的这个简单版本无论如何都是可执行的)生成两个相同大小的子图,一个在另一个之上。但是,我想得到的是以下内容:

  • 如果 somecondition 为 True,则两个子图都应出现在图中。因此,我希望第二个子图比第一个小 1/2;
  • 如果 somecondition 是 False,那么应该只出现第一个子图,我希望它的大小与所有图一样(不要留下空白,以防第二个子图不会出现)。

我很确定这只是调整两个子图大小的问题,甚至可能通过参数 211 和 212 (我不明白它们代表什么,因为我是 Python 新手,找不到网上有明确的解释)。有谁知道如何以简单的方式调节子图的大小,与子图的数量以及图形的整个大小成正比?为了更容易理解,您能否编辑我附加的简单代码以获得我正在寻找的结果?提前致谢!

【问题讨论】:

  • 好的,至于第二点(即,如果 somecondition 是 False 完全调整子图的大小)我已经做了以下解决方法:if somecondition = True: nsub = 111; else: nsub = 211; ax = plt.subplot(nsub) 但是,一个更优雅的解决方案将受到高度赞赏,并且主要问题(即第二个子图的 1/2 比例)​​尚未解决:/

标签: python matplotlib


【解决方案1】:

这个解决方案满足吗?

import matplotlib.pyplot as plt

list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart

if not somecondition:
    ax = plt.subplot(111) #create the first subplot that will ALWAYS be there
    ax.plot(list1) #populate the "main" subplot
else:
    ax = plt.subplot(211)
    ax.plot(list1)
    ax = plt.subplot(223) #create the second subplot, that MIGHT be there
    ax.plot(list2) #populate the second subplot
plt.show()

如果您需要宽度相同但高度只有一半,最好使用matplotlib.gridspecreference here

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart

gs = gridspec.GridSpec(3,1)

if not somecondition:
    ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there
    ax.plot(list1) #populate the "main" subplot
else:
    ax = plt.subplot(gs[:2, :])
    ax.plot(list1)
    ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there
    ax.plot(list2) #populate the second subplot
plt.show()

【讨论】:

  • 哇,几乎...而不是让第二个子图的宽度为一半,我怎么能得到它的宽度相同但高度为一半?我在网上找不到任何关于几何规则的信息,我正在玩弄这些数字,但只是弄得一团糟:)
【解决方案2】:

您似乎正在寻找这个:

if somecondition:
    ax = plt.subplot(3,1,(1,2))
    ax.plot(list1)
    ax = plt.subplot(3,1,3)
    ax.plot(list2)
else:
    plt.plot(list1)

幻数是 nrows、ncols、plot_number,参见the documentation。所以3,1,3 将创建 3 行 1 列,并将绘制到第三个单元格中。其缩写是313

可以将元组用作 plot_number,因此您可以创建一个位于第一个和第二个单元格中的图:3,1,(1,2)

【讨论】:

  • 这正是我想要的! “将创建 3 行,1 列并将绘制到第三个单元格”这句话是我找不到的(只是在玩耍时获得基本的直觉,但没有成功),我相信对于谁是新人来说非常重要到这个图书馆。我抓住了一个额外的小问题,如何解决尝试在子图中绘制元组时出现的错误TypeError: int() argument must be a string or a number, not 'tuple'
  • 您使用哪个版本?也许你有一个旧版本。您可以通过以下方式查看:print matplotlib.__version__
  • 这是 1.0.1,很可能是旧版本,因为我正在使用以前其他人正在使用的机器上工作,并且他们已经下载了一段时间。
  • 是的,这就是原因。在这种情况下,您可以使用@zhangxaochen 的解决方案,该解决方案同样适用于旧版本。
  • 是的,我做到了,但我非常感谢对几何解释的简单解释。我认为它更容易实现和理解,特别是当您有我的特定要求时(当然不是这次),我会要求升级版本;)谢谢!
猜你喜欢
  • 2015-10-18
  • 2021-08-05
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多