0

I want to add an axis line to my plot. I've tried other functions to set tick but it didn't work.

warnings.filterwarnings('ignore')

xvalues = np.arange(2)

# Graph - Grouped by class, survival and sex
r = sns.factorplot(x="Sex", y="Survived", col="Pclass", data=titanicData_clean, 
                   saturation=.5, kind="bar", ci=None, size=5 ,aspect=.8)
r.fig.suptitle('class, survival and sex')
r.fig.subplots_adjust(top=0.9) 
plt.xticks(xvalues)

plt.show()

# Fix up the labels
(r.set_axis_labels('', 'Survival Rate')
     
     .set_titles("Class {col_name}")
     .set(ylim=(0, 1))
     .despine(left=True, bottom=True));

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43

1 Answers1

0

I don't know what kind of line you want. I will use the horizontal line as an example, and answer with examples of drawing individual lines on subplots and drawing a unified line. The positions of the lines are appropriate, so please replace them with your own data.

import seaborn as sns

titanicData_clean = sns.load_dataset("titanic")

# warnings.filterwarnings('ignore')

xvalues = np.arange(2)

# Graph - Grouped by class, survival and sex
r = sns.catplot(x="sex", y="survived", col="pclass", data=titanicData_clean, 
                   saturation=.5, kind="bar", ci=None, height=5 ,aspect=.8)
r.fig.suptitle('class, survival and sex')
r.fig.subplots_adjust(top=0.9) 
plt.xticks(xvalues)

ax1,ax2,ax3 = r.axes[0]

# Fix up the labels
(r.set_axis_labels('', 'Survival Rate')
     
     .set_titles("Class {col_name}")
     .set(ylim=(0, 1))
     .despine(left=True, bottom=True))

# When drawing individual horizontal lines
ax1.axhline(0.4, ls='--')
ax2.axhline(0.2, ls='--')
ax3.axhline(0.2, ls='--')

# To draw a horizontal line in a unified manner
r.map(plt.axhline, y=0.5,ls='-', c='r')

plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Since factorplot is deprecated, it has been changed to catplot. If you find my answer helpful, please click the check mark to accept the answer. – r-beginners Aug 26 '21 at 14:46