首页 > 解决方案 > Matplotlib 绘制比例三角形

问题描述

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

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

标签: pythonmatplotlibreinforcement-learningopenai-gymopenai

解决方案


如果斜边是 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()

示例图


推荐阅读