Search code examples
pythonmatplotlibcolorbar

How to make color bar ticks white and internal


I am drawing a heatmap and this is my MWE:

import matplotlib
import seaborn as sns
import numpy as np

matplotlib.rcParams.update({"figure.dpi": 96})
np.random.seed(7)
A = np.random.randint(0,100, size=(20,20))
cmap = matplotlib.cm.get_cmap('viridis').copy()
g = sns.heatmap(A, vmin=10, vmax=90, cmap=cmap, cbar_kws={})
# Get the colorbar
cbar = g.collections[0].colorbar
tick_locations = [*range(15, 86, 10)]
# Set the tick positions and labels
cbar.set_ticks(tick_locations)
cbar.set_ticklabels(tick_locations)
plt.show()

This gives me:

enter image description here

But I would like the little horizontal tick marks on the color bar to be white and inside the color bar as in:

enter image description here

How can I do that?

(What I am looking for seems to be the default in plotnine/ggplot.)


Solution

  • add this line before plt.show()...

    cbar.ax.yaxis.set_ticks_position('both')
    cbar.ax.tick_params(axis="y",direction="in", color='white')
    

    Output plot

    enter image description here