Plotting histogram with python 2.7 with log scale on y axis - python

I want to plot an histogram in python 2.7 whith a log scale on y axis:
I wrote this code:
plt.hist(lifes,100, log=True)
But the results of histogram changed for the two axis like this:
But in the initial histogram I have values > 2000 for x axis and all values are smaller than 200 for y axis.
I think it is because some values on x axis have one as effectif?

Related

Create a Bar plot in Python where x axis contains continuous variables and y axis contains categorical. Plot the frequency of y axis

I have a pandas data frame where x axis has continuous values and y axis has ones and zeros. How should I make a bar plot so that the x axis has bins like 5-10 10-15 ,.... and y axis represents the no of ones and zeros =, i.e there are 100 ones when x is between 5-10
I tried the following method
ax = df.plot.bar(x='PAY',Y='ACCEPTED')
Here Y is categorical
I got the error
No numeric data to plot

Plotting in AITOFF using defined values of x and y

How do I plot in AITOFF for values of x and y
And then superimpose another set of x and y values with different color. Can you send me the code to plot
I have tried subplot but not working

Scaling the y axis in matplotlib

I want to draw multiple plots in the same plot so I took 2d list in which for one parameter it's storing the values in the string format and I am using the for loop for the same but when I am plotting the larger values on y axis are coming below the smaller values
Here is the code snippet that might help to understand
m=['H','.','<','^','*','+','x','#']
cnt=0
for i in all:# here all has the row wise data to plot
matplotlib.pyplot.plot(l3,i,m[cnt])# l3 contains the values about the x axis
cnt=cnt+1
plt.xlabel("x")
plt.ylabel("y")
plt.legend(para,loc='best')# para contains the info about the y parameters
plt.show()
The graph is coming like this how to get 12000 above 0 in the graph
This is the plot I got how to rescale it so that all values comes in acsending order on y axis
You have to convert strings to floats before plotting
for i in all:# here all has the row wise data to plot
y = [float(ii) for ii in i]
matplotlib.pyplot.plot(l3, y, m[cnt])# l3 contains the values about the x axis
cnt=cnt+1

Python 2d Ratio Plot with weighted mean trendline

Hello and thanks in advance. I am starting with a pandas dataframe and I would like like make a 2d plot with a trendline showing the weighteed mean y value with error bars for the uncertainty on the mean. The mean should be weighted by the total number of events in each bin. I start by grouping the df into a "photon" group and a "total" group where "photon" is a subset of the total. In each bin, I am plotting the ratio of photon events to total. On the x axis and y axis I have two unrelated variables "cluster energy" and "perimeter energy".
My attempt:
#make the 2d binning and total hist
energybins=[11,12,13,14,15,16,17,18,19,20,21,22]
ybins = [0,.125,.25,.5,.625,.75,1.,1.5,2.5]
total_hist,x,y,i = plt.hist2d(train['total_energy'].values,train['max_perimeter'].values,[energybins,ybins])
total_hist = np.array(total_hist)
#make the photon 2d hist with same bins
groups = train.groupby(['isPhoton'])
prompt_hist,x,y,i = plt.hist2d(groups.get_group(1)['total_energy'].values,groups.get_group(1)['max_perimeter'].values,bins=[energybins,ybins])
prompt_hist = np.array(prompt_hist)
ratio = np.divide(prompt_hist,total_hist,out=np.zeros_like(prompt_hist),where = total_hist!=0)
#plot the ratio
fig, ax = plt.subplots()
ratio=np.transpose(ratio)
p = ax.pcolormesh(ratio,)
for i in range(len(ratio)):
for j in range(len(ratio[i])):
text = ax.text(j+1, i+1, round(ratio[i, j], 2),ha="right", va="top", color="w")
ax.set_xticklabels(energybins)
ax.set_yticklabels(ybins)
plt.xlabel("Cluster Energy")
plt.ylabel("5x5 Perimeter Energy")
plt.title("Prompt Photon Fraction")
def myBinnedStat(x,v,bins):
means,_,_ = stats.binned_statistic(x,v,'mean',bins)
std,_ ,_= stats.binned_statistic(x,v,'std',bins)
count,_,_ = stats.binned_statistic(x,v,'count',bins)
return [ufloat(m,s/(c**(1./2))) for m,s,c in zip(means,std,count)]
I can then plot an errorbar plot, but I have not been able to plot the errorbar on the same axis as the pcolormesh. I was able to do this with hist2d. I am not sure why that is. I feel like there is a cleaner way to do the whole thing.
This yields a plot
pcolormesh plots each element as a unit on the x axis. That is, if you plot 8 columns, this data will span 0-8 on the x axis. However, you also redefined the x axis ticklabel so that 0-10 is labeled as 11-21.
For your errorbars, you specified x values at 11-21, or so it looks, which is where the data is plotted. But is not labeled since you changed the ticklabels to correspond to pcolormesh.
This discrepancy is why your two plots do not align. Instead, you could use "default" x values for errorbar or define x values for pcolormesh. For example, use:
ax.errorbar(range(11), means[0:11], yerr=uncertainties[0:11])

How to specify axes for a pcolormesh?

So i'm trying to plot a (125 x 1000) grid with specified values. I'm using matplotlib with pcolormesh. This is how my code looks, enzyme array just symbolic.
enzyme = np.array([125 x 1000])
plt.pcolormesh(enzyme, cmap='Reds')
plt.colorbar()
plt.show()
The x-axis is my spatial resolution and my y-axis is time. My x-axis just runs from 0 to 125 and y-axis runs from 0 to 1000. But my actual problem is in hours, so I want the y-axis to show like 0hours -> 24hours per 2hours step. Something similar for the x-axis. So the grid index is not the right scale and number for my plot. How do I fix this.
I tried already including like
pcolormesh(x, y, enzyme)
with x and y a 1D array, but these have to match the length of my enzyme grid and i have way too many datapoints to put on the x- and y- axis.
I would suggest creating a new x and y array that fits the size of your enzyme array. You would assign each x and y value the time that corresponds to each of your indices. For example, if your 0-1000 y-axis is supposed to represent 24 hours, you could do something like this:
increase=24/1000.
yvals=np.arange(0,24,increase)
Dividing 24/1000 will give you the increment needed such that you have 1000 values going from 0,24 hours.
You then can change the xtick increments with something like this:
ax.set_xticks(np.arange(0,24,2))

Categories