Python matplotlib:在绘制更多图形后显示相同的图形

Python matplotlib:在绘制更多图形后显示相同的图形,python,matplotlib,plot,click,point,Python,Matplotlib,Plot,Click,Point,我想做这样的事情,数字是一样的 fig = plt.figure() plt.plot(x1,y1) plt.show() 它将在图1中的x1,y1处显示一个点 然后,如果我单击鼠标或按键,则会出现以下情况: plt.plot(x2,y2) plt.show() 但是图形窗口不应该关闭,它应该在它上面画一个新的点 我想为数学演示做这样的事情,我知道这根本没有必要,但我有这样的想法,想知道python是否有可能。我以前做过MATLAB,像这样的东西要容易得多。最简单的方法是在matplo

我想做这样的事情,数字是一样的

fig = plt.figure()

plt.plot(x1,y1)

plt.show()
它将在图1中的x1,y1处显示一个点

然后,如果我单击鼠标或按键,则会出现以下情况:

plt.plot(x2,y2)

plt.show()
但是图形窗口不应该关闭,它应该在它上面画一个新的点


我想为数学演示做这样的事情,我知道这根本没有必要,但我有这样的想法,想知道python是否有可能。我以前做过MATLAB,像这样的东西要容易得多。

最简单的方法是在matplotlib中启用“交互模式”,它会自动利用更改。这是一种在命令行中执行操作的好方法,相当于MATLAB的工作方式。但是,它速度较慢,因此最好不要在脚本中使用它,因此它不是默认值:

import matplotlib.pyplot as plt

x1 = 1
x2 = 2

y1 = 1
y2 = 4

plt.ion()  # turn on interactive mode
plt.figure()
plt.xlim(0, 10)  # set the limits so they don't change while plotting
plt.ylim(0, 10)
plt.hold(True)  # turn hold on

plt.plot(x1, y1, 'b.')

input()  # wait for user to press "enter", raw_input() on python 2.x

plt.plot(x2, y2, 'b.')
plt.hold(False)  # turn hold off
对于循环,其工作原理如下:

import matplotlib.pyplot as plt
import numpy as np

xs = np.arange(10)
ys = np.arange(10)**2

plt.ion()
plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 100)
plt.hold(True)

for x, y in zip(xs, ys):
    plt.plot(x, y, 'b.')
    input()

plt.hold(False)
但是,如果使用IPython,只需使用
%pylab
,它负责导入所有内容并启用交互模式:

%pylab

xs = arange(10)
ys = arange(10)**2

figure()
xlim(0, 10)  # set the limits so they don't change while plotting
ylim(0, 100)
hold(True)

for x, y in zip(xs, ys):
    plot(x, y, 'b.')
    input()  # raw_input() on python 2.x

hold(False)