1

I'm trying to change the rotation on of the x axis labels on a seaborn linechart. However when I run the below code I just get a large list as output and a graph without x labels:

sns.set(rc={'figure.figsize':(15,5)})
chart = sns.lineplot(y = top_deaths['Deaths'], x = top_deaths['Date'], data = top_deaths, hue ='Country/Region')
x = chart.get_xticklabels()
chart.set_xticklabels(x, rotation = 45)


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

Graph

The dataframe I'm referencing looks like this: DataFrame

Why is this list being produced? Why aren't they x ticks rotating,and how can I get them to rotate? Any help is greatly appreciated:).

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Stephen
  • 133
  • 1
  • 9

1 Answers1

4

You need to force seaborn to draw the plot before get the xticklabel:

sns.set(rc={'figure.figsize':(15,5)})
chart = sns.lineplot(y = top_deaths['Deaths'], x = top_deaths['Date'], data = top_deaths, hue ='Country/Region')
chart.figure.canvas.draw()
x = chart.get_xticklabels()
chart.set_xticklabels(x, rotation = 45)

I don't know why this is necessary in seaborn, but this is how it works.

jjsantoso
  • 1,586
  • 1
  • 12
  • 17
  • Thank you! This works but I still get the output of [Text(0, 0, ''), repeated. Any idea how I can prevent Text(0,0 ") from being printed? – Stephen Apr 04 '20 at 21:08
  • 1
    Put a `;` at the end `chart.set_xticklabels(x, rotation = 45);` – jjsantoso Apr 04 '20 at 21:12