Reputation: 109
I'm trying to plot two seaborn catplots side by side and not one on top of the other. In order to not share the x axis I had to create a FacetGrid instance and use the '.map_dataframe' function to produce two plots but it has produced them one above the other. (I was able to create the plots side by side just by using 'sns.catplot' however the 'sharex=False' argument didn't work and I ended up with all of the values across both x-axes.)
Here is my code:
grid = sns.FacetGrid(data = pcd_data, col = 'Multi Storey Property')
grid.map_dataframe(sns.catplot, kind = 'bar', x="House Type ",
y="Percentage", height = 6, hue="Last Balance Range Sort",
palette = 'pastel', alpha = 0.6)
plt.close(1)
plt.show()
And it has produced the following:
Any help would be greatly appreciated.
I'm using Python 3.8.3 Anaconda. Conda 4.9.1
Thanks
Upvotes: 1
Views: 1497
Reputation: 46898
You can provide the columns to catplot
, using an example dataset:
import seaborn as sns
data = sns.load_dataset("exercise")
data = data.groupby(["diet","kind","time"])["pulse"].agg("mean").reset_index()
data.head()
diet kind time pulse
0 no fat rest 1 min 91.8
1 no fat rest 15 min 92.2
2 no fat rest 30 min 93.0
3 no fat walking 1 min 95.6
4 no fat walking 15 min 98.6
sns.catplot(x='kind',y='pulse',hue='time',kind='bar',col='diet',data=data)
If you have different x-axis, you can make the x label a string:
data = data[~((data.diet=="no fat") & (data.kind=="rest"))]
data['kind'] = data['kind'].astype('str')
And similar to @Alex solution, you call facet and map barplot onto it, the first argument is for x-axis, 2nd y-axis and 3rd hue:
g = sns.FacetGrid(data, col="diet",sharex=False,sharey=False)
g.map(sns.barplot, "kind", "pulse",'time',hue_order=data.time.unique())
Upvotes: 4
Reputation: 7045
You are using catplot
which returns a FacetGrid
. Instead you should use sns.barplot
directly:
grid = sns.FacetGrid(data=pcd_data, col="Multi Storey Property")
grid.map_dataframe(
sns.barplot,
x="House Type ",
y="Percentage",
hue="Last Balance Range Sort",
palette="pastel",
alpha=0.6,
)
grid.set_axis_labels("House Type", "Percentage")
grid.add_legend()
Upvotes: 2