Matplotlib四种绘图窗口!还可以进行简单交互哦~

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最先引入的执行

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值