0

Here is the code of plotting the figures. But why are there always two empty figures before the third expected figure, it seems I created two blank fig.

And I cannot save the figure in my local computer fig.savefig('Sens.png'). There is an error The C++ part of the object has been deleted, attribute access no longer allowed(actually successfully saved only for one time).

fig = plt.figure(figsize=(10,10))
m = 1
for s in dataList:
   plt.subplot(2,2,m)
   f = interp1d(FXSpotList, s, 'cubic')    
   xnew = np.linspace(FXSpotList[0], FXSpotList[-1], 40, True)
   plt.plot(xnew, f(xnew), '-')
   plt.xlabel('Spot')
   plt.ylabel(titleList[m-1])
   plt.axvline(x=tradeTest.Pair().Spot(), linestyle='--')  
   plt.axhline(y=0, linestyle='--')
   m = m + 1

plt.figtext(0.5, 0.01, 'Type='+str(tradeTest.Types()[0]), ha='center')
plt.tight_layout()
plt.show()
plt.close()
fig.savefig('Sens.png') 
user6703592
  • 1,004
  • 1
  • 12
  • 27
  • For the issue with saving, you should take a look here: https://stackoverflow.com/questions/21875356/saving-a-figure-after-invoking-pyplot-show-results-in-an-empty-file – OriolAbril Jun 14 '18 at 03:01
  • 1
    For the 2 empty figures, the code looks ok, it will probably be a problem with your data. Try to generate an [mcve](https://stackoverflow.com/help/mcve), and in the process you'll likely find the issue – OriolAbril Jun 14 '18 at 03:03
  • 1
    @user6703592 Can you please rollback the misleading changes introduced to your code and format it according to your intention? – Mr. T Jun 14 '18 at 19:32

1 Answers1

2

Although you did not provide a Minimal, Complete, and Verifiable example, it is obvious that there are things wrong with your loop construction. You show, close, then save the plot in every loop, which is probably not, what you are intending to do. A minimal example of your loop would be

import numpy as np
from matplotlib import pyplot as plt

#sample list to iterate over
dataList = ["fig1", "fig2", "fig3"]

plt.figure(figsize=(10,10))
#loop over the list, retrieve data entries and index
for i, s in enumerate(dataList):
    #define position of the plot in a 2 x 2 grid
    plt.subplot(2, 2, i + 1)
    #random plot, insert your calculations here
    plt.plot(range(3), np.random.randint(0, 10, 3))
    #utilize list data
    plt.title(s)

#save figure
plt.savefig('test.png') 
#show figure
plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Initially the saving was outside the loop. When the post was edited, the indentation was modified. – OriolAbril Jun 14 '18 at 19:23
  • @xg.plt.py Meh. Who approves edits that create pseudoproblems? ... I better check it was not me. – Mr. T Jun 14 '18 at 19:28
  • @Mr.T thanks, the later part of my code should be outside the loop, it's a typo. And it seems that I should save the fig before the `plt.show()`, but I have closed the fig after showing, why can not I save the fig? – user6703592 Jun 15 '18 at 06:43
  • @user6703592 After correcting the indentation, your `plt.close` is redundant, because `plt.show` is blocking and you will execute `plt.close` anyhow, when you close the shown figure. This destroys the figure instance. To my surprise, you can still access the data from `fig`, which includes saving `fig`. But you can't modify it, because matplotlib considers this application destroyed. This doesn't solve your problem at all but as it is stated now, I can't replicate your error message. – Mr. T Jun 15 '18 at 07:33