0

I'm trying to produce two different figures to separate files (.png images) that use the same basic data, but I get blank white images. However, both figures seem ok when shown in a jupyter notebook with plt.show() statement. My relevant code looks like:

plt.figure(1)
fig1, axs1 = plt.subplots(2,2)
plt.figure(2)
fig2, axs2 = plt.subplots(2,2)

# generate 4 datasets in a for loop
for i in range(4): 
    x,y = createRandomData(parameters)
    fit1 = doFit1(x,y)
    plt.figure(1)
    axs1[i//2,i%2].plot(x,y,color='black',linewidth = 1)
    axs1[i//2,i%2].plot(x,fit1,color='green',linewidth = 1)
    fit2 = doFit2(x,y)
    plt.figure(2)
    axs2[i//2,i%2].plot(x,y,color='black',linewidth = 1)
    axs2[i//2,i%2].plot(x,fit1,color='green',linewidth = 1)
plt.figure(1)
plt.gcf().set_size_inches(12,8)
plt.gcf().savefig('dataWithFit1.png', dpi=200)
plt.show()
plt.figure(2)
plt.gcf().set_size_inches(12,8)
plt.gcf().savefig('dataWithFit2.png', dpi=200)
plt.show()

Can't figure out why savefig() does not produce the corrent output even if following show() statements produce the correct output to browser inside jupyter notebook.

I have also tried variable fig1 instead of plt.gcf().

Any suggestions?

PSm
  • 13
  • 1
  • 5
  • Your code creates 3 figures when I run it. The first one is blank and the others have the plotted data. A good practice would be to only create the figures just before you plot to them and when you've saved the figure, run `plt.close()` on the next line before creating another figure to save. – kynnemall Apr 22 '20 at 09:08
  • It also creates many subplots and it doesn't seem like they're needed – kynnemall Apr 22 '20 at 09:11
  • Get rid of your `plt.figure(x)` statements. The Axes objects created by `subplots` (axs1 & axs2) are sufficient to identify and separate your figures. Similarly, replace `plt.gcf()` by the Figure objects returned by `subplots`: fig1 & fig2. – Patol75 Apr 22 '20 at 11:14
  • @kynnem Code creates one subplot to both figures in each for loop iteration. For the same reason, I need to keep both figures open until the end. But of course, close statements could be added to the end. – PSm Apr 22 '20 at 14:16

1 Answers1

0

Thanks to @Patol75 's comment, I have modified the code to:

fig1, axs1 = plt.subplots(2,2)
fig2, axs2 = plt.subplots(2,2)

# generate 4 datasets in a for loop
for i in range(4): 
    x,y = createRandomData(parameters)
    fit1 = doFit1(x,y)
    axs1[i//2,i%2].plot(x,y,color='black',linewidth = 1)
    axs1[i//2,i%2].plot(x,fit1,color='green',linewidth = 1)
    fit2 = doFit2(x,y)
    axs2[i//2,i%2].plot(x,y,color='black',linewidth = 1)
    axs2[i//2,i%2].plot(x,fit2,color='green',linewidth = 1)
fig1.set_size_inches(12,8)
fig1.savefig('dataWithFit1.png', dpi=200)
fig1.show()
fig2.set_size_inches(12,8)
fig2.savefig('dataWithFit2.png', dpi=200)
plt.show()

Also the last plot command had a typo in it (should plot fit2 instead of fit1) introduced when "pseudofying" the code for the post.

It now shows and saves the figures correctly. However, I'm still getting a warning UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure. in jupyter notebook. So, if one of you has any insight into that, please do add a comment for completeness.

PSm
  • 13
  • 1
  • 5