1 回答

TA貢獻1843條經驗 獲得超7個贊
沒有更多的代碼細節,很難猜出哪里出了問題。
matplotlib.axes.Axes.text
可以很好地在子圖上顯示文本框。我鼓勵您查看文檔(參數...)并自己嘗試。
文本位置基于以下 2 個參數:
transform=ax.transAxes
: 表示坐標是相對于坐標軸邊界框給出的,坐標軸(0, 0)
的左下角和(1, 1)
右上角。text(x, y,...)
: wherex
,y
是放置文本的位置。可以使用以下參數更改坐標系transform
。
這是一個例子:
# import modules
import matplotlib.pyplot as plt
import numpy as np
# Create random data
x = np.arange(0,20)
y1 = np.random.randint(0,10, 20)
y2 = np.random.randint(0,10, 20) + 15
# Create figure
fig, (ax1,ax2) = plt.subplots(nrows=2, ncols=1, figsize = (12,7), tight_layout = True)
# Add subplots
ax1.plot(x, y1)
ax1.plot(x, y2)
ax2.plot(x, y1)
ax2.plot(x, y2)
# Show texts
ax1.text(0.1, 0.5, 'Begin text', horizontalalignment='center', verticalalignment='center', transform=ax1.transAxes)
ax2.text(0.9, 0.5, 'End text', horizontalalignment='center', verticalalignment='center', transform=ax2.transAxes)
plt.show()
輸出
添加回答
舉報