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

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

Related

How to plot a numpy array over a pcolor image in matplotlib?

I would like to plot a matrix as image and a vector as a line in this image.
something like that:
I manage to do the code for the matrix image, but I'm not able to make the black line (here I did just
an example in powerpoint).
this is my code so far:
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import LogLocator
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
rcParams['font.size']=35
x = np.arange(1,16,1)
y = np.arange(-50,0,1)
z = 100 * np.random.random_sample((15, 50))
line = np.linspace(0,100,50)
fig, ax = plt.subplots(figsize=(25,25))
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.1)
im = ax.pcolor(x,y,z.T,norm=LogNorm(0.1, 100),cmap= 'jet')
cbar = fig.colorbar(im,cax=cax, orientation='vertical')
cbar.ax.yaxis.set_major_locator(LogLocator()) # <- Why? See above.
cbar.ax.set_ylabel('Resistividade \u03C1 [ohm.m]', rotation=270)
#ax2=ax.twinx()
#ax2.plot(line,y,'k--',linewidth=10)
ax.set_xlabel('Aquisição')
ax.set_ylabel('Profundidade [m]')
plt.savefig('mrec_1'+'.png',bbox_inches = "tight", format='png', dpi=300)
plt.show()
I have tried to use the ax.twinx() but since the order of magnetude is different the values on x-axis doesn' match.
Would someone help me please?
I tried this but it depends on the kind of data you have for the black line "track". I think you can just make an array of different (x,y) coordinates for the vertices (where the line changes direction + point of origin and point of end). Basing on what I saw in the image, you can add these two lines (is an approximation)
coordinates=np.array([[7,0],[7,-5],[9,-5],[9,-13], [7,-13],[7,-23],[13,-23],[13,-60]])
ax.plot(coordinates[:,0], coordinates[:,1], c='k', linewidth=10)
where every element of coordinates is the [x,y] couple. But if you have x and y from a well log, for example, you can just use those as arrays instead of coordinates[:,0] and coordinates[:,1]
As #BlueScr33n mentioned I used the twiny and works fine.

plt.Circle() has strange behavior when used with an axes with projection='3d'

I'm sorry if this is expected behavior, but I wanted to understand what was going on here better. So if I try to draw a circle with plt.Circle and projection='3d'
import matplotlib.pyplot as plt
f2 = plt.figure()
ax1 = f2.add_subplot(1,1,1,projection='3d')
x = plt.Circle([1,1],radius=10)
ax1.add_artist(x)
ax1.set_xlim(0,100)
ax1.set_ylim(0,100)
ax1.set_zlim(0,100)
plt.show()
then I get the following bizarre outcome:
although it all works as expected if I just remove the projection='3d'. I would appreciate any context as to why this weird result happens, I guess I don't totally understand why projection='3d' would mangle things so much.
You need somewhere a projection from 2d to 3d to accommodate your circular artist. One way is to create a circle patch and map it to 3d as shown in here, which I have adapted to answer your question. You can choose zdir='x' to be zdir='y' or zdir='z' as per your need.
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, PathPatch
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.art3d as art3d
f2 = plt.figure()
ax1 = f2.add_subplot(1,1,1,projection='3d')
circle = Circle((1, 1), 10, edgecolor='red', facecolor=None, fill=False)
ax1.add_patch(circle)
art3d.pathpatch_2d_to_3d(circle, z=0, zdir='x')
ax1.set_xlim3d(-20, 20)
ax1.set_ylim3d(-20, 20)
ax1.set_zlim3d(-20, 20)

python : matplotlib.Basemap.scatter does not show all points on map

