Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the legend in matplotlib not clear itself after `savefig()`?

I'm using Python 3.6 interactively. If I paste the code below from a text file into the Python command line, it works fine multiple times in a row. However, when commenting out show() and enabling the pp.savefig(...) line, the legend grows repeating 'abc' several times every time I pasted the whole chunk of code. What is going on? Is there a way to clear the legend so it starts over freshly each time?

import matplotlib.pyplot as pp

pp.title("Szekeres Polynomials")
pp.legend([]) # clears the legend? no!
pp.plot([1,2,3], [8,5,4], '-', label='xxxabc' )
pp.legend(loc='best', shadow=True )
#pp.show()
pp.savefig('TMPxxx.eps', format='eps', dpi=600)
like image 718
DarenW Avatar asked Oct 22 '25 19:10

DarenW


2 Answers

In this case you should close plot object before saving a new figure so as to avoid appending information:

import matplotlib.pyplot as pp

pp.title("Szekeres Polynomials")
pp.legend([]) # clears the legend? no.
pp.plot([1,2,3], [8,5,4], '-', label='xxxabc' )
pp.legend(loc='best', shadow=True )
#pp.show()
pp.savefig('TMPxxx.eps', format='eps', dpi=600)

# Close last plot object 
plt.close()

References:

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.close.html

like image 74
Lorran Sutter Avatar answered Oct 24 '25 09:10

Lorran Sutter


Just like what @ImportanceOfBeingErnest commented on your question, each time you run pp.plot(), it will plot one more line on the same figure if you did not specify which figure to plot on. To prevent this ambiguity You might want to follow @Lorran Sutter's suggestion or start using objects in matplotlib, then your code will become:

fig1 = pp.figure() #Creating new figure
ax1 = fig1.add_subplot(111) #Creating axis
ax1.set_title("Szekeres Polynomials")
ax1.plot([1,2,3], [8,5,4], '-', label='xxxabc' )
ax1.legend(loc='best', shadow=True)
fig1.savefig('TMPxxx.eps', format='eps', dpi=600)

This ensures every new plot is on a new figure, not plotting on the previous figure.


Benefits of using objects

Plotting graph in this way not only solves your problem, but also allows you do advanced plots and save multiple figure without confusion.

For example, when plotting three figures and each of the figure contains several subplots inside:

fig = pp.figure() #Creating the first figure    
ax = fig.add_subplot(111)
ax.set_title("Szekeres Polynomials")
ax.plot([1,2,3], [8,5,4], '-', label='xxxabc' )
ax.legend(loc='best', shadow=True)
fig.savefig('TMPxxx.eps', format='eps', dpi=600)

fig1 = pp.figure() #Creating the second figure    
ax1 = fig1.add_subplot(121)
ax1.set_title("Szekeres Polynomials")
ax1.plot([1,2,3], [8,5,4], '-', label='xxxabc' )
ax1.legend(loc='best', shadow=True) 

ax2 = fig1.add_subplot(122)
ax2.set_title("Second Szekeres Polynomials")
ax2.plot([3,9,10], [10,15,20], '-', label='xxx' )
ax2.legend(loc='best', shadow=True)
fig1.savefig('TMPxxx2.eps', format='eps', dpi=600)

fig2 = pp.figure() #Creating the third figure    
ax21 = fig2.add_subplot(131)
ax21.set_title("hahah")
ax21.plot([1,2,3], [1,2,3], '-', label='1', c='r')
ax21.legend(loc='best', shadow=True)

ax22 = fig2.add_subplot(132)
ax22.set_title("heheh")
ax22.plot([1,2,3], [-1,-2,-3], '-', label='2', c='b')
ax22.legend(loc='best', shadow=True)

ax23 = fig2.add_subplot(133)
ax23.set_title("hohoho")
ax23.plot([1,2,3], [2**2,4**2,6**2], '-', label='3', c='g' )
ax23.legend(loc='best', shadow=True)

fig2.savefig('graph2.eps', format='eps', dpi=600)

enter image description here enter image description here enter image description here

You can easily adjust parameters each individual subplot and save the three figures without a single confusion.

like image 45
Raven Cheuk Avatar answered Oct 24 '25 09:10

Raven Cheuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!