使用Matplotlib绘制比例三角形

我的三角形绘制看起来不对称,我如何使它成比例?

    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()

示例图

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注