Seaborn plot converts data values from int to float

Trying to plot the following:

import seaborn as sns
fligths = sns.load_dataset('flights')
flights['year'].dtype
[1] dtype('int64')

sns.relplot(data=flights, x='year', y='passengers', hue='month', kind='line')

Outputs the following:
incorrect graph

As we can see, the x values are incorrectly converted to floats, even if its type is int. How can I correct this?

>Solution :

Cannot tell you what is causing this, but you can use matplotlib.ticker to convert them back to integers.

First import packages:

from  matplotlib.ticker import FuncFormatter
import seaborn as sns

Then modify your code as follows:


fligths = sns.load_dataset('flights')
sns.relplot(data=flights, x='year', y='passengers', hue='month', kind='line')

#add this ↓
plt.gca().xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
plt.show() 

see this question for more info

Leave a Reply