【问题标题】:Is there a way to close a plot created with matplotlib from terminal (without having to close the window with mouse)有没有办法从终端关闭使用 matplotlib 创建的绘图(无需用鼠标关闭窗口)
【发布时间】:2021-04-09 12:39:59
【问题描述】:

有没有办法通过终端中的命令关闭使用 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()

在命令行中,当我执行python plot1.py 时,这将打开一个显示绘图的窗口。我想做的是用键盘命令从终端关闭这个窗口。我试过 ctrl + cctrl + y 没有成功,我必须用鼠标关闭窗口点击关闭按钮

注意:我没有使用 anaconda(如 this question 中所述

【问题讨论】:

    标签: python matplotlib command-line terminal


    【解决方案1】:

    您可以使用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 热键而不是终端快捷方式...

    【讨论】:

    猜你喜欢
    • 2019-05-08
    • 2016-03-10
    • 2013-03-01
    • 2017-07-02
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多