[英]How to display print statements interlaced with matplotlib plots inline in Ipython?
我希望打印語句的輸出與繪圖交錯,按照打印順序在Ipython筆記本單元格中繪制。 例如,請考慮以下代碼:
(用ipython notebook --no-browser --no-mathjax
啟動ipython ipython notebook --no-browser --no-mathjax
)
%matplotlib inline
import matplotlib.pyplot as plt
i = 0
for data in manydata:
fig, ax = plt.subplots()
print "data number i =", i
ax.hist(data)
i = i + 1
理想情況下,輸出看起來像:
data number i = 0
(histogram plot)
data number i = 1
(histogram plot)
...
但是,Ipython中的實際輸出將如下所示:
data number i = 0
data number i = 1
...
(histogram plot)
(histogram plot)
...
在Ipython中有直接的解決方案,還是解決方案或替代解決方案來獲得隔行輸出?
有簡單的解決方案,在繪圖后使用matplotlib.pyplot.show()函數。
這將在執行代碼的下一行之前顯示圖形
%matplotlib inline
import matplotlib.pyplot as plt
i = 0
for data in manydata:
fig, ax = plt.subplots()
print "data number i =", i
ax.hist(data)
plt.show() # this will load image to console before executing next line of code
i = i + 1
此代碼將按要求工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.