简体   繁体   English

Matplotlib面向对象的代码可在笔记本中内联显示

[英]Matplotlib Object Oriented Code to display inline in the notebook

Any ideas on how I can get this code 关于如何获取此代码的任何想法

# -*- 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')

from the matplotlib example gallery at this link to show me the chart in-line in my notebook? 从matplotlib示例库中的链接中,可以向我显示笔记本中的在线图表?

Please Note: 请注意:

  • I want to avoid using pyplot as I am trying to use matplotlib using their "Object Oriented" Library only 我想避免使用pyplot,因为我正尝试仅通过其“面向对象”库使用matplotlib
  • I have no issues getting pyplot based plots to render inline in my notebook using the %matplotlib inline or %matplotlib notebook magic 使用%matplotlib inline%matplotlib notebook魔术,可以使基于pyplot的图在笔记本中内联呈现没有问题

This confusing Object Oriented API of matplotlib isn't necessarily rendering inline. matplotlib的这种令人困惑的面向对象的API不一定是内联的。

Should I be using a different canvas? 我应该使用其他画布吗?

Using fig.show() gives me the following error 使用fig.show()给我以下错误

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

Also, this particular canvas doesn't have a show method. 另外,此特定画布没有show方法。 So I am totally lost on how to get these darn Obj Oriented plots to render inline. 因此,我完全迷失了如何获取这些令人讨厌的,面向对象的图以内嵌方式呈现。

To display a figure which does not live in pyplot and has no figure manager associated with it, you can use IPython.core.display : 要显示不存在于pyplot中并且没有与其关联的图形管理器的图形,可以使用IPython.core.display

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

在此处输入图片说明


Just note that there is actually no reason at all not to use pyplot to create the figure. 请注意,实际上根本没有理由不使用pyplot来创建图形。 Using pyplot, the code is much cleaner and will automatically show. 使用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'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 %matplotlib 内联与 ​​%matplotlib 笔记本在 pythonAnywhere jupyter 中的显示问题 - %matplotlib inline versus %matplotlib notebook display problems in pythonAnywhere jupyter 当我以面向对象的方式使用 matplotlib 进行编程时,是否可以在 jupyter notebook 中自动完成代码? - Is it possible to autocomplete codes in jupyter notebook while I programing using matplotlib in the way of object-oriented approach? Jupyter Notebook %matplotlib 内联不起作用 - Jupyter Notebook %matplotlib inline Not Working Matplotlib使RectangularSelector适应面向对象 - Matplotlib adapting RectangularSelector for Object Oriented 如何禁用Jupyter笔记本matplotlib内联? - How to DISABLE Jupyter notebook matplotlib plot inline? Matplotlib Notebook Magic内联/绘图命令不起作用 - Matplotlib notebook magic inline/plotting commands not working 在IPython笔记本中“重绘” matplotlib内联图 - “Replot” a matplotlib inline plot in a IPython notebook 在IPython Notebook中自动运行%matplotlib内联 - Automatically run %matplotlib inline in IPython Notebook 在Jupyter Notebook中同时使用Matplotlib内联和QT - Use both matplotlib inline and qt in jupyter notebook Jupyter Notebook`pylab inline`给出`matplotlib`错误 - Jupyter notebook `pylab inline` gives `matplotlib` error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM