【问题标题】:How to draw a scale invariant right angle label in matplot如何在matplot中绘制比例不变的直角标签
【发布时间】:2021-10-24 23:20:40
【问题描述】:

guide here 展示了如何制作一个比例不变的角度标签,如下所示:

没有涉及的是如何绘制90度角的标注,标注应该是直线而不是弧线。我怎样才能做到这一点?我想要这样的东西:

理想情况下,如上述指南中所述的弧形符号,它是比例不变的。

【问题讨论】:

标签: python matplotlib annotations geometry angle


【解决方案1】:

按照@Trenton McKinney 的建议,我选择了一个矩形补丁

这是我使用它的勾股定理证明:

from matplotlib.patches import Polygon

# 3/4/5 is the smallest pythagorean triple
side_a_length = 3
side_b_length = 4
hypotenuse = 5

fig, ax = plt.subplots()

triangle1 = Polygon([(0,0), (0, side_a_length), (side_b_length, 0)], ec="black", fc=[0,0,0,0])
# Label the triangle lines at the midpoints with appropriate offsets.
offset = 8
label(ax, "a", (0, side_a_length / 2), (-offset, 0))
label(ax, "b", (side_b_length / 2, 0), (0, -2 * offset))
label(ax, "c", (side_b_length / 2, side_a_length / 2), (0, offset))
ax.add_patch(triangle1)

label(ax, "b", (0, (side_a_length +side_a_length + side_b_length) /2), (-offset, 0))
label(ax, "a", (side_a_length / 2, side_a_length + side_b_length), (0, offset))
label(ax, "c", (side_a_length / 2, (side_a_length +side_a_length + side_b_length) /2), (0, -2 * offset))
label(ax, "c", (side_b_length, (side_a_length + side_b_length) /2), (-1.5 * offset, 0))

# Add lines to form other triangles.
line_width = .8
ax.plot([0, 0], [side_a_length, side_a_length + side_b_length], color="black", lw=line_width)

ax.plot([0,side_a_length], [side_a_length + side_b_length, side_a_length + side_b_length], color="black", lw=line_width)

ax.plot([side_a_length, 0], [side_a_length + side_b_length, side_a_length], color="black", lw=line_width)
ax.plot([side_a_length, side_b_length], [side_a_length + side_b_length, 0], color="black", lw=line_width)

# Right Triangles
right1 = matplotlib.patches.Rectangle((0,0),0.25,0.4, fc=[0,0,0,0], ec="black", zorder=0, lw=.7)
ax.add_patch(right1)
right2 = matplotlib.patches.Rectangle((0,side_a_length + side_b_length),0.4,0.25, fc=[0,0,0,0], ec="black", zorder=0, lw=.7, angle=-90)
ax.add_patch(right2)
right3 = matplotlib.patches.Rectangle((0, side_a_length), 0.325, 0.325, fc=[0,0,0,0], ec="black", zorder=0, lw=.7, angle=-35)
ax.add_patch(right3)

plt.ylim(-1,8)
plt.xlim(-1,8)

plt.axis('off')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-14
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多