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)),
                  columns=sorted((list(range(10))*N)[0:N]),
                  index=sorted((list(range(10))*N)[0:M]))


cmap = mpl.colors.ListedColormap([(.8, .8, .8, 1.0)] + [plt.cm.jet(i) for i in range(plt.cm.jet.N-1)])

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

Image

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:

ax.invert_yaxis()

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.

ax.collections[0].colorbar.ax.invert_yaxis()

inverting the y axis of the colorbar