如何解决Matplotlib绘制比例三角形
我的三角形绘图看起来偏斜,我如何使它成比例?
points = np.array([[x,10],[x/2,[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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。