繁体   English   中英

绘图窗口关闭后保持打开状态

[英]Plot window keeps opening after closing

我是编码的新手,目前正在尝试编写一个应用程序,该应用程序可跟踪模拟中的运动并在绘图窗口中可视化该运动。

没有break命令似乎起作用。

这是更新的代码,其中@ 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循环内,并且每次在while循环中进行迭代时,它基本上都会打开一个新窗口,并且要修复该问题,可以将plt.draw()放在循环,然后在要绘制图形时中断循环。

您正在寻找一个事件处理程序 您需要监视的事件是该图的关闭。 在无限循环外定义图形并添加事件处理程序:

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

while True:将继续做所有内部工作,直到一天结束。 因此,它不太适合随时更改的程序流。 相反,您需要引入一个条件,循环继续才能满足该while condition==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()

暂无
暂无

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

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