简体   繁体   中英

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

I'm considerably new to python and making a map of a room. 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 . 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. Additionally you may want to color them all the same, instead of a rainbow but I'll let you figure out the details 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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