I have just run a simple task of trying to plot the probability density histogram for a simulation I ran. However, when I plot it, the probability for each bin seems not to match the result of the frequency plot. with 50 bins i would expect each bin to have an average probability of 2% which is not reflected in the chart.
Thanks in advance
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plntAcres = 88.0
hvstPer = 0.99
hvstAcres = plntAcres*hvstPer
yldAcre = np.random.triangular(47,48,49, 10000)
carryIn = 464
pdn = hvstAcres * yldAcre
imp = 25.0
ttlSup = carryIn + pdn + imp
crush = np.random.uniform(1945, 1990,10000)
expts = np.random.uniform(2085, 2200,10000)
seedRes = 130
ttlDem = crush + expts + seedRes
carryOut = ttlSup - ttlDem
print carryOut
plt.hist(carryOut, bins=50,normed=True)
plt.title("Carry Out Distribution")
plt.xlabel("Value")
plt.ylabel("Probability")
plt.show()
Probability density of Carry out
Tweaking Matplotlib Preferably, one that has tick mark and other features closer to the aesthetic you want to achieve. Turn the frame and grid lines off. Tweak the x-axis so that there is a gap with the y-axis, which seems more appropriate for histograms. Have color options allowing for separation between bins.
You first create a plot object ax . Here, you can specify the number of bins in the histogram, specify the color of the histogram and specify density plot option with kde and linewidth option with hist_kws . You can also set labels for x and y axis using the xlabel and ylabel arguments.
A probability density function (PDF) is the continuous version of the histogram with densities (you can see this by imagining infinitesimal small bin widths); it specifies how the probability density is distributed over the range of values that a random variable can take.
Histogram plots provide a fast and reliable way to visualize the probability density of a data sample. Parametric probability density estimation involves selecting a common distribution and estimating the parameters for the density function from a data sample.
In the hist
function, the normed
argument does not result in probabilites, but in probability densities. If you want the probabilities themselves, use the weights
argument instead (and supply with 1 / len(carryOut)
).
The crucial two lines:
weights = np.ones_like(carryOut) / (len(carryOut))
plt.hist(carryOut, bins=50, weights=weights)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With