How to draw few angles to compare them? - python

I have a csv database with number of angles (all of them from 0 to 360). I want kind of matplotlib diagram which show me distribution of all angles (for every row in csv).
Maybe you know how to realize that with any python library?
Or please show me how to draw at least two angles as vectors?
I was find only polar plot in matplotlib, that have degrees and its a circle. And I need something like this image (three angles shown and understandable for comparing) (draw lines by hand).
https://i.stack.imgur.com/Ls5zI.png

The polar plot is a bit strange, I managed to create the plot using a scatter plot set to polar. I haven't included any code to read in the angles from a csv, just note that should be converted to radians before plotting.
import numpy as np
import matplotlib.pyplot as plt
theta = np.radians(np.array([10, 20, 40]))
radius = np.ones(theta.size) # make a radius for each point of length 1
fig = plt.figure()
ax = plt.subplot(111, polar=True) # Create subplot in polar format
for t, r in zip(theta, radius):
ax.plot((0, t), (0, r))
fig.show()

Related

How to rotate theta ticklabels in a matplotlib polar plot?

In a matplotlib polar plot, I would like to rotate each individual theta ticklabel by a different angle. However, I cannot find anything in the documentation to do that. Here's a simple plot to illustrate:
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes(polar=True)
ax.set_thetalim(0., np.pi/4.)
ax.set_rlim(0., 2.)
# set the size of theta ticklabels (works)
thetatick_locs = np.linspace(0.,45.,4)
thetatick_labels = [u'%i\u00b0'%np.round(x) for x in thetatick_locs]
ax.set_thetagrids(thetatick_locs, thetatick_labels, fontsize=16)
This adds labels at 0, 15, 30 and 45 degrees. What I'd like to do is rotate the 15 degree label by 15 degrees, the 30 degree label by 30 degrees, and so on, so that each label's text direction is radially outward. Since get_xticklabels on a PolarAxes instance seems to get the theta ticklabels, I tried:
for i,t in enumerate(ax.get_xticklabels()):
t.set_rotation(thetatick_locs[i])
However, that did nothing. Is there any other way of doing what I want? In general, I'm finding that the documentation for polar axes is not as thorough as for rectangular axes, probably because fewer people use it. So maybe there's already a way to do this.
Your current method works for cartesian coordinates but for polar coordinates, you can use the workaround solution presented earlier here. I have adapted that answer for you below. You can add the following code after setting the theta grids
fig.canvas.draw()
labels = []
for label, angle in zip(ax.get_xticklabels(), thetatick_locs):
x,y = label.get_position()
lab = ax.text(x,y, label.get_text(), transform=label.get_transform(),
ha=label.get_ha(), va=label.get_va())
lab.set_rotation(angle)
labels.append(lab)
ax.set_xticklabels([])
plt.show()

How to plot imshow starting at a certain radius?

I am trying to make a plot to specify gravitational redshift as a function of distance. However, i have a problem in plotting. I want to plot it from rs=1.0 because no object can be detectable within a schwarzshild radius, rs=1.0 in my case.
I tried to do mask but it was not working. Is there any method to do contour plot with the starting radius about at r>1?. Actually, in the above figure, I want to let my imshow to plot the amount of redshift from the blue solid circle, not at r=0 (i have no idea why it starts there).
import numpy as np
import matplotlib.pyplot as plt
rs=1
ang=np.linspace(0,2*np.pi,2000)
x, y = np.mgrid[2:100, 2:100]
dist = np.hypot(x, y) # Linear distance from point 0, 0
z = np.sqrt(1/dist)
f=1/np.sqrt((1-rs*z)/(1-rs/4))*(1/10)
plt.imshow(f, interpolation='bilinear')
a=np.cos(ang)
b=np.sin(ang)
plt.xlim(0,15)
plt.ylim(0,15)
plt.plot(a,b)
plt.colorbar()
plt.show()
I think there is a misunderstanding in the kind of plot. plt.imshow creates colormappings of 2D-arrays - but the scales of the axes are not showing the independant data variables, but only the indices of the array. This is different from e.g. plt.contourf.
In fact, your array f doesn't even have values at [x=1, y=1], as xand y start at 2...
Let's compare imshow and contourf:
fig, axs = plt.subplots(1, 2)
axs[0].imshow(f, interpolation='bilinear')
axs[0].set_xlim(0,15)
axs[0].set_ylim(0,15)
axs[1].contourf(x, y, f)
axs[1].set_aspect(1)
axs[1].set_xlim(0,15)
axs[1].set_ylim(0,15)
Or in other words: check the limits of your scales without setting xlim and ylim: they go from -0,5 to 97,5 instead of 2 to 99...
However, there are interesting kwargs of imshow for you.
Look what happens to the above plot with
axs[0].imshow(f, interpolation='bilinear', origin='lower', extent=[2, 99, 2, 99])

Plot a 3D surface plot based on points from ginput using Matplotlib

