Matplotlib的基本用法

Matplotlib是基于NumPy数组构建的多平台数据可视化库。

一.相关链接

1.官方文档:https://matplotlib.org/

2.Github: https://github.com/matplotlib/matplotlib

3.PyPi: https://pypi.org/project/matplotlib/

4.中文文档:https://www.matplotlib.org.cn/index.html

二.pip安装

pip install matplotlib

三.验证安装

通过绘制一个sin函数进行验证。

1.从.py脚本中绘图

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.savefig('20190124sin.png') # 保存图片
plt.show() # 显示图片

 运行结果如下:

2.从IPython shell中绘图

在命令窗口中如果不想每次都调用show()方法,那么可以%matplotlib魔法命令

C:\Users\95232>ipython
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import matplotlib as mpl

In [2]: import matplotlib.pyplot as plt

In [3]: import numpy as np

In [4]: %matplotlib
Using matplotlib backend: Qt5Agg

In [5]: x = np.linspace(0, 10, 100)

In [6]: plt.plot(x, np.sin(x))
Out[6]: [<matplotlib.lines.Line2D at 0x1f7aff01d30>]

In [7]:

3.从Jupyter Notebook中绘图 

(1)方式一:使用%matplotlib魔法命令

(2)方法二:将图形嵌入到notebook中,需要使用如下命令之一:

%matplotlib notebook # 会把交互式的绘图嵌入到jupyter notebook中
%matplotlib inline  # 会把绘图的静态图像嵌入到jupyter notebook中

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值