1. 程式人生 > 程式設計 >解決matplotlib.pyplot在Jupyter notebook中不顯示影象問題

解決matplotlib.pyplot在Jupyter notebook中不顯示影象問題

在程式碼首行新增:

%matplotlib inline

即可。

補充知識:jupyter不能顯示Matplotlib 動畫

看莫煩老師的matplotlib教程中,有一段sinx函式動畫,用Jupyter跑卻不能顯示動畫效果。

解決方案:在前面加一句%matplotlib notebook

動畫程式碼如下:

%matplotlib notebook
import tensorflow as tf 
import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import animation
fig,ax = plt.subplots()
x =np.arange(0,2*np.pi,0.01)
# 返回的是個列表
line,= ax.plot(x,np.sin(x))
def animate(i):
  # xdata 保持不變, ydata 更新成另外一批資料
  # 將0-100都傳進去更新一下,i變化時,y也會變化,更新影象
  line.set_ydata(np.sin(x+i/10))
  return line,def init():
  line.set_ydata(np.sin(x))
  return line,# interval 是更新的頻率,隔多少毫秒更新一次,這裡是隔20ms更新一次
# blit=True,只更新有變化的點
ani = animation.FuncAnimation(fig=fig,func=animate,frames =100,init_func=init,interval =20,blit=False)
plt.show()

以上這篇解決matplotlib.pyplot在Jupyter notebook中不顯示影象問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。