Python equivalent aan ‘wacht even’ in Matlab

Is er een expliciet equivalent commando in de matplotlib van Python voor hold onvan Matlab? Ik probeer al mijn grafieken op dezelfde assen te plotten. Sommige grafieken worden gegenereerd in een for-lus, en deze worden afzonderlijk van suen slgeplot:

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()

Antwoord 1, autoriteit 100%

Noem gewoon plt.show()aan het einde:

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()

Antwoord 2, autoriteit 40%

U kunt het volgende gebruiken:

plt.hold(True)

Antwoord 3, autoriteit 28%

De functie hold onis standaard ingeschakeld in matplotlib.pyplot. Dus elke keer dat je plt.plot()oproept vóór plt.show()wordt er een tekening aan de plot toegevoegd. Het starten van plt.plot()na de functie plt.show()leidt tot het opnieuw tekenen van het hele plaatje.


Antwoord 4, autoriteit 2%

controleer pyplot-documenten. Voor de volledigheid,

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()

Other episodes