import matplotlib.pyplot as mp
1. 基本的绘图
mp.plot(水平坐标, 垂直坐标, linestyle=线型, linewidth=线宽, color=颜色, ...)
2. 设置坐标范围
mp.xlim(水平坐标最小值, 水平坐标最大值)
mp.ylim(垂直坐标最小值, 垂直坐标最大值)
3. 设置坐标刻度
mp.xticks(位置序列[, 标签序列])
mp.yticks(位置序列[, 标签序列])
4. 设置坐标轴
坐标轴名:left/right/bottom/top
ax = mp.gca() # 获取当前坐标轴
ax.spines['坐标轴名'].set_position((坐标系, 坐标值))
ax.spines['坐标轴名'].set_color(颜色)
5 .图例
mp.plot(..., label=图例标签, ...)
mp.legend([loc=位置])
6. 特殊点
mp.scatter(水平坐标数组, 垂直坐标数组, marker=点型, s=大小,color=颜色, edgecolor=边缘色, facecolor=填充色,zorder=图层序号)
7. 备注
mp.annotate(备注文本,xy=目标坐标,xycoords=目标坐标系,xytext=文本坐标,textcoords=文本坐标系,fontsize=字体大小,arrowprops=箭头线属性)
import numpy as np import matplotlib.pyplot as mp x = np.linspace(-np.pi, np.pi, 1000) print(x) 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.title("zengsf") # 设置坐标轴, 边缘空白处一般是最大或最小的百分之十 # mp.xlim(水平坐标最小值, 水平坐标最大值) mp.xlim(x.min() * 1.1, x.max() * 1.1) mp.ylim(sin_y.min() * 1.1, sin_y.max() * 1.1) # 设置坐标刻度 # mp.xticks(位置序列[, 标签序列]) mp.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-pi$', r'$-frac{pi}{2}$', r'$0$', r'$frac{pi}{2}$', r'$pi$']) mp.yticks([-1, -0.5, 0.5, 1]) # 获取当前坐标轴 ax = mp.gca() ax.spines["left"].set_position(('data', 0)) ax.spines["bottom"].set_position(('data', 0)) ax.spines["top"].set_color("none") ax.spines["right"].set_color("none") # linestyle:设置线型,linewidth:设置线宽,color:设置线的颜色, label:图例,标签 mp.plot(x, cos_y, linestyle="--", linewidth=1, color="violet", label=r'$y=frac{1}{2}cos(x)$') mp.plot(x, sin_y, linestyle="-.", linewidth=3, color="lime", label=r'$y=sin(x)$') # 将两个点连接起来 mp.plot([xo, xo], [yo_cos, yo_sin], linestyle="-", color="red") # mp.scatter(水平坐标, 垂直坐标, marker=点型, s=大小,color=颜色, edgecolor=边缘色, facecolor=填充色,zorder=图层序号) mp.scatter([xo, xo], [yo_cos, yo_sin], s=60, edgecolors="limegreen", facecolor="white", zorder=7, marker="^") # mp.annotate(备注文本,xy=目标坐标,xycoords=目标坐标系,xytext=文本坐标,textcoords=文本坐标系,fontsize=字体大小,arrowprops=箭头线属性) mp.annotate(r'$frac{1}{2}cos(frac{3pi}{4})=-frac{sqrt{2}}{4}$', xy=(xo, yo_cos), xycoords="data", xytext=(-90, -40), textcoords="offset points", fontsize=14, arrowprops=dict(arrowstyle="->", connectionstyle="arc3, rad=.2"), ) mp.annotate(r'$frac{1}{2}cos(frac{3pi}{4})=frac{sqrt{2}}{2}$', xy=(xo, yo_sin), xycoords="data", xytext=(-10, 40), textcoords="offset points", fontsize=14, arrowprops=dict(arrowstyle="->", connectionstyle="arc3, rad=.2"), ) # legend主要是控制图例的位置 mp.legend(loc="upper left") mp.show()