Matplotlib画正弦余弦曲线

 

参考1:http://www.labri.fr/perso/nrougier/teaching/matplotlib/

参考2:https://matplotlib.org/api/artist_api.html

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 256,endpoint=True)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.figure(figsize=(10,5))
plt.xlabel('Time(s)')
plt.ylabel('Value')
plt.title('Sin and Cos')
plt.plot(x, y_sin, label="$sin(x)$", color="blue")
plt.plot(x, y_cos, label="$cos(x)$", color="red")
plt.xlim(-4,4)
plt.xticks(np.linspace(-4,4,9,endpoint=True))
# plt.ylim(-1.2,1.2)
# plt.yticks(np.linspace(-1,1,9,endpoint=True))
plt.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
plt.legend()
plt.show()

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 256,endpoint=True)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.figure(figsize=(10,5))

# 移动边界线,构建坐标系,原点为0
ax = plt.gca()  #获取当前轴线实例
ax.xaxis.set_ticks_position('bottom')  #x轴线,使用spine中的bottom线
ax.yaxis.set_ticks_position('left')  #y轴线,使用spine中的left线
ax.spines['bottom'].set_position(('data',0))  #将bottom线的位置设置为数据为0的位置
ax.spines['left'].set_position(('data',0))  #将left线的位置设置为数据为0的位置
ax.spines['top'].set_color('none')  #将top线的颜色设置为无
ax.spines['right'].set_color('none')  #将right线的颜色设置为无

# plt.xlabel('Time(s)')
# plt.ylabel('Value')
plt.title('Sin and Cos')
plt.plot(x, y_sin, label="$sin(x)$", color="blue")
plt.plot(x, y_cos, label="$cos(x)$", color="red")
plt.xlim(-4,4)  # 设置x轴的范围
# plt.xticks(np.linspace(-4,4,9,endpoint=True))
# 设置x轴的标尺刻度,从-pi到pi,取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.ylim(-1.2,1.2)
# plt.yticks(np.linspace(-1,1,5,endpoint=True))
plt.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
plt.legend()
plt.show()

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 256,endpoint=True)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.figure(figsize=(10,5),dpi=80)

# 移动边界线,构建坐标系,原点为0
ax = plt.gca()  #获取当前轴线实例
ax.xaxis.set_ticks_position('bottom')  #x轴线,使用spine中的bottom线
ax.yaxis.set_ticks_position('left')  #y轴线,使用spine中的left线
ax.spines['bottom'].set_position(('data',0))  #将bottom线的位置设置为数据为0的位置
ax.spines['left'].set_position(('data',0))  #将left线的位置设置为数据为0的位置
ax.spines['top'].set_color('none')  #将top线的颜色设置为无
ax.spines['right'].set_color('none')  #将right线的颜色设置为无

# plt.xlabel('Time(s)')
# plt.ylabel('Value')
plt.title('Sin and Cos')
plt.plot(x, y_sin, label="$sin(x)$", color="blue", linestyle="-")
plt.plot(x, y_cos, label="$cos(x)$", color="red", linestyle="-")
plt.xlim(-4,4)  # 设置x轴的范围
# plt.xticks(np.linspace(-4,4,9,endpoint=True))
# 设置x轴的标尺刻度,从-pi到pi,取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.ylim(-1.2,1.2)
plt.yticks(np.linspace(-1,1,5,endpoint=True))

t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)], color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='red')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
             xy=(t, np.sin(t)), xycoords='data',
             xytext=(+10, +30), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plt.plot([t,t],[0,np.sin(t)], color ='blue', linewidth=1.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.cos(t)), xycoords='data',
             xytext=(-90, -50), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

# plt.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
plt.legend()
plt.show()

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 256,endpoint=True)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.figure(figsize=(10,5),dpi=80)

# 移动边界线,构建坐标系,原点为0
ax = plt.gca()  #获取当前轴线实例
ax.xaxis.set_ticks_position('bottom')  #x轴线,使用spine中的bottom线
ax.yaxis.set_ticks_position('left')  #y轴线,使用spine中的left线
ax.spines['bottom'].set_position(('data',0))  #将bottom线的位置设置为数据为0的位置
ax.spines['left'].set_position(('data',0))  #将left线的位置设置为数据为0的位置
ax.spines['top'].set_color('none')  #将top线的颜色设置为无
ax.spines['right'].set_color('none')  #将right线的颜色设置为无

# plt.xlabel('Time(s)')
# plt.ylabel('Value')
plt.title('Sine and Cosine')
# zorder 控制划线顺序:数值越小越先画
plt.plot(x, y_sin, label="$sin(x)$", color="blue", linewidth=2.5, linestyle="-", zorder=-2)
plt.plot(x, y_cos, label="$cos(x)$", color="red", linewidth=2.5, linestyle="-", zorder=-1)
plt.xlim(-4,4)  # 设置x轴的范围
# plt.xticks(np.linspace(-4,4,9,endpoint=True))
# 设置x轴的标尺刻度,从-pi到pi,取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.ylim(-1.2,1.2)
plt.yticks(np.linspace(-1,1,5,endpoint=True))

t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)], color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='red')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
             xy=(t, np.sin(t)), xycoords='data',
             xytext=(+10, +30), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plt.plot([t,t],[0,np.sin(t)], color ='blue', linewidth=1.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.cos(t)), xycoords='data',
             xytext=(-90, -50), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

# 对坐标轴上的标度单独做标注
for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(14)
    label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65 ))

# plt.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
plt.legend()
plt.show()

 

 

 

 

 

 

 

 

  

 

posted @ 2019-03-22 20:30  lixin[at]hitwh  阅读(1394)  评论(0编辑  收藏  举报