Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't plot dual axis using seaborn

I had an issue when I was trying to plot dual axis plot using seaborn in a jupyter notebook Important note: The code worked very well with Python 2.

After upgrading to Python 3 with anaconda, I get the foolowing error message:

/Users/enyi/opt/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py:3720: UserWarning: catplot is a figure-level function and does not accept target axes. You may wish to try countplot
  warnings.warn(msg, UserWarning)

Here's the output image of my code:

Output of my code

My code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv('tips.csv')

fig, ax = plt.subplots(1,2,figsize = (10,5))

sns.catplot(x='sex', hue = 'group', data= df, kind = 'count', ax=ax[0])
sns.catplot(x='sex', y='conversion',hue = 'group', data= df, kind = 'bar',ax=ax[2])

plt.show()
like image 696
Wenyi Yan Avatar asked Jun 13 '26 21:06

Wenyi Yan


1 Answers

I don't see how your code could have worked with Python2, but that's beside the point. The error message clearly tells you that catplot does not take an ax= argument. If you want to plot on subplots, you have to use the underlying plotting function (in the first case, countplot as the error suggests)

fig, ax = plt.subplots(1,2,figsize = (10,5))
sns.countplot(x='sex', hue = 'group', data= df, ax=ax[0])
sns.barplot(x='sex', y='conversion',hue = 'group', data= df,ax=ax[1])
like image 173
Diziet Asahi Avatar answered Jun 15 '26 09:06

Diziet Asahi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!