python, histogram,data fitting - python

I wish to make a Histogram in Python 3 from an input file containing the raw data of energy (.dat). And on the same plot I want to plot a formula(distribution analytical, pho vs energy). It is easy to plot them seperately, but I need combined version. Can you help?

If you want 2 plots in the same figure look into this:
https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html
fig, (ax1, ax2) = plt.subplots(2)
If you want to have 2 plots int he same plot that share axis use this:
https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/two_scales.html#sphx-glr-gallery-subplots-axes-and-figures-two-scales-py
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis

Related

Displaying xticks using subplot Python

I'm attempting to plot a few subplots. The issue that I'm running into is in labeling the x-axis for each plot since they're all different.
The variables relHazardRate and relHazardFICO are dataframes of size 50 X 2
I attempting to plot the below I'm unable to show the x-axis tick marks (i.e. relHazardRate is a variable ranging from 3% to 6%, and relHazardFICO is a variable ranging from 300-850. Each figure in the subplot will have its own x-axis/ticker (there are 8 such plots) and I have provided my logic for 2 as shown below.
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6), (ax7, ax8)) = plt.subplots(4, 2,figsize=(12,8))
ax1.plot(relHazardRate['orig_coupon'],relHazardRate['Hazard Multiplier']);
ax1.title.set_text('Original Interest Rate');
ax1.set_xticks(range(len(relHazardRate['orig_coupon'])));
ax1.set_xticklabels(relHazardRate['orig_coupon'].to_list())
ax2.plot(relHazardFICO['orig_FICO'],relHazardFICO['Hazard Multiplier'], 'tab:orange');
ax2.title.set_text('Original FICO');
ax2.set_xticks(range(len(relHazardRate['orig_FICO'])));
ax2.set_xticklabels(relHazardRate['orig_FICO'].to_list())
ax.3 through ax.8 follow a similar decleration as the described above
for ax in fig.get_axes():
ax.label_outer()
The subplot that I get is as follows, I want to label each plot with its own x-axis, as shown this is not happening.
Remove the lines with label_outer.
From the docs:
label_outer()
Only show "outer" labels and tick labels.
x-labels are only kept for subplots on the last row; y-labels only for subplots on the first column
Clearly this is what is causing the behaviour you see in your plot

How to show separate boxplots for all columns?

When I try to show boxplots for all columns I used this command:
df_num.boxplot(rot=90)
But as you can see the boxes are so tiny as their ranges are different and should not be sharing the same y-axis. Can I do something like below but in boxplots? Thanks!
You could do it this way (example including just 2 of the columns but you can obviously add more):
fig, ax = plt.subplots(figsize=(12,6), ncols=2)
df_num["backers_count"].plot.box(ax=ax[0])
df_num["converted_pledged_amount"].plot.box(ax=ax[1]);
...or with Seaborn:
fig, ax = plt.subplots(figsize=(12,6), ncols=2)
sns.boxplot(data=df_num, y="backers_count", ax=ax[0])
sns.boxplot(data=df_num, y="converted_pledged_amount", ax=ax[1]);
If you want to display them in a grid of, say 3 rows and 3 columns then you can change the ncols=2 bit to nrows=3, ncols=3, and then instead of ax=ax[0], ax=ax[1] etc you would write ax=ax[0,0], ax=ax[0,1] etc

Matplotlib Subplot axes sharing: Apply to every other plot?

I am trying to find a way to apply the shared axes parameters of subplot() to every other plot in a series of subplots.
I've got the following code, which uses data from RPM4, based on rows in fpD
fig, ax = plt.subplots(2*(fpD['name'].count()), sharex=True, figsize=(6,fpD['name'].count()*2),
gridspec_kw={'height_ratios':[5,1]*fpD['name'].count()})
for i, r in fpD.iterrows():
RPM4[RPM4['name'] == RPM3.iloc[i,0]].plot(x='date', y='RPM', ax=ax[(2*i)], legend=False)
RPM4[RPM4['name'] == RPM3.iloc[i,0]].plot(kind='area', color='lightgrey', x='date', y='total', ax=ax[(2*i)+1],
legend=False,)
ax[2*i].set_title('test', fontsize=12)
plt.tight_layout()
Which produces an output that is very close to what I need. It loops through the 'name' column in a table and produces two plots for each, and displays them as subplots:
As you can see, the sharex parameter works fine for me here, since I want all the plots to share the same axis.
However, what I'd really like is for all the even-numbered (bigger) plots to share the same y axis, and for the odd-numbered (small grey) plots to all share a different y axis.
Any help on accomplishing this is much appreciated, thanks!

Sub Plots using Seaborn

I am trying to plot box plots and violin plots for three variables against a variable in a 3X2 subplot formation. But I am not able to figure out how to include sns lib with subplot function.
#plots=plt.figure()
axis=plt.subplots(nrows=3,ncols=3)
for i,feature in enumerate(list(df.columns.values)[:-1]):
axis[i].plot(sns.boxplot(data=df,x='survival_status_after_5yrs',y=feature))
i+=1
axis[i].plot(sns.violinplot(data=df,x='survival_status_after_5yrs',y=feature))
plt.show()```
I am expecting 3X2 subplot, x axis stays same all the time y axis rolls over the three variables I have mentioned.
Thanks for your help.
I think you have two problems.
First, plt.subplots(nrows=3, ncols=2) returns a figure object and an array of axes objects so you should replace this line with:
fig, ax = plt.subplots(nrows=3, ncols=2). The ax object is now a 3x2 numpy array of axes objects.
You could turn this into a 1-d array with ax = ax.flatten() but given what I think you are trying to do I think it is easier to keep as 3x2.
(Btw I assume the ncols=3 is a typo)
Second, as Ewoud answer mentions with seaborn you pass the axes to plot on as an argument to the plot call.
I think the following will work for you:
fig, ax = plt.subplots(nrows=3, ncols=2)
for i, feature in enumerate(list(df.columns.values)[:-1]):
# for each feature create two plots on the same row
sns.boxplot(data=df, x='survival_status_after_5yrs',y=feature, ax=ax[i, 0])
sns.violinplot(data=df, x='survival_status_after_5yrs', y=feature, ax=ax[i, 1])
plt.show()
Most seaborn plot functions have an axis kwarg, so instead of
axis[i].plot(sns.boxplot(data=df,x='survival_status_after_5yrs',y=feature))
try
sns.boxplot(data=df,x='survival_status_after_5yrs',y=feature,axis=axis[i])

Get axes from matrix

I have an animation plot, with 4 sub plots. The plots ax1 and ax2 share the same scale - hence I could plot the ax2 y-axis on the right hand side y-axes of ax1.
ax1.plot(df.pnl.as_matrix(),color='black')
ax2.plot(df.unit.as_matrix(),color='black')
axi = ax1.twinx()
axi.set_ylim(ax2.get_ylim())
Which crates the following plot:
But obviously I don't want the ax2 subplot to contain the data from df.unit.as_matrix() - as I would rather use it for something else. So my question is, how to I get the axes that ax2 creates, without actually using ax2. I.e. is there a way to derive it from the matrix df.unit.as_matrix(), and somehow use that, instead of ax2.get_ylim().
In essense what I am asking, is what method does matplotlib use to create the axes from a 2D matrix - then I could just replicate that and use ax2 subplot for something more useful.

Categories

Resources