How to get corner plots from a MCMC parameters - python

I have a MCMC results and trying to plot with corner.py I only obtain one plot instead of three. I have one parameter that I want to plot but I don't know what's happening.
I used my data results (gamma) as follows:
figure = corner.corner(nh, labels=[r"$\Gamma$", r"$\Gamma$"],
quantiles=[0.16, 0.5, 0.84], show_titles=True, title_kwargs={"fontsize": 12},)
#corner.corner(gamma*2, fig=figure, color='red')
plt.show()

Related

Plot a Gaussian Distribution over a Histogram

I am trying to fit a Gaussian Distribution over a histogram for a project.
I have read a csv file and produced a pandas data frame.
I have produced the histogram, and I have tried a few methods to fit a distribution, however I always end up with a straight line formed at the bottom of my histogram. I think it may have something to do with the column I am trying to fit over is an Float64 but I'm not sure how to change that.
So far I have...
x=df1['rating']
plt.figure(figsize(10,10))
plt.hist(x, bins=20,color='c',edgecolor='k', alpha=0.65, linewidth=2)
plt.axvline(x.mean(), color='k', linestyle='dashed', linewidth=2,label="mean")
plt.axvline(x.median(),color='r',linestyle='dashed',linewidth=2)
Rating Histogram

Seaborn distplot() won't display frequency in the y-axis

I am trying to display the weighted frequency in the y-axis of a seaborn.distplot() graph, but it keeps displaying the density (which is the default in distplot())
I read the documentation and also many similar questions here in Stack.
The common answer is to set norm_hist=False and also to assign the weights in a bumpy array as in a standard histogram. However, it keeps showing the density and not the probability/frequency of each bin.
My code is
plt.figure(figsize=(10, 4))
plt.xlim(-0.145,0.145)
plt.axvline(0, color='grey')
data = df['col1']
x = np.random.normal(data.mean(), scale=data.std(), size=(100000))
normal_dist =sns.distplot(x, hist=False,color="red",label="Gaussian")
data_viz = sns.distplot(data,color="blue", bins=31,label="data", norm_hist=False)
# I also tried adding the weights inside the argument
#hist_kws={'weights': np.ones(len(data))/len(data)})
plt.legend(bbox_to_anchor=(1, 1), loc=1)
And I keep receiving this output:
Does anyone have an idea of what could be the problem here?
Thanks!
[EDIT]: The problem is that the y-axis is showing the kdevalues and not those from the weighted histogram. If I set kde=False then I can display the frequency in the y-axis. However, I still want to keep the kde, so I am not considering that option.
Keeping the kde and the frequency/count in one y-axis in one plot will not work because they have different scales. So it might be better to create a plot with 2 axis with each showing the kde and histogram separately.
From documentation norm_hist If True, the histogram height shows a density rather than a count. **This is implied if a KDE or fitted density is plotted**.
versusnja in https://github.com/mwaskom/seaborn/issues/479 has a workaround:
# Plot hist without kde.
# Create another Y axis.
# Plot kde without hist on the second Y axis.
# Remove Y ticks from the second axis.
first_ax = sns.distplot(data, kde=False)
second_ax = ax.twinx()
sns.distplot(data, ax=second_ax, kde=True, hist=False)
second_ax.set_yticks([])
If you need this just for visualization it should be good enough.

How to put text in figure using matplotlib in python

I am trying to plot multiple figures in a loop using matplotlib in python3. For every element in loop I draw the actual curve and Fitted curve. Here is the code that I have written:
plt.figure()
plt.plot(t, y,'g--',label="data")
plt.ylabel("Rate")
plt.xlabel("Time")
plt.title("{},{},{}".format(a,b,c),fontsize=8)
plt.text(0.8,0.8,"R-sq:"+str(round(r_squared,2)),fontsize=8)
plt.text(0.8,0.7,"decay:"+str(round(1/(12*fit_b),2)),fontsize=8)
plt.text(40.1,1.0,"#Cohort:"+str(C),fontsize=8)
plt.text(40.1,0.9,"Samples:"+str(int(S)),fontsize=8)
plt.grid()
plt.plot(t, yhat, 'b--',label='Fitted Function:\n $y = %0.4f e^{%0.4f t} $' % (fit_a, fit_b))
plt.legend(bbox_to_anchor=(0.4, 0.4), fancybox=True, shadow=True) #1.4, 1.1
name="./fig_weight/{}__{}__{}.png".format(a,b,c)
plt.savefig(name)
plt.show()
I am trying to put R-squared, Decay value and several other things in figure so that it is easy to visualize. For some of the plots alignment is fine but for others the text goes outside plot area.
I am running the same code for different iteration.
How to fix it?
Here are the plots:
You use plt.text location values with fixed numbers but your axis range is changing. One option is to plot locations based on your data, for example the last [-1] or middle [int(t.shape[0]/2.)] element on your curve with a shift tshift to offset the text, e.g.
plt.text(t[-1]+tshift, y[-1]+tshift,"R-sq:"+str(round(r_squared,2)),fontsize=8)

