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中

猜你喜欢

转载自blog.csdn.net/cckavin/article/details/86626694
今日推荐