我的三角形绘制看起来不对称,我如何使它成比例?
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 pltimport numpy as npplt.style.use('dark_background')slanted_side = 5height = 2halfwidth = np.sqrt(slanted_side ** 2 - height ** 2)y = 10fig, axes = plt.subplots()# x = self.target_locationx = 3pivot_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') # x和y方向的距离相等plt.show()