簡體   English   中英

Matplotlib面向對象的代碼可在筆記本中內聯顯示

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

關於如何獲取此代碼的任何想法

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

從matplotlib示例庫中的鏈接中,可以向我顯示筆記本中的在線圖表?

請注意:

  • 我想避免使用pyplot,因為我正嘗試僅通過其“面向對象”庫使用matplotlib
  • 使用%matplotlib inline%matplotlib notebook魔術,可以使基於pyplot的圖在筆記本中內聯呈現沒有問題

matplotlib的這種令人困惑的面向對象的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方法。 因此,我完全迷失了如何獲取這些令人討厭的,面向對象的圖以內嵌方式呈現。

要顯示不存在於pyplot中並且沒有與其關聯的圖形管理器的圖形,可以使用IPython.core.display

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

在此處輸入圖片說明


請注意,實際上根本沒有理由不使用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'); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM