gpt4 book ai didi

python - Matplotlib 绘制比例三角形

转载 作者:行者123 更新时间:2023-12-02 22:45:25 26 4
gpt4 key购买 nike

我的三角形绘图出现不平衡,如何使其成比例?

    points = np.array([[x, 10], [x/2, 10], [x+1/2, np.sqrt(5**2 - 2**2)]])
pivot = plt.Polygon(points, closed = True)

最佳答案

如果斜边的长度为 5,三角形的高度为 2,则宽度的一半将为 sqrt(5**2 + 2**2)。将左角放在 x,y 位置,右角将位于相同的 y 并且宽度添加到 x。中心点将在 x 加上宽度的一半和 y+height:

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('dark_background')
slanted_side = 5
height = 2
halfwidth = np.sqrt(slanted_side ** 2 - height ** 2)
y = 10

fig, axes = plt.subplots()
# x = self.target_location
x = 3
pivot_left = (x, y)
pivot_right = (x + 2 * halfwidth, y)
pivot_top = (x + halfwidth, y + height)
points = np.array([pivot_left, pivot_right, pivot_top])
pivot = plt.Polygon(points, closed=True)
axes.add_patch(pivot)
axes.set_xlim(0, 20)
axes.set_ylim(0, 20)
axes.set_aspect('equal') # equal distances in x and y directions
plt.show()

example plot

关于python - Matplotlib 绘制比例三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64639554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com