简体   繁体   English

如何在python(matplotlib)中绘制二维锥体?

[英]How to plot a 2-D cone in python (matplotlib)?

I'm considerably new to python and making a map of a room. 我对python很新,并制作了一个房间的地图。 I've plotted the room, obstacles etc. And some points (which are sensors). 我绘制了房间,障碍物和一些点(传感器)。 Now I want make a 2-D cone which shows the area in which sensors see. 现在我想制作一个二维锥体,显示传感器看到的区域。 I will have an angle and radius for the cone. 我将为锥体设置一个角度和半径。 I've tried searching but mostly 3-D cones have been discussed here in previous questions. 我已经尝试过搜索,但在之前的问题中已经讨论了大多数三维锥体。 How the cone should look 锥体应该如何看

Any guidance is appreciated 任何指导表示赞赏

You would use matplotlib.patches.Wedge such as this example . 您可以使用matplotlib.patches.Wedge,例如此示例 Another example that I've reduced to the more relevant bits is: 我已经减少到更相关位的另一个例子是:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
import numpy as np

fig, ax = plt.subplots()

patches = []

wedge = mpatches.Wedge((.5, .5), 0.5, 30, 270, ec="none")
patches.append(wedge)

colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)
collection.set_array(np.array(colors))
ax.add_collection(collection)

plt.show()

Which produces something like: 产生的东西如下:

楔形图

Obviously you will need to tweak the theta1 and theta2 from their 30 and 270 to fit whatever angle you are trying to represent, and move the origin to wherever the sensors are located. 显然,您需要从30270调整theta1theta2 ,以适合您尝试表示的任何角度,并将原点移动到传感器所在的位置。 Additionally you may want to color them all the same, instead of a rainbow but I'll let you figure out the details XD 另外你可能想要将它们全部着色,而不是彩虹,但我会让你弄清楚细节XD

Ended up using the following: 结束使用以下内容:

import matplotlib.pyplot as plt
from matplotlib.patches import Wedge

fig, ax = plt.subplots()
patches=[]
ax.axis('equal')
we = Wedge((2756.6747,5339751.8148),10,30,180,edgecolor='b',facecolor='none')
patches.append(we)

ax.add_artist(we)
ax.set(xlim=[2740, 2800], ylim=[5339740, 5339780])

plt.show()

thanks to the direction given by @reedinationer 感谢@reedinationer给出的指示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM