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()
Related
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
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)
This question already has an answer here:
seaborn is not plotting within defined subplots
(1 answer)
Closed 1 year ago.
The seaborn documentation makes a distinction between figure-level and axes-level functions: https://seaborn.pydata.org/introduction.html#figure-level-and-axes-level-functions
I understand that functions like sns.boxplot can take an axis as argument, and can therefore be used within subplots.
But how about sns.relplot() ? Is there no way to put that into subplots?
More generally, is there any way to get seaborn to generate line plots within subplots?
For example, this doesn't work:
fig,ax=plt.subplots(2)
sns.relplot(x,y, ax=ax[0])
because relplot doesn't take axes as an argument.
Well that's not true. You can indeed pass axis objects to relplot. Below is a minimal answer. The key point here is to close the empty axis objects returned by relplot. You can then also use ax[0] or ax[1] to add additional curves to your individual subfigures just like you would do with matplotlib.
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2)
xdata = np.arange(50)
sns.set(style="ticks")
tips = sns.load_dataset("tips")
g1 = sns.relplot(x="total_bill", y="tip", hue="day", data=tips, ax=ax[0])
g2 = sns.relplot(x="total_bill", y="tip", hue="day", data=tips, ax=ax[1])
# Now you can add any curves to individual axis objects
ax[0].plot(xdata, xdata/5)
# You will have to close the additional empty figures returned by replot
plt.close(g1.fig)
plt.close(g2.fig)
plt.tight_layout()
You can also make line plot solely using seaborn as
import seaborn as sns
import numpy as np
x = np.linspace(0, 5, 100)
y = x**2
ax = sns.lineplot(x, y)
ax.set_xlabel('x-label')
ax.set_ylabel('y-label')
This question already has answers here:
How to add axis offset in matplotlib plot?
(2 answers)
Closed 4 years ago.
I am plotting two seaborn categorical plots (pointplot and swarmplot) on top of each other and just can't figure out how I can change the x axis position of one of them (i.e. the swarm plot in my particular case) so that instead of overlapping the plots are 'side by side' (i.e. ideally I want to have the individual data points to the right of the mean and ci).
Here's the code to produce the plot:
import seaborn as sns
# set style and font size
sns.set(style='white', rc={'figure.figsize':(6,6)}, font_scale=1.3)
# plot means as points with confidence intervals
a = sns.pointplot(x='Group',
y='RT',
data=data,
estimator= np.mean,
capsize=.2,
join=False,
color='black',
size=12)
# plot individual data points as swarmplot
b = sns.swarmplot(x='Group',
y='RT',
data=data,
size=8,
alpha=0.8)
You can feed the axis handle to the sns.
I am not sure whether this is what do you want!
import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
fig,ax =plt.subplots(1,2,figsize=(15,7))
sns.swarmplot(x="day", y="total_bill", data=tips,
ax= ax[1])
sns.pointplot(x='day',
y='total_bill',
data=tips,
estimator= np.mean,
capsize=.2,
join=False,
color='black',
size=12,ax=ax[0])
I really like the functionality of Seaborn's PairGrid. However, I haven't been able to reshape the subplots to my satisfaction. For instance, the code below will return a figure with 1 column and 2 rows, reflecting the 1 x-variable and 2 y-variables.
import seaborn as sns
tips = sns.load_dataset('tips')
g = sns.PairGrid(tips,y_vars=['tip','total_bill'],x_vars=['size'], hue='sex')
g.map(sns.regplot,x_jitter=.125)
However, it would be much preferable for me to re-orient this figure to have 2 columns and 1 row. It appears these subplots live within g.axes, but how do I pass them back to a plt.subplots(1,2) type of function?
PairGrid chooses this alignment because both plots have the same x axis. So the simplest way to get the plots in landscape would be to swap x and y:
import seaborn as sns
tips = sns.load_dataset('tips')
g = sns.PairGrid(tips,x_vars=['tip','total_bill'],y_vars=['size'], hue='sex')
g.map(sns.regplot,y_jitter=.125)
(Note that you also have to change x_jitter to y_jitter to get the same result.)
If you don't want to do that, then I think PairGrid is not the right tool for you. You can also just use two subplots and create the plots using sns.regplot:
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
male = tips[tips.sex=='Male']
female = tips[tips.sex=='Female']
with sns.color_palette(n_colors=2):
fig, axs = plt.subplots(1,2)
sns.regplot(x='size', y='tip', data=male, x_jitter=.125, ax=axs[0])
sns.regplot(x='size', y='tip', data=female, x_jitter=.125, ax=axs[0])
sns.regplot(x='size', y='total_bill', data=male, x_jitter=.125, ax=axs[1])
sns.regplot(x='size', y='total_bill', data=female, x_jitter=.125, ax=axs[1])