2017-08-11 77 views
0

如何,我可以从matplotlib例如画廊在this链接获取此代码Matplotlib面向对象的代码在笔记本内嵌显示

# -*- noplot -*- 
""" 
============================= 
The object-oriented interface 
============================= 

A pure OO (look Ma, no pylab!) example using the agg backend 
""" 
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas 
from matplotlib.figure import Figure 

fig = Figure() 
canvas = FigureCanvas(fig) 
ax = fig.add_subplot(111) 
ax.plot([1, 2, 3]) 
ax.set_title('hi mom') 
ax.grid(True) 
ax.set_xlabel('time') 
ax.set_ylabel('volts') 

向我展示图表内嵌在我的笔记本任何想法?

请注意:

  • 我想避免使用pyplot因为我想用自己的“面向对象”库使用matplotlib只
  • 我没有问题,得到基于pyplot地块呈现内嵌在我使用matplotlib的%matplotlib inline%matplotlib notebook魔法

这混乱的面向对象的API笔记本不一定渲染内联。

我应该使用不同的画布吗?

使用fig.show()给了我下面的错误

AttributeError: 'FigureCanvasAgg' object has no attribute 'manager' Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().

而且,这个特殊的画布上没有一个show方法。所以我完全失去了如何让这些dom Obj Oriented绘图呈现内联。

+0

搞怪够有一个类似的问题,就在昨天问:https://stackoverflow.com/questions/45614123/showing-several-figures-at-once – ImportanceOfBeingErnest

回答

0

要显示其并不住在pyplot,有没有与之相关的人物经理一个数字,你可以使用IPython.core.display

from IPython.core.display import display 
display(fig) 

enter image description here


只要注意,实际上是有 没有原因根本就是不要用pyplot来创建图形。使用pyplot,代码更清晰并且会自动显示。

%matplotlib inline 
import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.plot([1, 2, 3]) 
ax.set_title('hi mom') 
ax.grid(True) 
ax.set_xlabel('time') 
ax.set_ylabel('volts'); 
+0

多谢!我发现使用pyplot更直观,更清洁。你会知道为什么matplotlib文献建议使用面向对象的接口(我认为它不使用pyplot)? – Shanbhag

相关问题