【问题标题】:How to make subplots in python?如何在python中制作子图?
【发布时间】:2017-01-25 17:07:55
【问题描述】:

我正在尝试制作子图。 我从数据框中调用许多列,将它们转换为数组并绘制它们。 我想将它们绘制成 4 行 2 列。但我只得到 1 列(您可以查看图片)。我做错了什么?

这是我的代码:

  for column in df3:  #I call the dataframe
      data=df3[column].values  #Turn it into an array

      fig = plt.figure()
      plt.subplot (4,2,1) #I want 4 rows and 2 columns
      ax,_=plot_topomap(data, sensors_pos, cmap='viridis', vmin=0, vmax=100, show=False)
      plt.title("KNN" + " " + column) #This is the title for each subplot
      fig.colorbar(ax)
      plt.show 

【问题讨论】:

  • “但它不起作用”不是一个充分的问题描述。请阅读how to ask a question 并提供minimal reproducible example
  • 人们需要下载神经科学库才能使用 plot_topomap 和包含电极位置的文件。所以我不知道如何完成。
  • 您可以自上而下或自下而上构建minimal reproducible example。我不知道在这种情况下哪个更好,因为你没有告诉我们问题是什么。
  • 我想要一个 4 行 2 列的图。但我只得到 1 列所有的地块,它们已经变小了。
  • plot_topomap 命令你使用的是来自 mne 的 mne.viz.plot_topomap 吗?

标签: python-3.x pandas matplotlib subplot


【解决方案1】:

有几件事可能会导致您的代码出现问题,如果不知道完整的代码,很难找到解决方案。

在您的代码中,您创建了几个图形。但是,你真的想要一个单一的数字。所以需要在循环外创建图形。

然后你想创建子图,所以在每个循环步骤中你需要告诉 matplotlib 它应该绘制到哪个子图。这可以通过ax = fig.add_subplot(4,2,n) 完成,其中 n 是您在每次循环运行时增加的数字。

接下来你打电话给plot_topomap。但是plot_topomap 怎么知道在哪里绘制什么?您需要通过提供关键字参数axes = ax 来告诉它。

最后尝试设置一个颜色条,将返回图像作为坐标轴ax的参数。

当然我不能测试下面的代码,但它可能会做你想做的事,以防我解释得很好。

n = 1
fig = plt.figure()
for column in df3:  #I call the dataframe
    data=df3[column].values  #Turn it into an array

    ax = fig.add_subplot(4,2,n) #I want 4 rows and 2 columns
    im,_ = plot_topomap(data, sensors_pos, cmap='viridis', vmin=0, vmax=100, show=False, axes=ax)
    ax.set_title("KNN" + " " + column) #This is the title for each subplot
    fig.colorbar(im, ax=ax)
    n+=1

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2022-12-17
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多