不寫python的日子每天都覺得沒啥意思,重新搞起~
matplotlib中圖的註釋
直接上代碼看例子:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
#[0,5]區間,間隔0.01
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
#lw線條寬度2
ax.plot(t, s, lw=2)
"""
'local max'爲註釋的文本
箭頭指向座標系中的點(2, 1)
註釋文本位於座標系點(3, 1.5)
箭頭樣式arrowprops=dict(facecolor='black', shrink=0.05)
"""
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05))
#y座標軸(-2,2)範圍
ax.set_ylim(-2,2)
#作圖
plt.show()
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
ax.plot(t, s, lw=2)
"""
此處xycoords='data',xytext=(0.8, 0.95), textcoords='axesfraction'
是定義的註釋文本的位置方式。
(0.8, 0.95)表示,註釋文本位於座標系的橫座標的80%,縱座標的95%位置
"""
ax.annotate('local max', xy=(3, 1), xycoords='data',
xytext=(0.8, 0.95), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='right', verticalalignment='top'
)
ax.set_ylim(-2,2)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
r = np.arange(0,1,0.001)
theta = 2*2*np.pi*r
ax.plot(theta, r, color='#ee8d18', lw=3)
ind = 800
thisr, thistheta = r[ind], theta[ind]
ax.plot([thistheta], [thisr], 'o')
ax.annotate('a polar annotation',
xy=(thistheta, thisr),
xytext=(0.05, 0.05),
#註釋文本位於,相對於figure而言
textcoords='figure fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
#水平對齊方式:左對齊
horizontalalignment='left',
#垂直對齊方式:bottom對齊
verticalalignment='bottom')
plt.show()