2018年1月24日 星期三

在 2π/3 2π/3 的位置給兩條函數曲線加上一個注釋。

我們希望在 2π/3  2π/3 的位置給兩條函數曲線加上一個注釋。首先,我們在對應的函數圖像位置上畫一個點;然後,向橫軸引一條垂線,以虛線標記;最後,寫上標籤。


# 導入 matplotlib 的所有內容(nympy 可以用 np 這個名字來使用)
from pylab import *

# 創建一個 8 * 6 點(point)的圖,並設置解析度為 80
figure(figsize=(8,6), dpi=80)

# 創建一個新的 1 * 1 的子圖,接下來的圖樣繪製在其中的第 1 塊(也是唯一的一塊)
subplot(1,1,1)

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)


ax = 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))


t = 2*np.pi/3
plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--")
scatter([t,],[np.cos(t),], 50, color ='blue')

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"))

plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle="--")
scatter([t,],[np.sin(t),], 50, color ='red')

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"))


# 繪製余弦曲線,使用藍色的、連續的、寬度為 1 (圖元)的線條
plot(X, C, color="blue", linewidth=1.0, linestyle="-")

# 繪製正弦曲線,使用綠色的、連續的、寬度為 1 (圖元)的線條
plot(X, S, color="green", linewidth=1.0, linestyle="-")

# 設置橫軸的上下限
xlim(-4.0,4.0)

# 設置橫軸記號
xticks(np.linspace(-4,4,9,endpoint=True))

# 設置縱軸的上下限
ylim(-1.0,1.0)

# 設置縱軸記號
yticks(np.linspace(-1,1,5,endpoint=True))

# 以解析度 72 來保存圖片
# savefig("exercice_2.png",dpi=72)

# 在螢幕上顯示
show()


沒有留言:

張貼留言

MQTT Explorer 與 Node-Red 介面的實驗

 MQTT Explorer 與 Node-Red 介面的實驗 MQTT EXplorer 與 Node-Red介面的設定 (1) 設定  MQTT EXplorer Client    (2)        Node-Red LED ON  --> MQTT Explor...