I want to plot a heatmap in seaborn. My code is following:
plt.rcParams['font.size'] = 13
plt.rcParams['font.weight'] = 'bold'
my_dpi=96
fig, ax = plt.subplots(figsize=(800/my_dpi, 600/my_dpi), dpi=my_dpi, facecolor='black')
rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3)
sns.heatmap(df, cmap=rdgn, center=0.00, annot=True, fmt ='.2%', linewidths=1.3, linecolor='black', cbar=False, ax=ax)
plt.savefig('./image/image.png', dpi=96, facecolor='black')
And the result is following: enter image description here
I want the set 0 to be white, and the value >0 to be red, the values which <0 to be green. But the center in heatmap is invalid.
By the way, how to set the color unsymmetrical. Because the min value in my data is -0.34 and the maxima is 1.31. I want to set 0 to be white, -0.34 to be greenest and 1.31 to be reddest.
Changing heatmap color You can change the color of the seaborn heatmap by using the color map using the cmap attribute of the heatmap.
Reading a heat map depends on which data is represented on that particular map. Bear in mind that warmer colors indicate higher values and colder colors indicate lower values. Red is the warmest color and purple is the coldest in these maps.
To add colors to such heatmap in ranges, use scale_fill_manual() with a vector of the colors for each range. Example: R.
You can build color palettes using the function sns. color_palette() . This function can take any of the Seaborn built-in palettes (see below). You can also build your own palettes by passing in a list of colors in any valid Matplotlib format, including RGB tuples, hex color codes, or HTML color names.
center
would require something that can be centered. So instead of a palette, which is a list of colors, you will need a colormap. Seaborn provides the as_cmap
parameter for this case,
sns.diverging_palette(..., as_cmap=True)
Alternatively, you can of course use any other matplotlib colormap, or specify your custom colormap.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = np.linspace(-0.34, 1.31, 100).reshape(10,10)
fig, ax = plt.subplots()
rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3, as_cmap=True)
sns.heatmap(data, cmap=rdgn, center=0.00, annot=True, fmt ='.0%',
linewidths=1.3, linecolor='black', cbar=True, ax=ax)
plt.show()
If instead of centering the colormap you want to shift its middle point you cannot use center
. But instead a matplotlib.colors.DivergingNorm
.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import DivergingNorm
import seaborn as sns
data = np.linspace(-0.34, 1.31, 100).reshape(10,10)
fig, ax = plt.subplots()
rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3, as_cmap=True)
divnorm = DivergingNorm(vmin=data.min(), vcenter=0, vmax=data.max())
sns.heatmap(data, cmap=rdgn, norm=divnorm, annot=True, fmt ='.0%',
linewidths=1.3, linecolor='black', cbar=True, ax=ax)
plt.show()
Here, the full colormap will be squeezed in the green part and stretched in the red part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With