#๐Ÿ”’ Seaborn heatmap is sorted alphabetically

28 messages ยท Page 1 of 1 (latest)

long swallow
#

Got this code:


melted_df = pd.melt(df, id_vars=['State'], value_vars=["Rates.Age.> 64", "Rates.Age.45-64", "Rates.Age.18-45", "Rates.Age.< 18"], var_name='Age Group', value_name='Rate')

heatmap_data = melted_df.pivot(index='Age Group', columns='State', values='Rate')
sns.heatmap(heatmap_data, linewidths = 0.01)```

but it sorts the rows alphabetically, how to disable it?
kindred ferryBOT
#

@long swallow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

swift jewel
#

You can pass an order= parameter to control that

#

I'm annoyed at how hard it is to find the docs for that though

long swallow
#

is there a way to just disable the ordering?

swift jewel
#

use ID ordering

#

probably

#

presumably each row has an ID

long swallow
#

will just keep it unsorted

#

ty

#

well sorted technically

swift jewel
#

hm. Order might be wrong. That works on barplots but heatmaps don't imply they have it, which is starting to drive me nuts. And not being ablt o search github without a login is not helping

#

I suspect

#

that the heatmap doesn't sort at all

#

yeah

#

It shouldn't be sorting

#

Are you sure your input isn't sorted already?

long swallow
#

yea

#

printed it and its sorted like i want it to be

#

but when i run it in the heatmap it gets sorted

swift jewel
#

Yeah

#

Try re-sorting the dataframe before handing it in

#

with df.reindex or co

#

sns.heatmap(df[df.mean().sort_values().index], ...) to sort by mean for example

#

The dataframe is probably not sorting as you're expecting it to

kindred ferryBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.