0

I print a seaborn line plot using the below code.

timeplot = sns.lineplot(x='YearMonth', y='count', data=delivered_by_month)

This works fine, but x axis labels are overlapping, so I wanted to rotate them.

So I added an additional line like this after the above line.

timeplot.set_xticklabels(labels=timeplot.get_xticklabels(), rotation=90, ha="right")

Now X-axis labels just disappeared. I confirmed that all labels got cleared by running the below code.

for m in timeplot.get_xticklabels():
  print (m)

For this, I got an output like below.

Text(0, 0, '')
Text(1, 0, '')
Text(2, 0, '')
Text(3, 0, '')
Text(4, 0, '')

If I print labels before the rotation code, I get a proper output like below.

Text(0, 0, '2010-10')
Text(1, 0, '2010-11')
Text(2, 0, '2010-12')
Text(3, 0, '2010-2')
Text(4, 0, '2010-3')

Any input will be helpful.

krisho
  • 1,004
  • 7
  • 26

1 Answers1

2

I've just had the same issue and this helped me: https://stackoverflow.com/a/41131528/14396570

Apparently, timeplot.get_xticklabels() is empty until the graph is drawn, so you have to call plt.draw() before setting x tick labels, like so:

timeplot = sns.lineplot(x='YearMonth', y='count', data=delivered_by_month)
plt.draw()
timeplot.set_xticklabels(labels=timeplot.get_xticklabels(), rotation=90, ha="right")