1.plot() 绘制曲线图
导入所需模块
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 200)
y = np.sin(x)
'''将x,y的值绘制成图像,
c='r'线条颜色为红色,
ls='-'线条风格为虚线,
lw=2线条宽度为2,
label='y=sinx' 显示线条信息
'''
plt.plot(x, y, c='r', ls='-', lw=2, label='y=sinx')
plt.legend()
plt.show()
2.scatter() 绘制散点图
导入所需模块
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 1000)
y = np.random.randn(1000)
plt.scatter(x, y, c='r', s=0.5, label='scatter figure')
plt.legend()
plt.show()
3.坐标轴的范围与名称
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(1, 10, 200)
y = np.sin(x)
plt.titile('y=sinx')
plt.xlim(2, 5)
plt.ylim(-2, 2)
plt.xlabel('x的值')
plt.ylabel('sinx的值')
plt.plot(x, y)
plt.show()
4.在显示的图像画参考线
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 200)
y = np.sin(x)
plt.plot(x, y)
plt.grid(ls=':', c='r')
plt.axhline(y=0.5, c='y', ls='--', lw=2)
plt.axvline(x=2, c='y', ls='--', lw=2)
plt.axhspan(ymin=-0.25, ymax=0.25, facecolor='g', alpha=0.3)
plt.axvspan(xmin=0, xmax=1, facecolor='g', alpha=0.3)
plt.show()
5.添加图形内容细节的指向型注释文本
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 500)
y = np.sin(x)
plt.plot(x, y)
plt.grid(ls=':')
plt.axvline(x=np.pi / 2, ls=':', c='r')
plt.axhline(y=1, ls=':', c='r')
plt.axvline(x=np.pi * 3 / 2, ls=':', c='g')
plt.axhline(y=-1, ls=':', c='g')
plt.annotate('maximum',
xy=((np.pi / 2), 1),
xytext=((np.pi / 2) + 1, 0.8),
weight="bold",
color="b",
arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b")
)
plt.annotate('minimum',
xy=((np.pi * 3 / 2), -1),
xytext=((np.pi * 3 / 2) + 1, -0.8),
weight="bold",
color="b",
arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b")
)
plt.show()
6.添加图形内容细节的无指向型注释文本
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 200)
y = np.sin(x)
plt.plot(x, y)
plt.grid(ls=':')
plt.text(3, 0.5, 'y=sinx', c='b')
plt.show()