Seaborn: reverse cbar

Solution for Seaborn: reverse cbar
is Given Below:

I have a Dataframe which represents a binary matrix (0 and 1), with labels on rows and columns. I’m using the following code to print the matrix assigning each label a color:

import seaborn as sns
import matplotlib.pylab as plt
import matplotlib as mpl
import pandas as pd
import numpy as np
N = 100

M = 200
p = 0.8
df = pd.DataFrame(np.random.choice([0,1], (M,N), p=(p, 1-p)),

cmap = mpl.colors.ListedColormap([(.8, .8, .8, 1.0)] + [ for i in range(])

ax = sns.heatmap(df.apply(lambda s: (*s*(s.index+1)), mask=df.eq(0), cmap=cmap )


My issue is that the colors displayed in the cbar are in the reversed order with respect to those shown in the figure (and so are the labels). How can I reverse the colors and the labels in the cbar?
I tried:


but it also changes the structure of the plot.

Is there a solution?

You can grab the colorbar via ax.collections[0].colorbar and then call invert_yaxis() on its ax.


inverting the y axis of the colorbar