83

Is there an explicit equivalent command in Python's matplotlib for Matlab's hold on? I'm trying to plot all my graphs on the same axes. Some graphs are generated inside a for loop, and these are plotted separately from su and sl:

import numpy as np
import matplotlib.pyplot as plt

for i in np.arange(1,5):
    z = 68 + 4 * np.random.randn(50)
    zm = np.cumsum(z) / range(1,len(z)+1)
    plt.plot(zm)
    plt.axis([0,50,60,80])

plt.show()

n = np.arange(1,51)
su = 68 + 4 / np.sqrt(n)
sl = 68 - 4 / np.sqrt(n)

plt.plot(n,su,n,sl)

plt.axis([0,50,60,80])
plt.show()
6
  • 7
    Do you mean plt.hold(True)? Commented Jan 30, 2014 at 19:44
  • Possible dupe of multiple-plot-in-one-figure-in-python Commented Jan 30, 2014 at 19:46
  • @Cody Piersall - It might work for others, but apparently not for my example. Commented Jan 30, 2014 at 19:48
  • 2
    It should be pointed out the hold=True is the default. Commented Jan 31, 2014 at 3:23
  • I hope this helps since this worked for me: LINK Commented Feb 7, 2017 at 3:01

5 Answers 5

64

Just call plt.show() at the end:

import numpy as np
import matplotlib.pyplot as plt

plt.axis([0,50,60,80])
for i in np.arange(1,5):
    z = 68 + 4 * np.random.randn(50)
    zm = np.cumsum(z) / range(1,len(z)+1)
    plt.plot(zm)    

n = np.arange(1,51)
su = 68 + 4 / np.sqrt(n)
sl = 68 - 4 / np.sqrt(n)

plt.plot(n,su,n,sl)

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Note: This doesn't work in the case of interactive input from the command line. Works fine if run within a script.
24

You can use the following:

plt.hold(True)

2 Comments

This function is deprecated, Any alternative solution like this?
@Jyotirmay The hold function in matplotlib does not serve any purpose and was hence deprecated. pyplot always acts as if hold was set to True anyways.
20

The hold on feature is switched on by default in matplotlib.pyplot. So each time you evoke plt.plot() before plt.show() a drawing is added to the plot. Launching plt.plot() after the function plt.show() leads to redrawing the whole picture.

2 Comments

How would you get around this when using IPython?
use backend %matplotlib inline and all plt.plot() within one cell will be output in the same frame. If you put plt.show() between plt.plot() functions within a cell it will create more frames, new one for each plt.show()
0

check pyplot docs. For completeness,

import numpy as np
import matplotlib.pyplot as plt

#evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

Comments

0

Use plt.sca(ax) to set the current axes, where ax is the Axes object you'd like to become active.

For example:

In a first function: import numpy as np import matplotlib.pyplot as plt

for i in np.arange(1,5):
    z = 68 + 4 * np.random.randn(50)
    zm = np.cumsum(z) / range(1,len(z)+1)
    plt.plot(zm)
    plt.axis([0,50,60,80])

plt.show()

In the next function: def function2(...., ax=None)

if ax is None:
    fig, ax = plt.subplots(1)
else:
    plt.sca(ax)

n = np.arange(1,51)
su = 68 + 4 / np.sqrt(n)
sl = 68 - 4 / np.sqrt(n)

plt.plot(n,su,n,sl)

plt.axis([0,50,60,80])
plt.show()

Comments

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.