创建后将子图添加到matplotlib图

时间:2018-12-31 17:32:48

标签: python matplotlib

我想创建一个具有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

1 个答案:

答案 0 :(得分:0)

用户输入要绘制的数据,然后自动生成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)