如何在保持plt.show()后不重复重复调用的情况下保留前一个图并不清除图形? [英] How to keep the previous plot and not clear the figure after calling plt.show() without simply repeating the call?

查看:1406
本文介绍了如何在保持plt.show()后不重复重复调用的情况下保留前一个图并不清除图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.cos(x))
plt.show()

此示例在两个窗口中生成两个连续的图,第一个在左边,然后在右边:

This example produces two consecutive plots in two windows, first the one on the left, then the one on the right:

如您所见,正弦消失了,只有余弦出现在第二个窗口中.绘图窗口之间已被清除.但是,这就是我想要的:

As you can see, the sine disappeared and only the cosine shows up in the second window; the plot window has been cleared in between. However, this is what I want:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()

如何保留上一个图,使其也显示在第二个图上?除了简单地重复对plt.plot的先前调用之外,还必须有更好的方法?

How can I keep the previous plot so it shows up in the second plot, too? There has to be a better way other than simply repeating the previous call to plt.plot?

推荐答案

(A)重复先前对plt.plot 的调用:如问题中所述,可以重复先前的调用在添加新图之前创建了图.对于简单的情况,这可能是最简单的解决方案.

(A) Repeat previous calls to plt.plot: Just as mentioned in the question, it's possible to just repeat the previous calls which created the plots before adding the new plot. This might be the easiest solution for simple cases.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()

(B)使用plt.waitforbuttonpress :如果您的情况适用,可以将plt.waitforbuttonpress用作绘图之间的中断,等待鼠标单击或按键直到下一个绘图被添加到同一窗口中.

(B) Use plt.waitforbuttonpress: If this is applicable in your case, you can use plt.waitforbuttonpress as an interrupt between plots which waits for a mouse click or key press until the next plot is added into the same window.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
for f in [np.sin, np.cos]:
    plt.plot(x, f(x))
    plt.waitforbuttonpress()

(C)在plt.show 之前腌制地块:我不知道这对复杂地块的效果如何,但是似乎可以在调用plt.show之前腌制地块,仅用于之后立即将其还原.然后可以在下一次调用plt.show之前添加其他图.

(C) Pickle the plot before plt.show: I don't know how well this works for complex plots, but it seems possible to pickle the plot before calling plt.show, only to immediately restore it afterwards. Then the additional plot can be added before the next call to plt.show.

import matplotlib.pyplot as plt
import numpy as np
import pickle
x = np.linspace(0, 2 * np.pi, 100)
ax = plt.gca()
for f in [np.sin, np.cos]:
    plt.plot(x, f(x))
    temp = pickle.dumps(ax)
    plt.show()
    ax = pickle.loads(temp)

(D)操作图形管理器和画布:本质上,对于每次循环迭代,图形fig被加载"到一个新创建的图形中,然后用新的图形进行更新.

(D) Manipulating figure manager and canvas: Essentially, the figure fig gets "loaded into" a newly created figure for each loop iteration, then updated with new plots.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig = plt.figure()
plt.close()
for f in [np.sin, np.cos]:
    plt.figure()
    fm = plt.get_current_fig_manager()
    fm.canvas.figure = fig
    fig.canvas = fm.canvas
    plt.plot(x, f(x))
    plt.show()
    plt.close()

这篇关于如何在保持plt.show()后不重复重复调用的情况下保留前一个图并不清除图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