在子图中写入文本

Sos*_*hty 4 python charts matplotlib

我正在研究这个情节:

在此处输入图片说明

我需要在第一个图中的红线和黑线之间写一些东西,我尝试过,ax1.text()但它显示了两个图之间的文本,而不是第一个图内的文本。我怎样才能做到这一点?

情节是这样设定的:

fig, (ax1,ax2) = plt.subplots(nrows=2, ncols=1, figsize = (12,7), tight_layout = True)
Run Code Online (Sandbox Code Playgroud)

预先感谢您的帮助!

Ale*_* B. 11

没有更多的代码细节,很难猜测哪里出了问题。

matplotlib.axes.Axes.text作品很好地展现文本框上的次要情节。我鼓励您查看文档(参数...)并自己尝试。

下面是一个例子:

# 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()
Run Code Online (Sandbox Code Playgroud)

输出

在此处输入图片说明