注意
移至結尾下載完整範例程式碼。
註解轉換#
此範例示範如何為註解使用不同的座標系統。如需註解功能的完整概述,另請參閱註解教學。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.005)
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
xdata, ydata = 5, 0
xdisplay, ydisplay = ax.transData.transform((xdata, ydata))
bbox = dict(boxstyle="round", fc="0.8")
arrowprops = dict(
arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=90,rad=10")
offset = 72
ax.annotate(
f'data = ({xdata:.1f}, {ydata:.1f})',
(xdata, ydata),
xytext=(-2*offset, offset), textcoords='offset points',
bbox=bbox, arrowprops=arrowprops)
ax.annotate(
f'display = ({xdisplay:.1f}, {ydisplay:.1f})',
xy=(xdisplay, ydisplay), xycoords='figure pixels',
xytext=(0.5*offset, -offset), textcoords='offset points',
bbox=bbox, arrowprops=arrowprops)
plt.show()

參考資料
本範例中顯示了下列函數、方法、類別和模組的使用