使用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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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