Data order in seaborn heatmap from pivot
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)
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)