借用Pablo's helpful answer,似乎使用fig.transFigure 可以访问每个子图中的坐标,并且您可以在所有这些坐标之间画线。这可能是最好的方法,因为它可以直接确定起点和终点。由于您的 x 坐标方便地从 1 到 12,您还可以将每个子图绘制成两部分,以便在点之间留出间隙以供注释线通过。
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
y = np.array([0,1,2,3,4])
## recreate your data
df = pd.DataFrame({
'A':[0, 1, 1, 1, 2, 2, 3, 2, 3] + [float("nan")]*3,
'N':[1, 0, 0, 2, 1, 1, 2, 3, 3, 3, 3, 3],
'P':[0, 1, 1, 1, 2, 1, 1, 1, 2, 3, 3, 3],
},
index=range(1,13)
)
fig, axs = plt.subplots(3, sharex=True, sharey=True)
fig.suptitle("I1 - Reakce na změnu prvku")
## create a gap in the line
axs[0].plot(df.index[0:3],df['A'][0:3], color='lightblue', label="A", marker='.')
axs[0].plot(df.index[3:12],df['A'][3:12], color='lightblue', label="A", marker='.')
## create a gap in the line
axs[1].plot(df.index[0:8],df['N'][0:8], color='darkblue', label="N", marker='.')
axs[1].plot(df.index[8:12],df['N'][8:12], color='darkblue', label="N", marker='.')
## create a gap in the line
axs[2].plot(df.index[0:10],df['P'][0:10], color='blue', label="P", marker='.')
axs[2].plot(df.index[10:12],df['P'][10:12], color='blue', label="P", marker='.')
plt.yticks(np.arange(y.min(), y.max(), 1))
transFigure = fig.transFigure.inverted()
## Since your subplots have a ymax value of 3, setting the end y-coordinate
## of each line to just above that value should help it display outside of the figure
coord1 = transFigure.transform(axs[0].transData.transform([3.5,3]))
coord2 = transFigure.transform(axs[1].transData.transform([3.5,3.5]))
coord3 = transFigure.transform(axs[1].transData.transform([8.5,3.5]))
coord4 = transFigure.transform(axs[2].transData.transform([8.5,3.5]))
coord5 = transFigure.transform(axs[2].transData.transform([10.5,3.5]))
coord6 = transFigure.transform(axs[2].transData.transform([10.5,0]))
## add a vertical dashed line
line1 = matplotlib.lines.Line2D((coord1[0],coord2[0]),(coord1[1],coord2[1]),
transform=fig.transFigure,
ls='--',
color='grey')
## add a horizontal dashed line
line2 = matplotlib.lines.Line2D((coord2[0],coord3[0]),(coord2[1],coord3[1]),
transform=fig.transFigure,
ls='--',
color='grey')
## add a vertical dashed line
line3 = matplotlib.lines.Line2D((coord3[0],coord4[0]),(coord3[1],coord4[1]),
transform=fig.transFigure,
ls='--',
color='grey')
## add a horizontal dashed line
line4 = matplotlib.lines.Line2D((coord4[0],coord5[0]),(coord4[1],coord5[1]),
transform=fig.transFigure,
ls='--',
color='grey')
## add a vertical dashed line
line5 = matplotlib.lines.Line2D((coord5[0],coord6[0]),(coord5[1],coord6[1]),
transform=fig.transFigure,
ls='--',
color='grey')
fig.lines.extend([line1, line2, line3, line4, line5])
plt.show()