仅显示选择的或特定的matplotlib图形 [英] Show only selected or specific matplotlib figures

查看:114
本文介绍了仅显示选择的或特定的matplotlib图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一共生成了50多个图形,但我不希望全部显示.我想挑选出我想要显示的数字.有什么办法吗?

I am generating a total of 50+ figures but I do not wish to display them all. I would like to pick out the figures that I want to display. Is there any way to do it?

例如:我有50个绘图,这些绘图是在程序过程中生成的.最后,当我输入plt.show()时,它将显示所有数字.但是,我只想显示3或4个数字(但它们不是固定的,即我可以一次绘制1、2、3、4图,而另一次可以绘制10、27、33、45图).另外,一个单独的函数正在生成这些数字,我将返回所有数字.

For example: I have 50 plots which are generated over the course of the program. At the end, when I enter plt.show(), it shows all the figures. However, I would like to display only 3 or 4 figures (but they aren't fixed i.e, I could plot figures 1, 2, 3, 4 at one time and another time I could plot figures 10, 27, 33, 45). Also, a separate function is generating these figures and I am returning all the figures.

示例主脚本:

import matplotlib.pylab as plt
import numpy as np
from sampleplotfn import *


A = 1
omega = np.linspace(10,35,50)

for i in range(len(omega)):
    fig1 = sinewave(A,omega[i])

plt.show()

samplepltfn.py

samplepltfn.py

import numpy as np
import matplotlib.pylab as plt


def sinewave(A,omega):
      t = np.linspace(0,10,25)
      f = A*np.sin(omega*t)
      fig1 = plt.figure()
      plt.plot(f,t)
      return fig1

推荐答案

如果您只想绘制一些图形,则可以通过调用各个图形的 show 方法来实现,只要您已经使用 plt.figure 制作了数字.例如.如果制作了fig1和fig2,则调用 fig2.show()而不是 plt.show()时,只能显示fig2.但是,我同意jeanrc的评论-您应该在制作地块之前除掉所需的地块,而不要保留大量未显示的地块(它们仍然占用内存,您必须记住清理它们!)

If you really want to plot only some, you can do so by calling the show method of the individual figures, as long as you've made the figures using plt.figure. E.g. if you made fig1 and fig2, you could show only fig2 if you called fig2.show() instead of plt.show(). However, I agree with jeanrc's comment - you should weed the plots you want before making them instead of keeping a ton of non-shown plots (they still take up memory and you have to remember to clean them up!)

此外,我强烈建议您不要使用pylab,尤其是不要使用"plt"作为导入别名.传统上,"plt"用于pyplot,而不是pylab.(将matplotlib.pyplot导入为plt ).Pylab包含大量其他内容,除了交互式工作外,不建议使用.

Additionally, I would highly recommend against using pylab, and in particular, against using "plt" as an import alias for it. "plt" is traditionally used for pyplot, not pylab. (import matplotlib.pyplot as plt). Pylab includes a ton of other stuff and is discouraged from use except for interactive work.

最后,您正在循环中覆盖"fig1".尝试将图形保存到列表中,而不是单个变量中.

Lastly, you are overwriting "fig1" in your loop. Try saving the figures into a list instead of a single variable.

这篇关于仅显示选择的或特定的matplotlib图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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