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') 
3
  • For the issue with saving, you should take a look here: stackoverflow.com/questions/21875356/… Commented Jun 14, 2018 at 3: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, and in the process you'll likely find the issue Commented Jun 14, 2018 at 3:03
  • 1
    @user6703592 Can you please rollback the misleading changes introduced to your code and format it according to your intention? Commented Jun 14, 2018 at 19:32

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Initially the saving was outside the loop. When the post was edited, the indentation was modified.
@xg.plt.py Meh. Who approves edits that create pseudoproblems? ... I better check it was not me.
@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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.