I am trying to build a histogram in python matplot lib
def histogram(value_array,xlabel): mu, sigma = np.mean(value_array), np.std(value_array) n, bins, patches = plt.hist(value_array, 50, normed=1, facecolor='blue',histtype = "bar", alpha=0.75) # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = plt.plot(bins, y, linewidth=1) plt.axis([np.min(value_array), np.max(value_array), np.min(y),np.max(y)]) plt.grid(False) plt.xlabel(xlabel) plt.ylabel("pdf") plt.show()
See also questions close to this topic
How to rename files using python script according to downloading time?
- I was download a playlist in which all videos are not arranged in manner.
- so i want to rename each and every file according to downloading
EXAMPLE : suppose there are three videos :
First vdo downloaded at 7 : 56 AM
Second vdo downloaded at 7 : 56 : 5 AM
Third vdo download at 7 : 57 AM
after applying the script
the file must renamed as 1, 2, 3 at the starting of the file name. (shouldn't loss the real file name but add 1, 2, 3 at the start of the file name)
Transcrypt: How to check for empty list?
This a simple python program which causes an endless loop when compiled with Transcrypt 3.7.5:
stack = [ch for ch in "Hello"] while stack: print(stack.pop())
This can be solved by replacing
while len(stack):but this would only be an unacceptable workaround. Is there any other Transcrypt-related pragma switch to get around it?
Delete duplicated files
I hope you guys can help me. I have 2 PCs, 1 iMac and 2 External HD full of pictures and files. Some of them are duplicated and I was looking for a script that I can run on Windows, Mac, and HD to delete all the duplicated files. Can any of you please suggest the best way to get this accomplished. Best regards. Manoelito
Matplotlib/basemap: plot a line with different colors, based on depending values
I have a set of lat/lon points, which represent the center of a hurricane, and a third list of wind speeds. I'm trying to plot of the hurricane as a line connecting the lat/lons, but coloring the line various colors, depending on what the wind speed is at that location.
I've seen various threads on how to do this in matplotlib, but I'm having trouble making the conversion to basemap.
The following code is basic to plot the entire line in green, but as a rookie, I'm having trouble applying what I've seen regarding segments or a listed colormap work properly in the basemap forum. Probably because I'm misunderstanding how to link the wind values to the points. (I have yards of incorrect code trying these, so much so that I'll skip posting any because I confused myself). Although I will post the listed colormap for reference.
I'm only going to be drawing 20-30 points here, tops, so timing and resources aren't an issue.
Am I way off base in attempting to apply either segments or a listed colormap? Appreciate any help!
map = Basemap(projection='merc',llcrnrlat=lowlat,urcrnrlat=highlat,llcrnrlon=leftlon,urcrnrlon=rightlon,lat_ts=30,resolution='i',area_thresh=10000) map.drawmapboundary() map.drawcoastlines(linewidth=0.50) map.drawcountries(linewidth=0.25) map.drawstates(linewidth=0.25) map.drawmeridians(np.arange(0,360,10),labels=[0,0,0,1],fontsize=10) map.drawparallels(np.arange(-90,90,10),labels=[1,0,0,0],fontsize=10) plt.title(ensupper + ' Tropical Cyclone ' + storm +' ' + cyd + ' ' + cyc + 'UTC Cycle' ) lat = [33.7, 32.9, 32.1, 31.5, 31.0, 30.7, 30.5, 30.4, 30.0, 29.4, 29.6, 29.9, 29.7] lon = [-27.1, -26.6, -26.6, -26.6, -26.9, -27.4, -28.0, -28.5, -28.9, -29.9, -30.9, -31.4, -32.3] wind = [49, 44, 53, 60, 69, 77, 86, 85, 85, 74, 75, 62, 44] x,y = map(lat,lon) map.plot(x,y,color='green',linestyle='solid',linewidth=1, markersize=1) cmap = ListedColormap(['black', 'saddlebrown', 'green', 'gold', 'darkorange', 'red', 'purple']) norm = BoundaryNorm([0, 33, 64, 83, 95, 113, 134, 300], cmap.N)
Matplotlib: Check for empty plot
I have a loop which loads and plots some data, something like this:
import os import numpy as np import matplotlib.pyplot as plt for filename in filenames: plt.figure() if os.path.exists(filename): x, y = np.loadtxt(filename, unpack=True) plt.plot(x, y) plt.savefig(filename + '.png') plt.close()
Now, if the file does not exist, the data is not loaded or plotted but an (empty) figure is still saved. In the above example I could correct for this simply by including all of the
pltcalls inside of the
ifstatement. My real use case is somewhat more involved, and so I am in search for a way to ask
plt/the figure/the axis whether or not the figure/axis is completely empty or not. Something like
for filename in filenames: plt.figure() if os.path.exists(filename): x, y = np.loadtxt(filename, unpack=True) plt.plot(x, y) if not plt.figure_empty(): # <-- new line plt.savefig(filename + '.png') plt.close()
Mayavi - hide "diagonal" lines in wireframe
I've just started using mayavi and was wondering if there's a way to plot the wireframe representation of the surface that looks like the one I'm used from matplotlib 3d.
# sphere example import numpy as np from mayavi import mlab from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D # azimut and polar angle phi = np.linspace(0,2*np.pi,10) theta = np.linspace(0,np.pi,10) phi, theta = np.meshgrid(phi,theta) # cartesian coordinates x = np.cos(phi)*np.sin(theta) y = np.sin(phi)*np.sin(theta) z = np.cos(theta) #plotting using matplotlib fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_wireframe(x,y,z)
#plotting using mayavi mlab.mesh(x,y,z,representation='wireframe', color=(0,0,1))
As you can see the outputs differ: matplotlib prints the lines with constant
theta. Mayavi however, also prints diagonal lines (highlighted in yellow) connecting those paths.
I prefer the matplotlib version. Is there a way to achieve the same wireframe with mayavi?