如何在Python的matplotlib中获取当前图形编号? [英] How do you get the current figure number in Python's matplotlib?

查看:611
本文介绍了如何在Python的matplotlib中获取当前图形编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个示例脚本,该脚本显示了如何在图形之间来回切换.我在这里找到了示例: http://matplotlib.org/examples/pylab_examples/multiple_figs_demo.html当我尝试打印数字时,我得到的是数字(640x480)",而不是我期望的数字.您如何获得电话号码?

I'm playing around with an example script that shows how to switch back and forth between figures. I found the example here: http://matplotlib.org/examples/pylab_examples/multiple_figs_demo.html When I try to print out the figure number I get "Figure(640x480)" instead of the number 1 I was expecting. How do you get the number?

# Working with multiple figure windows and subplots
import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.sin(4*np.pi*t)

plt.figure(1)
plt.subplot(211)
plt.plot(t, s1)
plt.subplot(212)
plt.plot(t, 2*s1)

plt.figure(2)
plt.plot(t, s2)

# now switch back to figure 1 and make some changes
plt.figure(1)
plt.subplot(211)
plt.plot(t, s2, 's')
ax = plt.gca()
ax.set_xticklabels([])

# Return a list of existing figure numbers.
print "Figure numbers are = " + str(plt.get_fignums())
print "current figure = " + str(plt.gcf())
print "current axes   = " + str(plt.gca())

plt.show()

以下是输出:

Figure numbers are = [1, 2]
current figure = Figure(640x480)
current axes   = Axes(0.125,0.53;0.775x0.35)

推荐答案

Figure对象具有number属性,因此您可以通过以下方式获取数字:

The Figure object has a number attribute, so you can obtain the number via

>>> plt.gcf().number

这篇关于如何在Python的matplotlib中获取当前图形编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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