Fitted distribution is cut when creating several plots

I'm trying to plot 3 figures of the normal distribution of publications but I am only getting one good figure (UK). The remaining two (USA and JAPAN) have a normal curve that is incomplete.
I fitted the curves to histograms so you could say that each figure needs to hold two graphs, i.e. a histogram and a Gaussian distribution.
Please take a look at a part of my code and let me know how to fix this.
I am very open to suggestions, thanks.
My Matplotlib figures: fitted distribution, fitted distribution, fitted distribution
for item in totalIPs:
USA=totalIPs[18]
JAPAN=totalIPs[10]
UK=totalIPs[17]
AUSTRALIA=totalIPs[0]
#print(USA)
#print(JAPAN)
#print(UK)
#print(AUSTRALIA)
#print('done')
#print(country)
#print(ipFirmnames)
#print(totalIPs)
#print("done")
#Calculating mean and standard deviation
#from sublists in country list of lists
#i could write a function for this but dont know how
mu_USA=statistics.mean(USA)
mu_JAPAN=statistics.mean(JAPAN)
mu_UK=statistics.mean(UK)
std_USA=statistics.stdev(USA)
std_JAPAN=statistics.stdev(JAPAN)
std_UK=statistics.stdev(UK)
plt.figure(1)
plt.hist(USA, bins=10, normed=True, alpha=0.6, color='g')
plt.figure(2)
plt.hist(JAPAN,bins=10,normed=True,alpha=0.6, color ='g')
plt.figure(3)
plt.hist(UK, bins=10,normed=True, alpha=0.6, color = 'g')
standardize_USA=(np.array(USA)-mu_USA)/std_USA
standardize_JAPAN=(np.array(JAPAN)-mu_JAPAN)/std_JAPAN
standardize_UK=(np.array(UK)-mu_UK)/std_UK
xmin, xmax = plt.xlim()
x1=np.linspace(xmin, xmax, 100)
x2=np.linspace(xmin, xmax, 100)
x3=np.linspace(xmin, xmax, 100)
fitted_pdf_USA=ss.norm.pdf(x1,mu_USA, std_USA)
fitted_pdf_JAPAN=ss.norm.pdf(x3,mu_JAPAN, std_JAPAN)
fitted_pdf_UK=ss.norm.pdf(x3,mu_UK, std_UK)
plt.figure(1)
plt.plot(x1, fitted_pdf_USA, 'K', linewidth=2)
plt.figure(2)
plt.plot(x2, fitted_pdf_JAPAN,'K', linewidth=2)
fitted_pdf_JAPAN=ss.norm.pdf(x2,mu_JAPAN, std_JAPAN)
plt.figure(3)
plt.plot(x3, fitted_pdf_UK,'K', linewidth=2)
#plt.show()
print(standardize_USA)
print(standardize_JAPAN)
#print(USA)
print(UK)
print(JAPAN)
The problem that you have is that the limit for the curve is obtained from only one curve in the part
xmin, xmax = plt.xlim()
Make individual limits for every plot from its respective data, not the graph limit, and it will solve your issue. Do use using max() and min() from numpy.
x1=np.linspace(USA.min(),USA.max(),100)
Do it for every plot with its respective data. This way will give smooth curves, but the limit is not the graph, but the data. If it got too small, just increase the limits through a multiplication (as 1.1*max()) or a sum (max()+10; min depends on the data).

Draw a curve from the scatter plot in matplotlib in Python?

My Question:
How can i draw a curve though this data, thus describing an equation for this plot..
I generated this scatter plot by following code, but I am not able to figure out how to generate an equation for this data and draw the corresponding curve on this plot simultaneously. Please Help.!
def draw(data,xlabel,ylabel):
print('length of data : ',len(data))
x,y = [],[]
for i in data:
x.append((i[1]))
y.append((i[0]))
plt.scatter(x, y,marker=r'o',color='b')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.show()
Basically I want something like this:
You have to perform a curve fitting procedure, which is called a regression problem in mathematics. In your case it seems that data is more or less exponential, but you can fit arbitrary function through scipy.optimize.curve_fit
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

Categories

Resources