Python equivalent to 'hold on' in Matlab
234,224
Solution 1
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()
Solution 2
You can use the following:
plt.hold(True)
Solution 3
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.
Solution 4
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()
Author by
Medulla Oblongata
If you want to build a ship, don't drum up people to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea.
Updated on October 22, 2021Comments
-
Medulla Oblongata over 2 years
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 afor
loop, and these are plotted separately fromsu
andsl
: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()
-
Jyotirmay almost 7 yearsThis function is deprecated, Any alternative solution like this?
-
ZZZ almost 7 yearsNote: This doesn't work in the case of interactive input from the command line. Works fine if run within a script.
-
ImportanceOfBeingErnest about 5 years@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. -
ThatNewGuy about 3 yearsHow would you get around this when using IPython?
-
freude about 3 yearsuse 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()