python - 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 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()
推荐阅读
- html - 我仍然不太确定 div 标签应该做什么
- c# - 如何在 URL MVC 路由上从问号 (?) 更改为斜线 (/)?
- xamarin.forms - Xamarin.Forms 的绑定问题
- html - 如何设置网格模板行/列的属性,以便在没有足够元素的情况下保持居中?
- python - 有没有一种方法可以在不使用 for 循环的情况下制作带有序列号的字符串?
- javascript - 如何在不重新加载页面的情况下修改页面的 URL?
- xpath - 如何在 Google 表格中使用 ImportXML 导入 html-tag 的属性?
- python - 为什么在python中使用Locks进程时,方法无法运行?
- javascript - 使用内容脚本从 chrome 扩展中的当前 DOM 中删除一些元素
- php - 使用 Ajax 列出成员搜索