matplotlib.pyplot Jupyter notebook图像 解决matplotlib.pyplot在Jupyter notebook中不显示图像问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

matplotlib.pyplot Jupyter notebook图像 解决matplotlib.pyplot在Jupyter notebook中不显示图像问题

骑蜗牛前行   2021-04-22 我要评论

在代码首行添加:

%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中不显示图像问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们