在本章中,我们将学习如何使用matplotlib创建一个简单图。假设要在matplotlib中显示一个简单的角度线图,以弧度为单位,与正弦值相对应。首先,导入matplotlib包中的pyplot模块,并使用别名为plt。
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 import matplotlib.pyplot as plt
接下来需要一组数字来绘制。numpy库中定义了各种数组函数,这些函数是使用np别名导入的。
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 import numpy as np
使用numpy库中的arange()函数获得0到2π之间角度的ndarray对象。
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 x = np.arange(0, math.pi*2, 0.05)
ndarray对象用作图的x轴上的值。通过以下语句获得在y轴上显示的x中的角度的相应正弦值 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 y = np.sin(x)
使用plot()函数绘制两个数组的值。
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 plt.plot(x,y)
可以设置绘图标题以及x和y轴的标签。
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 plt.xlabel("angle") plt.ylabel("sine") plt.title('sine wave')
show()函数调用绘图查看器窗口 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 plt.show()
完整的程序代码如下 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math #needed for definition of pi # 显示中文设置... plt.rcparams['font.sans-serif'] = ['simhei'] # 步骤一(替换sans-serif字体) plt.rcparams['axes.unicode_minus'] = false # 步骤二(解决坐标轴负数的负号显示问题) x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) plt.plot(x,y) plt.xlabel(u"角度") plt.ylabel("正弦") plt.title('正弦波') plt.show()
执行上面上面示例代码,得到以下结果 -
接下来在jupyter notebook环境中运行上面代码。
如前所述,从anaconda导航器或命令行启动jupyter notebook。在输入单元格中,输入import语句导入pyplot和numpy -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 from matplotlib import pyplot as plt import numpy as np
要在jupyter notebook内部显示绘图输出(不是在单独的查看器中显示),请输入以下语句 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 %matplotlib inline
完整的代码如下所示 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math #needed for definition of pi %matplotlib inline # 显示中文设置... plt.rcparams['font.sans-serif'] = ['simhei'] # 步骤一(替换sans-serif字体) plt.rcparams['axes.unicode_minus'] = false # 步骤二(解决坐标轴负数的负号显示问题) x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) plt.plot(x,y) plt.xlabel("角度") plt.ylabel("正弦") plt.title('正弦波') plt.show()
在jupyter notebook内执行上面示例代码,得到以下结果 -