The default cmap is sns.cm.rocket. To reverse it set cmap to sns.cm.rocket_r
Using your code:
cmap = sns.cm.rocket_r
ax = sns.heatmap(cm_prob,
annot=False,
fmt=".3f",
xticklabels=print_categories,
yticklabels=print_categories,
vmin=-0.05,
cmap = cmap)
Answer from Ben on Stack OverflowThe default cmap is sns.cm.rocket. To reverse it set cmap to sns.cm.rocket_r
Using your code:
cmap = sns.cm.rocket_r
ax = sns.heatmap(cm_prob,
annot=False,
fmt=".3f",
xticklabels=print_categories,
yticklabels=print_categories,
vmin=-0.05,
cmap = cmap)
To expand on Ben's answer, you can do this with most if not any color map.
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
X = np.random.random((4, 4))
sns.heatmap(X,cmap="Blues")
plt.show()
sns.heatmap(X,cmap="Blues_r")
plt.show()
sns.heatmap(X,cmap="YlGnBu")
plt.show()
sns.heatmap(X,cmap="YlGnBu_r")
plt.show()

python - How to reverse colorscales in seaborn heatmap - Stack Overflow
How do I reverse the direction of the color shading in seaborn barplot ?
python - How does cmap work in seaborn and how to change binning? - Stack Overflow
python 3.x - create custom cmap for seaborn heatmap - Stack Overflow
You can reverse all of the matplotlib colormaps by appending _r to the name, i.e., plt.cm.coolwarm vs plt.cm.coolwarm_r.
I believe seaborn uses a cubehelix colormap by default.
So you'd do:
from matplotlib import pyplot
import seaborn as sns
colormap = pyplot.cm.cubehelix_r
flights = sns.load_dataset('flights').pivot("month", "year", "passengers")
sns.heatmap(flights, cmap=colormap)
There is no need to build a separate reverse function for heatmap.
Just use cmap = 'magma_r'. The default setting is magma and thus we just append the '_r' for reverse.
Expected - Getting this output in Jupyter Notebook. This is what I want.
https://i.imgur.com/lUgNDIU.png
I'm getting this when I run the same python file from the Terminal. But I want the opposite direction of colors.
https://i.imgur.com/9pDQtKL.png
This is my code :
https://pastebin.com/KFCkG6Qw
You have to convert a list of colors from seaborn palette to color map of matplolib (thx to @RafaelLopes for proposed changes):
import seaborn as sns
import matplotlib.pylab as plt
import numpy as np
from matplotlib.colors import ListedColormap
# construct cmap
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
my_cmap = ListedColormap(sns.color_palette(flatui).as_hex())
N = 500
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()

Most seaborn methods to generate color palettes have an optional argument as_cmap which by default is False. You can use to directly get a Matplotlib colormap:
import seaborn as sns
import matplotlib.pylab as plt
import numpy as np
# construct cmap
my_cmap = sns.light_palette("Navy", as_cmap=True)
N = 500
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()

