horizontal plot in python - python

I am looking for a plot that is rotated 90 degree in clockwise directions. An similar example of such plot is "hist(x, orientation='horizontal')". Is there any way to achieve similar orientation.
#Make horizontal plots.
import random
import matplotlib.pyplot as plt
x = random.sample(range(1000), 100)
x
plt.plot(x) #orientation='horizontal'
plt.show()

plt.plot(x) plots your x values automatically against the y-axis. In order to get a rotated plot you have to plot your x values against the x axis. So you'll need a to make vector for the y-axis, which has the same length as your sample.
import random
import matplotlib.pyplot as plt
import numpy as np
x=random.sample(1000)
y=np.arange(1000)
plt.plot(x,y)
Using plt.plot(x), matplotlib takes your x-values as its y-values and generates a vector for the x axis automatically.

Related

How to plot polar historgram?

I have the data of azimuth and the magnitude of the earth property and would like to plot it on the polar azimuthal histrogram (like a rose diagram)
This is the excerpt of the data:
degrees velocity
22.44903 9449.275
22.4512 9474.46
22.45321 9717.624
22.45537 9745.26
22.45739 9746.532
22.45953 9372.272
22.46157 9899.907
22.46369 9499.646
22.46581 9856.678
22.46786 9811.213
22.46999 9765.846
22.47202 9814.11
22.47418 9974.829
22.47619 10162.89
This is what I have tried, but it produces the plot that is not similar to the one I have expected:
from physt import histogram, binnings, special
import numpy as np
import matplotlib.pyplot as plt
data = genfromtxt(file, delimiter=',')
x=data[:,0]
y=data[:,1]
hist = special.polar_histogram(x, y)
ax = hist.plot.polar_map()
I suspect there could be a problem with conversion of coordinates. What I want is simply distribution (histogram) of values plotted along the azimuth axis
Try with seaborn pakage
import seaborn as sns
sns.barplot(x,y)

How to fix the limits of color scale in matplotlib?

I am implementing a loop to produce contour plots using the function contourf in matplotlib. The objective of the study is to find out any moving patterns in the area. But, the plots produced are having different color scales. Some of them have -4 to 4 while others have -1.5 to 9.0 and so on which renders the interpretation pointless. How can I fix this color scale to -5.0 to 9.0?
Also, when I try to export the plots number of colorbars increases in each plot. For example the second plot in the loop has 2 colorbars and fifth plot has 5 colorbars and so on.
What I've done so far:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from numpy import linspace
from numpy import meshgrid
i=0
while i<len(inputdata):
map = Basemap(projection='cyl', llcrnrlat=5.125, llcrnrlon=60.125, urcrnrlat=34.875, urcrnrlon=94.875)
data = np.array(inputdata[i])
x = linspace(map.llcrnrx, map.urcrnrx, data.shape[1])
y = linspace(map.llcrnry, map.urcrnry, data.shape[0])
xx, yy = meshgrid(x, y)
map.contourf(xx, yy, data, cmap = 'summer_r')
plt.colormap()
plt.savefig('filename.jpg',dpi=300)
i+=1
In order to change the limits of the colorbar, you can call plt.clim() before you call plt.colorbar and specify the range manually:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from numpy import linspace
from numpy import meshgrid
i=0
while i<len(inputdata):
map = Basemap(projection='cyl', llcrnrlat=5.125, llcrnrlon=60.125, urcrnrlat=34.875, urcrnrlon=94.875)
data = np.array(inputdata[i])
x = linspace(map.llcrnrx, map.urcrnrx, data.shape[1])
y = linspace(map.llcrnry, map.urcrnry, data.shape[0])
xx, yy = meshgrid(x, y)
map.contourf(xx, yy, data, cmap = 'summer_r')
plt.clim(-5, 9) # manually setup the range of the colorscale and colorbar
plt.colormap()
plt.savefig('filename.jpg',dpi=300)
plt.clf()
i+=1
You also may want to clear the figure after you have saved it to avoid multiple colorbars appearing using plt.clf()

plotting vertical bars instead of points, plot_date

I want to plot vertical bars instead of points. The actual data I have are irregularly spaced, so this will help visualize gaps more easily.
When I try to plot it, the best I can do are points, which don't increase in size as you zoom in!
import matplotlib
from matplotlib import pyplot as plt
import datetime
XX = [datetime.date.today()+datetime.timedelta(x) for x in range(10)]
YY = range(10)
plt.plot_date(XX,YY,'o')
Any ideas on how I can make taller/bigger (but not wider!) points?
You can use ax.vlines to plot a collection of vertical lines.
You can adjust ymin and ymax to suit your data.
import matplotlib
from matplotlib import pyplot as plt
import datetime
XX = [datetime.date.today()+datetime.timedelta(x) for x in range(10)]
plt.vlines(XX, ymin=0, ymax=1, linewidth=5)
plt.show()
Did you mean bars like this?
And here is the code:
import matplotlib
from matplotlib import pyplot as plt
import datetime
XX = [datetime.date.today()+datetime.timedelta(x) for x in range(10)]
YY = range(10)
plt.plot_date(XX,YY,'|')
plt.show()
You can change the shape of your plot by changing the third argument you pass in the plt.plot_date function.
In your code you are passing an 'o' that is why you get a dot. Here i pass bar to plot bar.

Changing the scale of the x axis in a plot

Let's say I have a 2D array I plot using imshow. I want to be able to scale the x axis to the percent of the x axis. So I plot the data like this:
import numpy as np
import matplotlib.pyplot as plt
A = np.random.random((10,10))
plt.show(plt.imshow(A,origin='low', extent=[0,10,0,10]))
Now I'm not sure how I can do that. Any insight?
EDIT: fixed to include extent as #tcaswell pointed out

how do you radially 'sweep out' a 1D array to plot 3d figure in python? (to represent a wavefunction)

effectively I have a large 1D array of heights. As a small example consider:
u=array([0,1,2,1,0,2,4,6,4,2,1])
and a 1D array, the same size as u, of radial values which the heights correspond to, e.g.:
r=array([0,1,2,3,4,5,6,7,8,9,10])
Obviously plotting these with:
pylab.plot(r,u)
gives a nice 2D plot.
How can one sweep this out around 360 degrees, to give a 3D contour/surface plot?
If you can imagine it should look like a series of concentric, circular ridges, like for the wavefunction of an atom.
any help would be much appreciated!
You're better off with something more 3D oriented than matplotlib, in this case...
Here's a quick example using mayavi:
from enthought.mayavi import mlab
import numpy as np
# Generate some random data along a straight line in the x-direction
num = 100
x = np.arange(num)
y, z = np.ones(num), np.ones(num)
s = np.cumsum(np.random.random(num) - 0.5)
# Plot using mayavi's mlab api
fig = mlab.figure()
# First we need to make a line source from our data
line = mlab.pipeline.line_source(x,y,z,s)
# Then we apply the "tube" filter to it, and vary the radius by "s"
tube = mlab.pipeline.tube(line, tube_sides=20, tube_radius=1.0)
tube.filter.vary_radius = 'vary_radius_by_scalar'
# Now we display the tube as a surface
mlab.pipeline.surface(tube)
# And finally visualize the result
mlab.show()
#!/usr/bin/python
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from scipy.interpolate import interp1d
from matplotlib import cm
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)
u=np.array([0,1,2,1,0,2,4,6,4,2,1])
r=np.array([0,1,2,3,4,5,6,7,8,9,10])
f=interp1d(r,u)
# walk along the circle
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = R*np.cos(P),R*np.sin(P)
Z=f(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xticks([])
plt.show()

Categories