I'm actually trying to plot points on a map. Each points is associated to a value beetween 0 and 1000 in sumlist. I just want to color the points with a proportional red component as you can see on this reproducible exemple :
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='kav7',lon_0=0,lat_0=0,resolution='l')
m.drawcoastlines()
m.fillcontinents(color='#ddaa66',lake_color='#7777ff')
m.drawmeridians(np.arange(0.,420.,60.))
m.drawparallels(np.arange(-90.,120.,30.))
m.drawmapboundary(fill_color='#7777ff')
lon_prox=[-36.4,-38.5,-77,-71,-19.8,-69.5,-10.09,-4.93,9,-91.3,-85.92,-114.2,-109.3,103,-110.1,-42.6,1.16,-20.1,-23.2,-18.2,-17.95,-17.7,-10.1,-5.87]
lat_prox=[76.6,72.6,80.7,69.87,64.6,81.33,30.845,58.14,46,35.15,30.45,38.9,37.5,71.29,40,71.7,40.34,57.44,56.1,66.54,66.53,66.55,30.85,56.67]
x,y=m(lon_prox,lat_prox)
sumlist=[276,554,39,0,2,1,107,14,10,32,0,992,933,17,897,39,0,0,0,0,0,24,111,0]
for i in range(len(x)):
col='#%02x%02x%02x' % (int((255*sumlist[i])/1000),0,0)
m.scatter(x[i],y[i],color=col,zorder=10)
plt.show()
The problem is that all the points are not shown on the map. I'm thinking that the zorderparameter could helps me because when I set it to 10 (I put nothing before) some points that I had not appeared. But I don't really understand what does zorder. I know that the problem should not be really difficult to solve but I spent two hours on it.
Thanks in advance,
Smich.
Scatter() takes x, y, c lists or arrays as parameters. Try this code:
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
#import numpy as np
# defind figure dimension
fig = plt.figure(figsize=(12, 8))
m = Basemap(projection='kav7',lon_0=0,lat_0=0,resolution='l')
m.drawcoastlines(color='gray')
m.fillcontinents(color='#ddaa66',lake_color='#7777ff')
m.drawmeridians(range(0,420,60))
m.drawparallels(range(-90,120,30))
m.drawmapboundary(fill_color='#7777ff')
lon_prox=[-36.4,-38.5,-77,-71,-19.8,-69.5,-10.09,-4.93,9,-91.3,-85.92,-114.2,-109.3,103,-110.1,-42.6,1.16,-20.1,-23.2,-18.2,-17.95,-17.7,-10.1,-5.87]
lat_prox=[76.6,72.6,80.7,69.87,64.6,81.33,30.845,58.14,46,35.15,30.45,38.9,37.5,71.29,40,71.7,40.34,57.44,56.1,66.54,66.53,66.55,30.85,56.67]
x,y=m(lon_prox, lat_prox)
sumlist=[276,554,39,0,2,1,107,14,10,32,0,992,933,17,897,39,0,0,0,0,0,24,111,0]
# prep colors
hexcolors = []
for i in range(len(x)):
hexcolors.append('#%02x%02x%02x' % (int((255*sumlist[i])/max(sumlist)),0,0))
# scatter takes x,y,c arrays as parameters
m.scatter(x, y, c=hexcolors, s=60, zorder=10, alpha=0.7)
plt.show()

Add units to polar coordinate scatter plot in python

I have this polar scatter plot and I would like to show that distances from the origin are measured in centimeters by labelling the scale with a "cm." Any advice on how to do this?
import numpy as np
import matplotlib.pyplot as plt
r = R
theta = o
colors = theta
ax = plt.subplot(111, projection='polar')
c = plt.scatter(theta, r, cmap=plt.cm.hsv)
c.set_alpha(0.75)
plt.show()
Simply adding a label by use of plt.set_ylabel does not seem to work, sadly, as it always gets positioned at the origin. There is a simple way around it, though. You can introduce text with ax.text at an arbitrary position. My suggestion would be, to move the tick labels away from the data to make sure that the label won't be misunderstood and then to introduce the label as follows:
import numpy as np
import matplotlib.pyplot as plt
ax = plt.subplot(111, projection="polar")
ax.set_rlabel_position(270) # Moves the tick-labels
ax.text(0.52, 0.25, "cm", transform=ax.transAxes) # Adds text
plt.show()
The result looks like this:
I did something similar, that should work:
plt.yticks(np.arange(0,np.amax(r),3),["%.1f cm" % x for x in np.arange(0,np.amax(r),3)])
in np.arange(0,np.amax(r),3) the 0 is just minimum tick you want in the graph, the 3 is step you want ticks should be.

MatPlotlib: Patches not being displayed

I have just started experimenting with matplotlib, since I often come across instances where I need to plot some data, for which matplotlib seems an excellent tool. I attempted to adapt the ellipse example in the main site, so as to draw two circles instead, how ever after running the code, I find that none of the patches are displayed, I am not able to figure out what exactly is wrong... here is the code. Thanks in Advance.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
plt.axis([-3,3,-3,3])
ax = plt.axes([-3,3,-3,3])
# add a circle
art = mpatches.Circle([0,0], radius = 1, color = 'r', axes = ax)
ax.add_artist(art)
#add another circle
art = mpatches.Circle([0,0], radius = 0.1, color = 'b', axes = ax)
ax.add_artist(art)
print ax.patches
plt.show()
Which version of matplotlib are you using? I'm not able to replicate your results, I can see the two ellipsis quite well. I'm going by a long shot, but i guess you mean to do something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
# create the figure and the axis in one shot
fig, ax = plt.subplots(1,figsize=(6,6))
art = mpatches.Circle([0,0], radius = 1, color = 'r')
#use add_patch instead, it's more clear what you are doing
ax.add_patch(art)
art = mpatches.Circle([0,0], radius = 0.1, color = 'b')
ax.add_patch(art)
print ax.patches
#set the limit of the axes to -3,3 both on x and y
ax.set_xlim(-3,3)
ax.set_ylim(-3,3)
plt.show()

Categories