0

I would like to create a figure with 2x2 subplots. Then with some input from the user (on a different thread), the figure changes to MxN group of sub plots without creating another figure box. Is this possible?

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()

These posts do not help because it creates new figures (at least in my implementation, if they should't let me know and I will keep trying): Dynamically add/create subplots in matplotlib

matplotlib dynamic number of subplot

laserpython
  • 300
  • 1
  • 6
  • 21
  • Would you care to elaborate why those posts do not help? What is different in your case. It seems from the outset at least, that what you require is a combination of those posts. – Duck Dodgers Dec 31 '18 at 17:36
  • Or this: https://stackoverflow.com/questions/35322009/can-i-tell-python-to-put-an-existing-figure-in-a-new-figure – doctorlove Dec 31 '18 at 17:39
  • @JoeyMallone I tried the combo and I got new figures each iteration. I went through the docs and came up with this example I added and its not working... I will now go back and try the combo again – laserpython Dec 31 '18 at 18:25
  • @doctorlove I don't understand that example – laserpython Dec 31 '18 at 18:26
  • I updated matplotlib from 2.0.2 to 3.0.x and used gridspec and it works... Should I delete question? or answer it? – laserpython Dec 31 '18 at 20:06
  • I think answering it is sensible – doctorlove Jan 02 '19 at 09:40

1 Answers1

0

user inputs which data to plot, then M,N are auto generated. We clear figure, add gridpsec, add subplot, plot data. Same figure is reused, just new data each time.

clear_figure()
gs = fig.add_gridspec(M, N, wspace=0.1)
ax = fig.add_subplot(gs[M,N])
ax.plot(x,y)
laserpython
  • 300
  • 1
  • 6
  • 21