python 绘图进阶

import matplotlib.pylab as plt
import numpy as np
#处理中文乱码
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']

#三角函数
# 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()

#绘制折线
#修改坐标轴 值最大值和最小值范围
# plt.xlim(x.min()*1.5,x.max()*1.5)
# plt.ylim(c.min()*1.5,c.max()*1.5)
# plt.plot(x,c,linewidth=2.5,color='blue')
# plt.plot(x,s,c='r',lw=2.5)
# plt.show()

# x = np.linspace(-np.pi,np.pi,256,endpoint=True)
# c = np.cos(x)
# s = np.sin(x)
# plt.xlim(x.min()*1.5,x.max()*1.5)
# plt.ylim(c.min()*1.5,c.max()*1.5)
# #修改轴的显示刻度
# plt.xticks(np.linspace(-np.pi,np.pi,5))
# plt.yticks([-1,0,1])
# plt.plot(x,c,linewidth=2.5,color='blue')
# plt.plot(x,s,c='r',lw=2.5)
# plt.show()
#
# #修改轴与π相关
# x = np.linspace(-np.pi,np.pi,256,endpoint=True)
# c = np.cos(x)
# s = np.sin(x)
# plt.xlim(x.min()*1.5,x.max()*1.5)
# plt.ylim(c.min()*1.5,c.max()*1.5)
# #修改轴的显示刻度 3.14-π
# #-$\pi$ \pi 就是π
# plt.xticks(np.linspace(-np.pi,np.pi,5),[r'-$\pi$',r'-$\pi/2$','0','π/2','π'])
# plt.yticks([-1,0,1])
# plt.plot(x,c,linewidth=2.5,color='blue')
# plt.plot(x,s,c='r',lw=2.5)
# plt.show()

#将坐标轴放到中间
x = np.linspace(-np.pi,np.pi,256,endpoint=True)
c = np.cos(x)
s = np.sin(x)
plt.xlim(x.min()*1.5,x.max()*1.5)
plt.ylim(c.min()*1.5,c.max()*1.5)
#修改轴的显示刻度 3.14-π
#-$\pi$ \pi 就是π
plt.xticks(np.linspace(-np.pi,np.pi,5),[r'-$\pi$',r'-$\pi/2$','0','π/2','π'])
plt.yticks([-1,1])
plt.plot(x,c,linewidth=2.5,color='blue',label='cos')
plt.plot(x,s,c='r',lw=2.5,label='sin')
# plt.legend(loc='center left')
上一篇:发布设置setting.xml


下一篇:Python可视化库matplotlib(超详细)