如何在 Matplotlib 中在轴外画一条线?

matplotlibpythondata visualization

要在轴外画一条线(即箭头),我们可以使用 annotate() 方法,

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用 figure() 方法创建新图形或激活现有图形。

  • 清除当前图形。

  • 使用 add_subplot() 方法将 '~.axes.Axes' 作为子图排列的一部分添加到图形中。

  • 使用 annotate() 方法在轴外放置一条线轴。

  • 要显示图形,请使用 show() 方法。

示例

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure(1)
fig.clf()

ax = fig.add_subplot(1, 1, 1)
ax.annotate('', xy=(0, -0.1), xycoords='axes fraction', xytext=(1, -0.1),
arrowprops=dict(arrowstyle="<->", color='b'))
plt.show()

输出


相关文章