python - 有没有办法从终端关闭使用 matplotlib 创建的绘图(无需用鼠标关闭窗口)

标签 python matplotlib command-line terminal

有没有办法通过终端中的命令关闭使用 matplotlib 创建的绘图(无需用鼠标关闭窗口)?

我在 macos 10.15.7 上使用 zsh

假设我有一个包含以下内容的文件 plot1.py:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()

从命令行,当我执行pythonplot1.py时,这将打开一个显示绘图的窗口。我想做的是使用键盘命令从终端关闭此窗口。我尝试了 ctrl + cctrl + y 但没有成功,我必须用鼠标关闭窗口单击关闭按钮

注意:我没有使用 anaconda(如 this question 中提到的)

最佳答案

您可以使用 plt.show 方法的 block 参数:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show(block=False)
plt.pause(1)
input()
plt.close()

使用上面的代码,只要在终端中按 Enter 就可以关闭窗口。


您还可以使用 Alt + F4,确保 matplotlib 窗口位于终端窗口上方。当然,那是 Windows 热键而不是终端快捷键...

关于python - 有没有办法从终端关闭使用 matplotlib 创建的绘图(无需用鼠标关闭窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65550508/

相关文章:

python - Conda:当前 win-64 channel 中缺少包

Python - 如何在 ValidationError 中打印消息

python - 如何在分类数据上使用 seaborn 获取条形图上的标签值

git - 我可以使用 SSH URL 检查 git 存储库是否存在吗?

java - 从命令行使用 Eclipse ant 插件?

python - HTML <li> 有什么限制吗?

python - 将 IronPython 链接到 WPF

python - 如何为已经存在的 x-ticks 标签 matplotlib 添加额外的符号?

python - Matplotlib:在堆叠饼图中放大、填充图例并将其居中

linux - 查看进程/文件是否正在运行 linux