python - 在另一个图中按下按钮时自动更新图
问题描述
我在 pyplot 中创建了一个图形,并为其分配了一个回调,该回调应该调用另一个图形的更新。但是,直到我在 PyCharm Python 控制台中键入一些任意命令,它才会更新。
代码片段:
import matplotlib.pyplot as plt
def onclick(event):
global sign
sign *= -1
ax.cla()
x2 = [i for i in range(10)]
y2 = [i*sign for i in range(10)]
ax.plot(x2, y2)
plt.show()
print("Done")
global sign
sign = 1
ax = plt.figure().add_subplot()
ax.set_title("Toggles slope on click in other figure")
fig = plt.figure()
ax2 = fig.add_subplot()
x = [i for i in range(10)]
y = x
ax2.plot(x, y)
ax2.set_title("Click here")
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
单击标题为“单击此处”的轴不会在另一个图形中产生变化,除非我在 PyCharm 控制台中写了一些东西 - 可以是任何东西,比如 i = 2。
如何在第二个图中按下按钮时自动更新第一个图?我尝试按照此处和此处的说明添加 plt.draw()、plt.pause(.)但无济于事。
版本:
Pycharm 2021.2.2 社区版
Python 3.8
Windows 10 Enterprise 10.0.18363
解决方案
推荐阅读
- openedge - 如何为每个循环导出一个文本文件?
- wordpress - 如何通过编程创建 edd disocunt-code?
- unix - 如何使用 x 命令计算 sas 表中的行数
- c# - 以电子邮件正文格式发送 Excel 文件数据
- ruby - 如何使用 Rspec 模拟具有初始化方法的类?
- cts - 如何指明哪个 CTS 模块将对我们对 AOSP 的修改产生影响?
- java - Spring Boot 应用部署错误:“ServletException:无法实例化 WebApplicationInitializer 类”
- angular - 是否有内置的 Angular lostfocus 事件?
- python - 如何使用python找到最适合我的分布的概率密度函数曲线
- html - 在 Winjs 中从 HTML 内容生成 PDF