简体   繁体   English

Python 相当于 Matlab 中的“保持”

[英]Python equivalent to 'hold on' in Matlab

Is there an explicit equivalent command in Python's matplotlib for Matlab's hold on ?在 Python 的 matplotlib 中是否有明确的等效命令用于 Matlab 的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 :一些图是在for循环内生成的,这些图与susl分开绘制:

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

Just call plt.show() at the end: 只需在结尾处调用plt.show()

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

您可以使用以下内容:

plt.hold(True)

The hold on feature is switched on by default in matplotlib.pyplot . 默认情况下,在matplotlib.pyplot打开hold on功能。 So each time you evoke plt.plot() before plt.show() a drawing is added to the plot. 因此,每次在plt.plot()之前唤起plt.plot()plt.show()将绘图添加到绘图中。 Launching plt.plot() after the function plt.show() leads to redrawing the whole picture. 启动plt.plot()函数后plt.show()导致重画的全貌。

check pyplot docs. 检查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()

Use plt.sca(ax) to set the current axes, where ax is the Axes object you'd like to become active.使用 plt.sca(ax) 设置当前轴,其中 ax 是您想要激活的 Axes 对象。

For example:例如:

In a first function: import numpy as np import matplotlib.pyplot as plt在第一个函数中: 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)在下一个函数中: 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM