三、数据可视化:matplotlib.pyplot (mp)
1、基本绘图函数
mp.plot(水平坐标数组, 垂直坐标数组)
x: [1 2 3 4]
y: [5 6 7 8]
代码:plt1.py
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.plot(x, cos_y)
mp.plot(x, sin_y)
mp.show()
mp.plot(..., linestyle=线型, linewidth=线宽(默认为2),
color=颜色)
代码:plt2.py
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.plot(x, cos_y, linestyle='--', linewidth=6, color='dodgerblue')
mp.plot(x, sin_y, linestyle=':', linewidth=3, color='orangered')
mp.show()
mp.xlim(左边界, 右边界)
mp.ylim(底边界, 顶边界)
代码:plt3.py
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.xlim(x.min(), x.max()) # 左右边界
mp.ylim(sin_y.min(), sin_y.max()) # 上下边界
mp.plot(x, cos_y, linestyle='--', linewidth=6, color='dodgerblue')
mp.plot(x, sin_y, linestyle=':', linewidth=3, color='orangered')
mp.show()
mp.xticks(刻度位置数组, 刻度文本数组) 水平方向
mp.yticks(刻度位置数组, 刻度文本数组) 垂直方向
代码:plt4.py
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.xlim(x.min() * 1.1, x.max() * 1.1)
mp.ylim(sin_y.min() * 1.1, sin_y.max() * 1.1)
mp.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4,
np.pi],
[r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
mp.plot(x, cos_y, linestyle='-', linewidth=2,
color='dodgerblue')
mp.plot(x, sin_y, linestyle='-', linewidth=2,
color='orangered')
mp.show()
ax = mp.gca() # 获取当前坐标轴
ax.spines['left'].set_position(('data', 0)) 设置坐标轴的位置
ax.spines['left'].set_color(颜色)
代码:plt5.py
mp.plot(…, label=图例文本)
mp.legend(loc=‘upper left’)
代码:plt6.py
mp.scatter(水平坐标数组, 垂直坐标数组,
marker=点型, s=大小, edgecolor=勾边色,
facecolor=填充色, zorder=Z序)
代码:plt7.py
mp.annotate(
备注文本,
xy=目标位置,
xycoords=目标坐标系,
xytext=文本位置, # 常用偏移位置
textcoords=文本坐标系, # 常用偏移坐标系 offset points
fontsize=字体大小,
arrowprops=箭头属性)
代码:plt8.py
![import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
xo = np.pi * 3 / 4
yo_cos = np.cos(xo) / 2
yo_sin = np.sin(xo)
mp.xlim(x.min() * 1.1, x.max() * 1.1)
mp.ylim(sin_y.min() * 1.1, sin_y.max() * 1.1)
# 设置坐标刻度
mp.xticks(\[-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4,
np.pi\],
\[r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
r'$\pi$'\])
mp.yticks(\[-1, -0.5, 0.5, 1\])
# 设置坐标系, 变为十字坐标系
ax = mp.gca()
ax.spines\['left'\].set_position(('data', 0)) # 将坐标轴的左边框移动到0
ax.spines\['bottom'\].set_position(('data', 0)) # 将坐标轴的上边框移动的0
ax.spines\['right'\].set_color('None') # 将坐标轴的右边框颜色设置为无
ax.spines\['top'\].set_color('None') # 将坐标轴的上边框颜色设置为无
# 设置图例
mp.plot(x, cos_y, linestyle='-', linewidth=2,
color='dodgerblue', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle='-', linewidth=2,
color='orangered', label=r'$y=sin(x)$')
mp.legend(loc='upper left')
# scatter
mp.scatter(\[xo, xo\], \[yo_cos, yo_sin\], s=60,
edgecolor='limegreen', facecolor='white',
zorder=3)
mp.plot(\[xo, xo\], \[yo_cos, yo_sin\], linestyle='--',
linewidth=1, color='limegreen')
# 对两个点添加注释
mp.annotate(
r'$\frac{1}{2}cos(\frac{3\pi}{4})=-\frac{\sqrt{2}}{4}$', # frac{}{}为分数 sqrt{}为根号
xy=(xo, yo_cos), xycoords='data',
xytext=(-90, -40), textcoords='offset points',
fontsize=14,
arrowprops=dict(arrowstyle='->',
connectionstyle='arc3, rad=.2'))
mp.annotate(
r'$sin(\frac{3\pi}{4})=\frac{\sqrt{2}}{2}$',
xy=(xo, yo_sin), xycoords='data',
xytext=(20, 20), textcoords='offset points',
fontsize=14,
arrowprops=dict(arrowstyle='->',
connectionstyle='arc3, rad=.2'))
mp.show()