-
- 1.1创建三角函数及重构
- 1.2 重构1 线条
- 1.3 坐标轴重构
- 1.4 坐标刻度重构
- 1.5 坐标刻度重构2
- 1.6坐标轴放到中间
- 1.7图例区分正余弦
- 1.8特殊点的注释
- 1.9完整代码
1.1创建三角函数及重构
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-np.pi,np.pi,256,endpoint=True)
C = np.cos(x)
S = np.sin(x)
plt.plot(x,C)
plt.plot(x,S)
plt.show()
1.2 重构1 线条
#线宽加颜色的修改
##### 'b-':表示color='blue',linestyle="-"
plt.plot(x,C,"b-",lw=2.5) #lw是线宽
plt.plot(x,S,"r-",lw=2.5)
plt.show()
1.3 坐标轴重构
plt.xlim(x.min()*1.5,x.max()*1.5)
plt.ylim(C.min()*1.5,C.max()*1.5)
1.4 坐标刻度重构
plt.xticks([-np.pi,np.pi/2,0,np.pi/2,np.pi])
plt.yticks([-1,0,1])
1.5 坐标刻度重构2
plt.xticks([-np.pi,np.pi/2,0,np.pi/2,np.pi],
[r'$-\pi$',r'$-\pi/2$'r'$\pi/2$'r'$\pi$'])
plt.yticks([-1,0,1])
1.6坐标轴放到中间
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
1.7图例区分正余弦
#########加图例
plt.polt(x,C,"b-",lw=2.5,label='cosine')
plt.polt(x,S,"r-",lw=2.5,label='sine')
plt.legend(loc="upper left")
1.8特殊点的注释
t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)],color='blue',linewidth=2.5,linestyle='--')
plt.scatter([t,],[np.cos(t,)],50,color='blue')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t,np.cos(t)),xycoords='data',
xytext=(-90,-50),textcoords='offset points',fontsize=16,
arrowprops=dict(arrowstyle="=>",connectionstyle="arc3,rad=.2"))
plt.plot([t,t],[0,np.sin(t)],color='red',linewidth=2.5,linestyle='--')
plt.scatter([t,],[np.sin(t,)],50,color='blue')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t,np.sin(t)),xycoords='data',
xytext=(-90,-50),textcoords='offset points',fontsize=16,
arrowprops=dict(arrowstyle="=>",connectionstyle="arc3,rad=.2"))
1.9完整代码
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"]=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x = np.linspace(-np.pi,np.pi,256,endpoint=True)
sin,cos = np.sin(x),np.cos(x)
plt.plot(x,sin,"g-",lw=2.5,label="正弦")
plt.plot(x,cos,"y-",lw=2.5,label="正弦")
plt.xlim(x.min()*1.5,x.max()*1.5)
plt.ylim(sin.min()*1.5,cos.max()*1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r"$-\pi$",r"$-\pi/2$",r"$0$",r"$\pi/2$",r"$\pi$"])
plt.yticks([-1,0,1])
ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(("data",0))
ax.spines["left"].set_position(("data",0))
plt.legend()
t=2*np.pi/3
plt.plot([t,t],[0,np.cos(t)],
color ='blue',
linewidth=2.5,
linestyle="--")
plt.scatter([t,],[np.cos(t),], 20, color ='blue')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)),
xycoords='data',
xytext=(-50, -50),
textcoords='offset points',
fontsize=10,
arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))
t=2*np.pi/3
plt.plot([t,t],[0,np.sin(t)],
color ='red',
linewidth=2.5,
linestyle="--")
plt.scatter([t,],[np.sin(t),], 20, color ='red')
plt.annotate(r'$\sin(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.sin(t)),
xycoords='data',
xytext=(50, 50),
textcoords='offset points',
fontsize=10,
arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='w',edgecolor='b',alpha=0.5))
plt.show()