【问题标题】:Add subplots to matplotlib figure after creation创建后将子图添加到 matplotlib 图
【发布时间】:2018-12-31 17:32:48
【问题描述】:

我想创建一个带有 2x2 子图的图形。然后通过用户的一些输入(在不同的线程上),图​​形更改为 MxN 组子图,而无需创建另一个图形框。这可能吗?

x = [1,2,3]
y = [1,2,3]
fig, axs = plt.subplots(222)
threadedPlotShow(ax, x, y) #in a different thread, shows figure with xy on each

#wait for user input
m = raw_input("enter rows")
n = raw_input("enter cols")

update_figure(x,y,M,N,fig)

def update_figure(self, x, y, M, N, fig):
    ax=fig.add_subplot(nrows=M,ncols=N, index=M+N+1)
    ax.plot(x,y)
    plt.draw()

这些帖子没有帮助,因为它创建了新的数字(至少在我的实施中,如果他们不应该让我知道,我会继续尝试): Dynamically add/create subplots in matplotlib

matplotlib dynamic number of subplot

【问题讨论】:

  • 您愿意详细说明为什么这些帖子没有帮助吗?你的情况有什么不同。至少从一开始,您需要的是这些帖子的组合。
  • @JoeyMallone 我尝试了这个组合,每次迭代我都会得到新的数字。我浏览了文档并提出了我添加的这个示例,但它不起作用......我现在将返回并再次尝试组合
  • @doctorlove 我不明白那个例子
  • 我将 matplotlib 从 2.0.2 更新到 3.0.x 并使用了 gridspec 并且它有效...我应该删除问题吗?还是回答?

标签: python matplotlib


【解决方案1】:

用户输入要绘制的数据,然后自动生成 M,N。我们清除图形,添加gridpsec,添加子图,绘制数据。重复使用相同的图形,每次只是新数据。

clear_figure()
gs = fig.add_gridspec(M, N, wspace=0.1)
ax = fig.add_subplot(gs[M,N])
ax.plot(x,y)

【讨论】:

    猜你喜欢
    • 2016-04-28
    • 2012-01-20
    • 2012-09-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2016-11-29
    相关资源
    最近更新 更多