How to add displot to sub-histogram using matplotlib [duplicate] - python

This question already has answers here:
Add density curve on the histogram
(2 answers)
Closed 8 months ago.
I have simplified code like this:
fig, axs = plt.subplots(1, 2)
axs[0].hist(x)
axs[1].hist(y)
and I need to add density curve to each plot. Anyone know reasonable simple way to do this? It could be using seaborn. Kindly help.
Funtion seaborn.displot() does not working in subplots.

You could use seaborn.histplot and pass kde parameter:
fig, axs = plt.subplots(1, 2)
sns.histplot(x, ax = axs[0], kde = True)
sns.histplot(y, ax = axs[1], kde = True)

Related

Shaded area either side of mean on line graph - matplotlib, seaborn - Python [duplicate]

This question already has answers here:
Why am I getting a line shadow in a seaborn line plot?
(2 answers)
Seaborn lineplot using median instead of mean
(2 answers)
Closed 10 months ago.
`def comparison_visuals(df_new):
matplotlib.rc_file_defaults()
ax1 = sns.set_style(style=None, rc=None )
fig, ax1 = plt.subplots(figsize=(12,6))
sns.lineplot(data = df_new, x='Date', y=
(df_new['Transfer_fee'])/1000000, marker='o', sort = False,
ax=ax1)
ax2 = ax1.twinx()
from matplotlib.ticker import FormatStrFormatter
ax1.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
sns.lineplot(data = df_new, x='Date', y='Inflation', alpha=0.5,
ax=ax2)
from matplotlib.ticker import FormatStrFormatter
ax2.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
comparison_visuals(df_new)'
(Edited to paste code above)
Can anyone tell me what the shaded area represents on my line graph (see screenshot)? The middle line represents the mean. I haven't specifically added it. I would like to know first what it is and secondly how to remove it (I might chose to keep it if I find out what it represents and it adds value to my visualisation).
Any related answers I've come across don't del with this directly. Thansk in advance.
Screenshot of line graph

Use subplots for pie char and bar char pandas [duplicate]

This question already has answers here:
How to plot multiple dataframes in subplots
(10 answers)
Plotting Pandas into subplots
(1 answer)
Closed 1 year ago.
I'm trying to show two graphs next to each other. pie char and bar char.
my code is:
import matplotlib.pyplot as plt
fig , ax = plt.subplots(nrows = 1, ncols = 2)
df['column'].value_counts().plot.pie()
df['column'].value_counts().plot.bar()
plt.show()
this is the output:
can someone help me please?
Pass the subplots to the plot commands:
fig , ax = plt.subplots(nrows = 1, ncols = 2)
df['column'].value_counts().plot.pie(ax=ax[0])
df['column'].value_counts().plot.bar(ax=ax[1])
plt.show()

Plot two catplots in same figure using Python [duplicate]

This question already has an answer here:
How to plot multiple seaborn catplots on a 2x2 grid?
(1 answer)
Closed 3 years ago.
I am trying to plot two catplots in same figure. I tried to use subplot() function but no result.
Here is the code I am using for ploting one catplot at a time.
First Catplot
fig, axs =plt.subplots(2,1)
sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
kind ='bar',
height = 8 , aspect= 1.5,ax=axs[0])
Second Catplot
Here is a second catplot am plotting :
sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
kind ='bar',
height = 8 , aspect= 1.5,ax=axs[1])
Goal:
plot to catplots in the same figure ( one next to the other)
I tried something like this (with subplot), but it does not work.
fig, axs =plt.subplots(2,1)
sns.catplot(x = 'day',y = 'count',data=day_of_month_count,
kind ='bar',
height = 8 , aspect= 1.5,ax=axs[0])
sns.catplot(x = 'month',y = 'count',data=month_of_the_year_count,
kind ='bar',
height = 8 , aspect= 1.5,ax=axs[1])
Any alternatives? solutions?
Firstly, next to each other would require 1 row 2 columns. Then the following method works normally as expected.
Here you have to close/hide the axis returned by the catplot. This can be done using the correct index and plt.close. The indexing/numbering of figures starts from 0. Here is a sample answer.
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
fig, axs = plt.subplots(1,2)
sns.catplot(x="time", y="pulse", kind ='bar', data=exercise, ax=axs[0])
sns.catplot(x="time", y="pulse", kind ='bar', data=exercise, ax=axs[1])
plt.close(2)
plt.close(3)
fig.tight_layout()

Seaborn Jointplot Change Figsize [duplicate]

This question already has answers here:
How to plot non-square Seaborn jointplot or JointGrid
(3 answers)
Closed 3 years ago.
I am using Jupyter Notebook and want a full width jointplot figure.
I cant seem to get it working though.
g = sns.jointplot(x="pos", y="diff", data=plot_data)
plt.figure(figsize=(16, 6))
doesn't change the size at all.
fig, ax = plt.subplots(figsize=(16, 6))
g = sns.jointplot(ax=ax, x="pos", y="diff", data=plot_data)
Throws an error.
Use the height parameter in the jointplot function to set the size of the figure(it will be square). Refer to official docs: seaborn.pydata.org/generated/seaborn.jointplot.html

How to show colorbar on each individual matshow subplot [duplicate]

This question already has answers here:
matplotlib colorbar in each subplot
(5 answers)
Closed 3 years ago.
I am creating a (10,7) subplot of multiple different gridded fields. The following code is what is being currently used:
fig, axes = plt.subplots(nrows=10, ncols=7, figsize=(18, 16), dpi= 100,
facecolor='w', edgecolor='k')
titles = ['Z1','Z2','Z3','ZDR1','ZDR2','ZDR3','Dist']
for i in range(0,10):
z = 1*10+i
for j in range(0,7):
aa = axes[i,j].matshow(alldata_sim[z,:,:,j], cmap='jet')
fig.colorbar(aa)
axes[0,j].set_title(titles[j])
axes[i,j].get_xaxis().set_visible(False)
axes[i,j].get_yaxis().set_ticks([])
axes[i,0].set_ylabel(allgauge_sim[z])
Which produces the following figure:
Figure1
The question is: how do I get the colorbars to be on the right-hand side of each respective individual subplot?
maybe try changing
fig.colorbar(aa)
to
fig.colorbar(aa,ax=axes[i,j])
Hope it helps!

Categories

Resources