Warning: Cannot change to a different GUI toolkit: notebook. Using qt instead
0 导入库
import matplotlib.pyplot as plt
import numpy as np
1 展示在代码下方
1.1 plt.show()
# 图标窗口(1) --> plt.show()
plt.plot(np.random.rand(5))
plt.show()
# 必须使用plt.show()生成窗口图标
1.2 %matplotlib inline
# 图标窗口2 --> %matplotlib inline
%matplotlib inline
x = np.random.rand(10)
y = np.random.rand(10)
plt.scatter(x, y)
# 直接嵌入图表,不用plt.show()
# <matplotlib.collections.PathCollection at ...> 代表该图表对象
2 展示在窗口中
2.1 %matplotlib notebook
切记:要重新开启服务才会运行成功
# 图标窗口3 --> %matplotlib notebook,在notebook里弹出可交互的Matplotlib窗口
%matplotlib notebook
x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x, y)
# 可交互的matplotlib窗口,不用plt.show()
# 可做一定调整
2.2 %matplotlib qt 或 %matplotlib qt5
切记:要重新开启服务才会运行成功
# 图表窗口4 → %matplotlib qt弹出matplotlib控制台
%matplotlib qt
x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x, y)
# 可交互性控制台
# 如果已经设置了显示方式(比如notebook),需要重启然后再运行魔法函数
# 网页嵌入的交互性窗口 和 控制台,只能显示一个
#plt.close()
# 关闭窗口
#plt.gcf().clear()
# 每次清空图表内内容
3 使用%matplotlib qt 和 %matplotlib notebook绘图的注意
1.网页嵌入的交互性窗口 和 控制台,只能显示一个
2.如果已经使用系统会提醒:
Warning: Cannot change to a different GUI toolkit: qt5. Using notebook instead.
Warning: Cannot change to a different GUI toolkit: notebook. Using qt instead
3.如出现上面的情况必须重新启动服务
4.如果继续画图则会按照%matplotlib最先引入的执行