我是编码的新手,此刻我试图编码一个从模拟跟踪移动的应用程序,并可视化绘图窗口中的运动。
似乎没有断路命令。
这是 @bf-g的答案的建议,下面是 @bf-g的建议:
def handle_close(evt):
raise SystemExit('Closed figure, exit program.')
fig = plt.figure()
fig.canvas.mpl_connect('close_event', handle_close)# Definition der figure
while True:
plt.clf() # vorherige Plots clearen
for i in range(0, int(alpha_max), 5):
plt.plot(Drehachse_x + Radius*np.cos((i+alpha_0)*np.pi/180), Drehachse_y + Radius*np.sin((i+alpha_0)*np.pi/180), color='red', marker='*', markersize=1)
for i in range(0, int(alpha_max), 2):
plt.plot(Drehachse_x + Radius_Heckklappe*np.cos((i+alpha_0+beta_0+delta)*np.pi/180), Drehachse_y + Radius_Heckklappe*np.sin((i+alpha_0+beta_0+delta)*np.pi/180), color='red', marker='*', markersize=1.5)
alpha = "PATH"
Schwerpunkt_x = "PATH"
Schwerpunkt_y = "PATH"
Spindel_Heck_x = "PATH"
Spindel_Heck_y = "PATH"
x=(Drehachse_x, Schwerpunkt_x)
y=(Drehachse_y, Schwerpunkt_y)
x1=(Spindel_Heck_x, Spindel_Karo_x)
y1=(Spindel_Heck_y, Spindel_Karo_y)
plt.axis('equal')
plt.axis([3100, 3800, 600, 1400])
plt.plot(x,y, color="blue", linewidth=2.0, linestyle="-", marker='o', label='$Heckklappe$')
plt.plot(x1, y1, color="green", linewidth=2.0, linestyle="-", marker='o', label='$Spindel$')
plt.plot(x_g_max, y_g_max, color="orange", linewidth=2.0, linestyle="--", marker='*', label='$Maximal$')
plt.plot(x_g_min, y_g_min, color="red", linewidth=2.0, linestyle="--", marker='*', label='$Minimal$')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
fancybox=True, shadow=True, ncol=5)
plt.pause(0.001)
plt.text(0.35, 0.5, 'Close Me!', dict(size=30))
plt.draw()
它可以做我想做的事,但是如果我尝试关闭绘图窗口,它将不断打开直到关闭程序。
我认为这是因为您的 plt.draw()
在当时的循环中,并且每次在while
循环中迭代时,它基本上都会打开一个新窗口,并且可以修复它,您可以将其放置 plt.draw()
循环外,然后在要绘制图表时打破循环。
while True:
将继续完成内部的一切,直到全天结束。因此,它不适合在任何时候更改的程序流。相反,您需要引入一个需要满足循环的条件,while condition==True:
。
在代码方面,以下内容刚刚运行
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
fig, ax = plt.subplots()
phi = 0
x = np.linspace(0,2*np.pi)
y = np.sin(x)
line, = ax.plot(x,y)
while True:
phi += 0.1
y = np.sin(x+phi)
line.set_ydata(y)
plt.pause(0.1)
print(f"Still running; phi={phi}")
plt.ioff()
plt.show()
因此,我们需要引入一个不继续循环的条件。
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
fig, ax = plt.subplots()
phi = 0
x = np.linspace(0,2*np.pi)
y = np.sin(x)
line, = ax.plot(x,y)
condition = True
def on_close(evt=None):
global condition
condition = False
fig.canvas.mpl_connect("close_event", on_close)
while condition:
phi += 0.1
y = np.sin(x+phi)
line.set_ydata(y)
plt.pause(0.1)
print(f"Still running; phi={phi}")
plt.ioff()
plt.show()
这很复杂,因此Matplotlib具有一个内置的动画模块,可以在程序的事件循环中执行相同的操作。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
phi = 0
x = np.linspace(0,2*np.pi)
y = np.sin(x)
line, = ax.plot(x,y)
def animate(i):
phi = i/10
y = np.sin(x+phi)
line.set_ydata(y)
print(f"Still running; phi={phi}")
ani = FuncAnimation(fig, animate)
plt.show()
您正在寻找事件处理程序。您需要监视的事件是图的关闭。定义无限循环外部的图形并添加事件处理程序:
import matplotlib.pyplot as plt
def handle_close(evt):
raise SystemExit('Closed figure, exit program.')
fig = plt.figure()
fig.canvas.mpl_connect('close_event', handle_close)
while True:
plt.text(0.35, 0.5, 'Close Me!', dict(size=30))
plt.show()