小编Jam*_*mes的帖子

如何绘制带有箭头的轴线?

我想在axvline以箭头结尾的特定数据坐标处添加到绘图中(并选择终止于绘图顶部的向上箭头或终止于绘图底部的向下箭头, 或两者)。

例如

fig, ax = plt.subplots()
ax.plot([1,2,3])
ax.axvline(1.5, arrow_head=[True,False])
Run Code Online (Sandbox Code Playgroud)

将给出一条底部有箭头但顶部没有箭头的垂直线(即向下的箭头)。

我没有找到直接在 中执行此操作的方法,但我可以通过混合变换(x 的数据坐标和 y 的轴坐标)axvline获得大部分方法:ax.arrow()

import matplotlib.pyplot as plt
import matplotlib.transforms as transforms

fig, ax = plt.subplots()
ax.plot([1,2,3],[1,4,9])

mytrans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
ax.arrow(2.5,0,0,1,transform=mytrans,head_width=0.1,length_includes_head=True)
Run Code Online (Sandbox Code Playgroud)

axvline-as-arrow 的输出图像

我的问题:
1)有没有更简单/更干净的方法来做到这一点?
2)如何用这种方式制作双头箭头?

python matplotlib

5
推荐指数
1
解决办法
2548
查看次数

标签 统计

matplotlib ×1

python ×1