Python 相当于 Matlab 中的“保持”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21465988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:55:14  来源:igfitidea点击:

Python equivalent to 'hold on' in Matlab

pythonmatlabgraphmatplotlib

提问by Medulla Oblongata

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 forloop, and these are plotted separately from suand sl:

在 Python 的 matplotlib 中是否有用于 Matlab 的显式等效命令hold on?我试图在相同的轴上绘制我的所有图形。一些图表是一个内部产生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()

采纳答案by Alvaro Fuentes

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

回答by mapsa

You can use the following:

您可以使用以下内容:

plt.hold(True)

回答by freude

The hold onfeature 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.

hold on默认情况下,该功能在matplotlib.pyplot. 因此,每次您plt.plot()plt.show()将绘图添加到绘图之前唤起。plt.plot()在函数之后启动plt.show()会导致重绘整个画面。

回答by CKM

check pyplotdocs. For completeness,

检查pyplot文档。为了完整性,

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