1

why are the y-axis labels lost when specifying: ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)

%pylab inline
import pandas as pd
import seaborn as sns; sns.set()

df = pd.DataFrame({'dt':['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04'], 'category':['a', 'b', 'a', 'b'], 'foo':[10, 15, 8, 13], 'bar':[12, 8, 5, 18]})
df['dt'] = pd.to_datetime(df['dt'])

plt.figure()
ax0 = plt.subplot(211)
ax = sns.lineplot(x='dt', y='foo', data=df, hue='category', ax=ax0)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=16)
plt.show()

edit

plt.yticks(fontsize=16) works just fine though. However, I must use theo object-oriented API as I use this in a more complex plot. Otherwise, plt refers to the wrong axis.

Georg Heiler
  • 16,916
  • 36
  • 162
  • 292

2 Answers2

2

Try to replace it with:

ax.set_yticklabels(ax.get_yticks(), fontsize=16)

A short explaination why ax.set_yticklabels(ax.get_yticklabels(), fontsize=16) gives you empty y labels:

If you print (ax.get_yticklabels()[:]), it gives you:

[Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, ''), Text(1, 0, '')]

Notice that the last item in every sublist is an empty string, they will be regarded as your ylabel. It is thus equivalent to ax.set_yticklabels(labels=[], fontsize=16)

tianlinhe
  • 991
  • 1
  • 6
  • 15
0

You can just use tick_params function and specify which axis. For both axis, use axis='both'

ax.tick_params(axis='y', labelsize=16)
Sheldore
  • 37,862
  • 7
  • 57
  • 71