3

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)
DarenW
  • 16,549
  • 7
  • 63
  • 102
  • Does [this](https://stackoverflow.com/questions/5735208/remove-the-legend-on-a-matplotlib-figure) work for you? – Gabriel Jablonski Jul 10 '18 at 21:19
  • [this](https://stackoverflow.com/questions/17106288/matplotlib-pyplot-will-not-forget-previous-plots-how-can-i-flush-refresh) seems to work rather than the one suggested above. Just clear figure in next iteration. Actual reason why this happens is interesting. – Dinesh Jul 10 '18 at 21:24
  • 1
    It's not the legend that is growing, it's the plot itself. Each time you add a new lineplot via `pp.plot`. At the end you have three line plots in you plot and of course each gets its own legend entry. If the aim is to get new figures each time, use `plt.figure()` at the start of this snippet. – ImportanceOfBeingErnest Jul 10 '18 at 22:07

2 Answers2

5

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

Lorran Sutter
  • 518
  • 3
  • 9
2

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.

Raven Cheuk
  • 2,903
  • 4
  • 27
  • 54