Python 面向对象的pyplot
我需要处理pyplot对象,如图形和轴。以下是我想要的一个简化示例:Python 面向对象的pyplot,python,matplotlib,Python,Matplotlib,我需要处理pyplot对象,如图形和轴。以下是我想要的一个简化示例: In [1]: import matplotlib.pyplot as mp In [2]: fig = mp.figure() # create a figure In [3]: mp.show() # and immediately show it. And close. In [4]: ax = fig.add_subplot(111) # T
In [1]: import matplotlib.pyplot as mp
In [2]: fig = mp.figure() # create a figure
In [3]: mp.show() # and immediately show it. And close.
In [4]: ax = fig.add_subplot(111) # Then I create a plot on that figure
In [5]: ax.plot([1, 2, 3])
Out[5]: [<matplotlib.lines.Line2D at 0x104e29a50>]
In [6]: mp.get_fignums() # But I already released the figure, so it doesn't appear in the list of available figures
Out[6]: []
In [7]: fig.axes[0].lines[0].get_data() # The data is there, on the plot
Out[7]: (array([ 0., 1., 2.]), array([1, 2, 3]))
In [8]: mp.show() # But mp.show() shows nothing.
[1]中的:将matplotlib.pyplot作为mp导入
在[2]中:fig=mp.figure()#创建一个图形
在[3]中:mp.show()#并立即显示它。然后关闭。
在[4]:ax=fig.add_子图(111)#中,我在该图上创建了一个绘图
[5]中的ax.plot([1,2,3])
输出[5]:[]
在[6]中:mp.get_fignums()#但是我已经发布了这个数字,所以它没有出现在可用数字列表中
输出[6]:[]
在[7]中:图。轴[0]。线[0]。获取数据()。数据在绘图上
Out[7]:(数组([0,1,2]),数组([1,2,3]))
在[8]中:mp.show()#但是mp.show()什么也不显示。
fig.show()也不起作用。发布后如何显示图形
UPD:有一个类似的问题:,但没有答案。试试这个:
import matplotlib.pyplot as mp
fig = mp.figure()
plt.show() # empty figure appears, close it
fig = plt.gcf() # get current figure, this is the key piece.
ax = fig.add_subplot(111) # added axes object
ax.plot([1,2,3])
plt.show()
当我这样做的时候,我能够让绘图显示一条对角线。我找到了它!让我们创建一个mp.Figure() 现在它没有连接到pyplot,所以我们无法显示它。这相当于关闭图形时发生的情况。无法显示未连接到pyplot的图形这一事实已得到充分证明。试试看
In []: fig.show?
Docstring:
If using a GUI backend with pyplot, display the figure window.
For non-GUI backends, this does nothing.
(我减少了帮助消息的文本。)
但也有可能欺骗pyplot。让我们创建一个图形:
temp_fig = mp.figure()
从temp_fig中窃取图形管理器并将其分配给我们的fig:
m = mp.get_current_fig_manager()
fig.canvas.manager = m
现在我们可以展示:
mp.show() # Shows the fig figure.
当然,删除temp_fig是一种很好的做法:
del temp_fig
这个问题与这个问题有关
pyplot
界面是一个方便的模块,用于跟踪a)打开的图形和b)当前图形和当前轴。在它下面是使用OO接口
要有一个开放的图形并能够在repl中输入新命令,您需要处于“交互式”模式,该模式集成了python repl循环和GUI事件循环
从您的问题来看,您似乎正在使用IPython,因此请使用%matplotlib
魔术:
16:31 $ ipython
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:16:01)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %matplotlib
Using matplotlib backend: Qt4Agg
In [2]: import matplotlib.pyplot as plt
In [3]: fig, ax = plt.subplots() # prompt returns immediatly leaving open figure
In [4]: ln, = ax.plot(range(15), label='test') # draws line and updates figure
In [5]: ln.set_linewidth(5) # changes lw and updates screen
In [6]:
为什么不从一个工作的
matplotlib
示例开始呢?我很乐意看到一个工作示例,其中显示了如何获取发布的数据。不幸的是,它是错误的。当您执行plt.gcf()时,您实际上创建了一个新图形,并将此新图形分配给旧变量fig。因此,您松开了旧图形。您可以检查调用plt.gcf()之前和之后,fig的地址是否不同。这使得它比需要的更复杂。如果要创建图形
对象,则还必须为其创建画布
对象。对于GUI后端,Canvas
对象是给定框架的“小部件”的子类,必须嵌入到GUI中,图形管理器负责pyplot
的这一点。出于这些原因,我对此投了反对票。请不要把它当回事,我只是想通过一种比评论更有力的方法表明,这个答案不是交互式使用mpl的推荐方式。
16:31 $ ipython
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:16:01)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %matplotlib
Using matplotlib backend: Qt4Agg
In [2]: import matplotlib.pyplot as plt
In [3]: fig, ax = plt.subplots() # prompt returns immediatly leaving open figure
In [4]: ln, = ax.plot(range(15), label='test') # draws line and updates figure
In [5]: ln.set_linewidth(5) # changes lw and updates screen
In [6]: