1

I'm trying to replace plt.hold() which accumulates lines in a figure.

Research: Several posted suggestions to this issue say to use multiple plt.plot(x,y) commands followed by plt.show(). On my system (Python 3.6) that prints only the content of the last plt.plot() call.

import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0, 2*np.pi, 400)
a = np.sin(t)
b = np.cos(t)

plt.plot(t, a, 'b') 
plt.show()

plot of blue sine wave

Adding another plt.plot() call clears the first line

plt.plot(t, a, 'b') 
plt.plot(t, b, 'g')
plt.show()

plot of a green cosine wave

A method that works is to put them all in one plt.plot() call.

plt.plot(t, a, 'r--', t, b, 'b') 
plt.show()

plot of 2 sine/cosine waves

But I have five lines with labels to plot so this would be a long, hard-to-read argument list.

How can I define each of the lines individually for one figure?

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
GTaylor
  • 61
  • 6
  • 2
    Running your code I get lines plotted on the same plot (which is what I would expect). Can you provide some more details about your setup? – dan_g Aug 15 '18 at 20:02
  • Same as above. matplotlib 2.2.3 – DYZ Aug 15 '18 at 20:03
  • `matplotlib` accumulates the graphs drawn by multiple calls to `plt.plot()` by default. This answer might help: https://stackoverflow.com/a/4805456/1102056 – Sufian Latif Aug 15 '18 at 20:08
  • my matplotlib version is 2.1.2 – GTaylor Aug 15 '18 at 20:08
  • There are numerous examples showing that it works so there must be something wrong with my installation. – GTaylor Aug 15 '18 at 20:10
  • `plt.show()` was designed with the expectation that it be called [only once](https://stackoverflow.com/a/15724812/190597). So normally you would just call all the `plt.plot` commands first, then `plt.show()` at the end. If you want to make an animation, [use `matplotlib.animation`](https://matplotlib.org/examples/animation/basic_example.html). Then you could allow the user to pause the animation on click events, [for instance](https://stackoverflow.com/a/16733373/190597). – unutbu Aug 15 '18 at 20:24
  • It's not necessarily the installation. It might be the way you execute this code. If it is in a script which you call via python, you will sure get 2 lines as expected in any matplotlib version. But if you run this differently, something might prevent that from happening. – ImportanceOfBeingErnest Aug 15 '18 at 21:02
  • `plt.hold()` will toggle the hold state, which make the latter plotting clear the `Axes`. The default setting will not. You should check previous code for this state change. Or just do `plt.hold(True)` to reset it. – Ian Aug 16 '18 at 02:34
  • I was troubleshooting a script that seemed to quit working when the hold calls were removed. Today the simple plot example worked until the problem script ran; then the simple script would print only the last .plot line. – GTaylor Aug 28 '18 at 18:29
  • After I restarted the IDE and fixed a problem in the main script (passing a string used as a value on the Y axis) everything worked as expected. Seems like something was getting screwed up in the environment. – GTaylor Aug 28 '18 at 18:31
  • My script creates plots for each of 50 tests of 20 units. During development it had both `plt.savefig()` and `plt.show()` calls. When the script is run in File Explorer, the `show` call displays each figure and doesn't proceed until you close it. So how should I reset the `plt.plot()` queue (?) so it creates the files without using show? – GTaylor Aug 28 '18 at 21:16
  • Found the answer in [link](https://stackoverflow.com/questions/17106288/matplotlib-pyplot-will-not-forget-previous-plots-how-can-i-flush-refresh/17107568). Use `plt.gcf().clear()` instead of `plt.show()` – GTaylor Aug 29 '18 at 15:34

0 Answers0