Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set x axis ticklabels in a seaborn plot

I am unable to set x axis ticklabels for a seaborn lineplot correctly.

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

df = pd.DataFrame({'a':np.random.rand(8),'b':np.random.rand(8)})
sns.set(style="darkgrid")
g = sns.lineplot(data=df)
g.set_xticklabels(['2011','2012','2013','2014','2015','2016','2017','2018'])

enter image description here

The years on the x axis are not aligning properly.

like image 388
Vinay Avatar asked Jun 14 '19 21:06

Vinay


People also ask

How do I change the orientation of the X-axis labels in Seaborn?

Use the setp() Function to Rotate Labels on on Seaborn Axes Since most seaborn plots return a matplotlib axes object, we can use the setp() function from this library. We will take the tick label values using the xtick() function and rotate them using the rotation parameter of the setp() function.

How do you change the X-axis ticks in Python?

To plot the line chart, use the plot() method. To rotate the ticks at the x-axis, use the plt. xticks() method and pass the rotation argument to it.

How do I reduce the number of ticks in Seaborn?

To decrease the density of x-ticks in Seaborn, we can use set_visible=False for odd positions.

How to set the Axis tick labels on Seaborn plots in Python?

Use the matplotlib.pyplot.xticks () and matplotlib.pyplot.yticks () Functions to Set the Axis Tick Labels on Seaborn Plots in Python These functions can be used for many purposes. If we use them without parameters, they will return the location and label values of the default tick labels on the axis.

How do I set labels in a seaborn plot?

Set axes labels Method 1: To set the axes label in the seaborn plot, we use matplotlib.axes.Axes.set () function from the matlpotlib library of python. Syntax: Axes.set (self, xlabel, ylabel, fontdict=None, labelpad=None, **kwargs)

How to use the methods for the Y-axis in Seaborn plots?

We can use the methods for the y-axis in the exact same way. These functions are used to provide custom labels for the plot. They are taken from the matplotlib library and can be used for seaborn plots. They are generally used after the set_xticks and set_yticks functions are used to specify the position of the tick labels.

What are set_xticks and set_yticks in Seaborn plots?

They are taken from the matplotlib library and can be used for seaborn plots. They are generally used after the set_xticks and set_yticks functions are used to specify the position of the tick labels.


1 Answers

Whenever you set the x-ticklabels manually, you should try to first set the corresponding ticks, and then specify the labels. In your case, therefore you should do

g = sns.lineplot(data=df)
g.set_xticks(range(len(df))) # <--- set the ticks first
g.set_xticklabels(['2011','2012','2013','2014','2015','2016','2017','2018'])

enter image description here

like image 147
Sheldore Avatar answered Sep 29 '22 05:09

Sheldore