I am trying to plot peaks (3D topographical peaks) on ginput() points specified by a user. These peaks have to be in the form of a 3D surface plot as shown in the example image on this link 3D surface plot peaks .So far I have managed to acquire the points from the ginput() function and separated them into two different arrays one with x coordinates another with y coordinates. How do I go about plotting these peaks on these points using the cosine wave?
I am still new to python so I have written a few lines of pseudo code of what I am trying to achieve in order to try and add more clarity to my question.
import sys, numpy as np, matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
pts = []
fig = plt.figure()
ax = fig.gca(projection='3d')
Npeaks = input(' Enter your number of peaks here..')
pts = plt.ginput(Npeaks)
x=map(lambda x: x[0],pts) # creating an array with x coordinates
y=map(lambda x: x[1],pts) # creating an array with y coordinates
Pseudo code of the code I am trying to achieve
if number_of_peaks > 0 :
ax.plot_surface( plot a peak at those points )
else:
sys.exit()
ax.set_xlabel('X')
ax.set_xlim(value , value )
ax.set_ylabel('Y')
ax.set_ylim( value , value )
ax.set_zlabel('Z')
ax.set_zlim( value , value )
plt.show()

Changing axis options for Polar Plots in Matplotlib/Python

I have a problem changing my axis labels in Matplotlib. I want to change the radial axis options in my Polar Plot.
Basically, I'm computing the distortion of a cylinder, which is nothing but how much the radius deviates from the original (perfectly circular) cylinder. Some of the distortion values are negative, while some are positive due to tensile and compressive forces. I'm looking for a way to represent this in cylindrical coordinates graphically, so I thought that a polar plot was my best bet. Excel gives me a 'radar chart' option which is flexible enough to let me specify minimum and maximum radial axis values. I want to replicate this on Python using Matplotlib.
My Python script for plotting on polar coordinates is as follows.
#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x # in radians
offset = 2.0
R1 = [-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358]
fig1 = plt.figure()
ax1 = fig1.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax1.set_rmax(1)
ax1.plot(theta,R1,lw=2.5)
My plot looks as follows:
But this is not how I want to present it. I want to vary my radial axis, so that I can show the data as a deviation from some reference value, say -2. How do I ask Matplotlib in polar coordinates to change the minimum axis label? I can do this VERY easily in Excel. I choose a minimum radial value of -2, to get the following Excel radar chart:
On Python, I can easily offset my input data by a magnitude of 2. My new dataset is called R2, as shown:
#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x # in radians
offset = 2.0
R2 = [1.642,1.517,1.521,1.654,1.879,2.137,2.358,2.483,2.479,2.346,2.121,1.863,\
1.642,1.517,1.521,1.654,1.879,2.137,2.358,2.483,2.479,2.346,2.121,1.863,1.642,\
1.517,1.521,1.654,1.879,2.137,2.358,2.483,2.479,2.346,2.121,1.863,1.642]
fig2 = plt.figure()
ax2 = fig2.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax2.plot(theta,R2,lw=2.5)
ax2.set_rmax(1.5*offset)
plt.show()
The plot is shown below:
Once I get this, I can MANUALLY add axis labels and hard-code it into my script. But this is a really ugly way. Is there any way I can directly get a Matplotlib equivalent of the Excel radar chart and change my axis labels without having to manipulate my input data?
You can just use the normal way of setting axis limits:
#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x # in radians
offset = 2.0
R1 = [-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358]
fig1 = plt.figure()
ax1 = fig1.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax1.set_ylim(-2,2)
ax1.set_yticks(np.arange(-2,2,0.5))
ax1.plot(theta,R1,lw=2.5)

Create a stack of polar plots using Matplotlib/Python

I need to generate a stack of 2D polar plots (a 3D cylindrical plot) so that I can view a distorted cylinder. I want to use matplotlib since I already have it installed and want to distribute my code to others who only have matplotlib. For example, say I have a bunch of 2-D arrays. Is there any way I can do this without having to download an external package? Here's my code.
#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x # in radians
A0 = 55.0
offset = 60.0
R = [116.225,115.105,114.697,115.008,115.908,117.184,118.61,119.998,121.224,122.216,\
122.93,123.323,123.343,122.948,122.134,120.963,119.575,118.165,116.941,116.074,115.66\
,115.706,116.154,116.913,117.894,119.029,120.261,121.518,122.684,123.594,124.059,\
123.917,123.096,121.661,119.821,117.894,116.225]
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8],polar=True) # Polar plot
ax.plot(theta,R,lw=2.5)
ax.set_rmax(1.5*(A0)+offset)
plt.show()
I have 10 more similar 2D polar plots and I want to stack them up nicely. If there's any better way to visualize a distorted cylinder in 3D, I'm totally open to suggestions. Any help would be appreciated. Thanks!
If you want to stack polar charts using matplotlib, one approach is to use the Axes3D module. You'll notice that I used polar coordinates first and then converted them back to Cartesian when I was ready to plot them.
from numpy import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
n = 1000
fig = plt.figure()
ax = fig.gca(projection='3d')
for k in linspace(0, 5, 5):
THETA = linspace(0, 2*pi, n)
R = ones(THETA.shape)*cos(THETA*k)
# Convert to Cartesian coordinates
X = R*cos(THETA)
Y = R*sin(THETA)
ax.plot(X, Y, k-2)
plt.show()
If you play with the last argument of ax.plot, it controls the height of each slice. For example, if you want to project all of your data down to a single axis you would use ax.plot(X, Y, 0). For a more exotic example, you can map the height of the data onto a function, say a saddle ax.plot(X, Y, -X**2+Y**2 ). By playing with the colors as well, you could in theory represent multiple 4 dimensional datasets (though I'm not sure how clear this would be). Examples below:

Categories