16

So I have a heatmap created using seaborn

revels = rd.pivot("Flavour", "Packet number", "Contents")

ax = sns.heatmap(revels, annot=True, fmt="d", linewidths=0.4, cmap="YlOrRd")

plt.show()

which producesenter image description here

There are two things which I want to do however, which I can't for the life of me work out how to do, despite consulting seaborn's helpfile (http://seaborn.pydata.org/generated/seaborn.heatmap.html)

The thing I want to do is order the flavours differently. Despite the order being inputted in the textfile as orange, toffee, choc, malt, raisin, coffee, it doesn't generate this way when plotting. I tried to edit the yticklabs but that just edits the labels as opposed to moving the data with it.

Thanks for any help

PS data looks like this:

Packet number,Flavour,Contents
1,orange,4
2,orange,3
3,orange,2
...
1,toffee,4
2,toffee,3
3,toffee,3
...
etc.
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
pow
  • 415
  • 3
  • 8
  • 18
  • You created a df "revels", have you tried sorting by Flavour? – Andrew L Apr 29 '17 at 10:23
  • Be aware that this Q&A site is designed to have one question per question. Sorting pandas data is completely different from changing a matplotlib colorbar. Since the first question already got an answer here, leave that one in and remove the second. If you cannot find out how to set the colorbarlabels you may ask a new question about it. – ImportanceOfBeingErnest Apr 29 '17 at 10:58
  • Yes, I would recommend breaking the second question out separately as I'm sure @ImportanceOfBeingErnest has the answer! His matplotlib skills are far superior to mine. – Andrew L Apr 29 '17 at 11:40
  • The question on how to produce discrete ticks in a colorbar has a [good answer here](http://stackoverflow.com/a/28730546/4124317). I therefor removed that part from the question. (If you have problems implementing the linked answer, you may ask a new question about it.) – ImportanceOfBeingErnest Apr 29 '17 at 12:19

2 Answers2

22

As for the first question, you'll need to perform the sort with your data. Your first line creates a dataframe which you can then use the sortlevel method to sort.

Create dataframe:

revels = rd.pivot("Flavour", "Packet number", "Contents")

Because you're using Flavour as the index, use the sortlevel method before adding to heatmap:

revels.sort_index(level=0, ascending=True, inplace=True)

This will change the order of your data in the heatmap.

This obviously provides ascending/descending sorting but if you need a custom sort order, try this link: Custom sorting in pandas dataframe.

Custom Sorting Example

revels.index = pd.CategoricalIndex(revels.index, categories= ["orange", "toffee", "chocolate", "malteser", "raisin", "coffee"])
revels.sort_index(level=0, inplace=True)
Andrew L
  • 6,618
  • 3
  • 26
  • 30
  • i followed your link and used the code revels['Flavour'] = pd.Categorical(revels['Flavour'], ["orange", "toffee", "chocolate", "malteser", "raisin", "coffee"]) but i get KeyError: 'Flavour' any idea? – pow Apr 29 '17 at 11:12
  • Yes- you're running into trouble because revels is setting 'Flavour' to index (and dropping the column). I'll update answer. – Andrew L Apr 29 '17 at 11:20
  • Dataframe object has no attribute sortlevel, need to use sort_index – Adam Ryason Jan 23 '20 at 20:08
4

The above example works, but you have to replace sortlevel with sort_index

i.e. revels.sortlevel(level=0, ascending=True, inplace=True) becomes revels.sort_index(axis=0, ascending=True, inplace=True)

bracoo
  • 151
  • 2