Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading