如何切换 matplotlib 图形的可见性? [英] How to toggle visibility of matplotlib figures?

查看:48
本文介绍了如何切换 matplotlib 图形的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 matplotlib 图形消失并重新出现以响应某些事件?(即按键)

Is there a way that I can make a matplotlib figure disappear and reappear in response to some event? (i.e. a keypress)

我尝试过使用 fig.set_visible(False) 但这似乎对我没有任何作用.

I've tried using fig.set_visible(False) but that doesn't seem to do anything for me.

简单的代码示例:

import matplotlib
import matplotlib.pyplot as plt

fig=matplotlib.pyplot.figure(figsize=(10, 10))

# Some other code will go here

def toggle_plot():
  # This function is called by a keypress to hide/show the figure
  fig.set_visible(not fig.get_visible()) # This doesn't work for me

plt.show()

我尝试这样做的原因是因为我在图形上运行了一堆绘图/动画来显示正在运行的模拟的输出,但是一直显示它们会大大降低我的计算机的速度.有什么想法吗?

The reason I'm trying to do this is because I have a bunch of plots/animations running on the figure that show the output of a running simulation, but displaying them all the time slows down my computer a lot. Any ideas?

推荐答案

您必须调用 plt.draw() 来实际实例化任何更改.这应该起作用:

You have to call plt.draw() to actually instantiate any changes. This should work:

def toggle_plot():
  # This function is called by a keypress to hide/show the figure
  fig.set_visible(not fig.get_visible())
  plt.draw()

这篇关于如何切换 matplotlib 图形的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