2013-03-18 58 views
4

我正在調用IPython Notebook的內聯模式;用fig.show()內聯IPython Notebook圖形?

%pylab inline 

而下面的代碼立即在單元格上繪製一個圖形;

fig = plt.figure() 
axes = fig.add_axes([0, 0, 1, 1]) 

但是我想在一個單元格中創建繪圖/座標軸等,並稍後使用或許繪圖;

fig.show() 

如何獲得對內聯模式的更多控制?如果我不使用%pylab內聯,它會在我不想要的單獨窗口中創建圖表(並且通常會凍結窗口)。

版本;

Numpy: 1.7.0 
Matplotlib: 1.2.1rc1 
Python: 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)] 
Pandas: 0.10.1 
PyLab: 1.7.0 
+0

您是否嘗試過與IPython中的顯示功能?就我所知,它是「正式的」方法 – EnricoGiampieri 2013-03-18 16:43:20

+0

是不是你想要生成圖形,或者你只是不想看空軸? – askewchan 2013-03-18 16:45:05

+0

一般來說,我想在一個單元格中創建一個圖形,在其中使用它的句柄在另一個單元格中進行操作,然後暫緩顯示它,直到稍後再有一個不同的單元格。 – 2013-03-19 12:40:40

回答

5

你可能會尋找禁用自動關閉圖:

InlineBackend options 
--------------------- 
--InlineBackend.close_figures=<CBool> 
    Default: True 
Close all figures at the end of each cell. 
When True, ensures that each cell starts with no active figures, but it also 
means that one must keep track of references in order to edit or redraw 
figures in subsequent cells. This mode is ideal for the notebook, where 
residual plots from other cells might be surprising. 
When False, one must call figure() to create new figures. This means that 
gcf() and getfigs() can reference figures created in other cells, and the 
active figure can continue to be edited with pylab/pyplot methods that 
reference the current active figure. This mode facilitates iterative editing 
of figures, and behaves most consistently with other matplotlib backends, 
but figure barriers between cells must be explicit. 

的是,IPython中會顯示這個數字如果單元格的最後一行返回圖對象,那麼就可以避免與結局呢a ;或添加pass作爲最後一行。

+0

c.InlineBackend.close_figures = False現在在\ myIPythonDir \ profile_myHomeProfile \ ipython_notebook_config.py中設置。座標軸= fig.add_axes([0,0,1,1])cmd在單元格後仍然給我一個繪圖,即使是分號和通過。任何方式來查看當前選項來檢查這個標誌是否確實設置? – 2013-03-19 12:35:25

+0

'%config InlineBackend'應該告訴你不同的配置值。 – Matt 2013-03-19 16:33:22

+0

這個幾乎可以工作!我只是無法讓數字在中間細胞中表現出來,即:或者通過 – nrob 2014-02-05 09:36:43

5

所以我猜你想要的是這樣的:

from matplotlib.backends.backend_agg import FigureCanvasAgg as fc 
fig = Figure() 
canvas = fc(fig) 
ax = fig.add_subplot(1, 1, 1) 
ax.plot(arange(10)) 

要在其他單元格中顯示的情節簡單地使用:

fig 
+0

感謝您的答案。這也意味着顯示屏(圖)將做到這一點。 – ntg 2017-10-13 12:46:57

相關問題