pythonmatplotlibcolorscolorbarspine

How can you change the spine color of a matplotlib colorbar?


I only modyfied this basic example a little bit so that the spine color of the color bar would change. Interestingly, only the color of the ticks is changing, but not that of the spine. What am I doing wrong?

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

fig, ax = plt.subplots()
im = ax.imshow(np.arange(100).reshape((10, 10)))

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

colorbar = fig.colorbar(im, cax=cax)
colorbar.ax.tick_params(axis='both', colors='#f9f2d7')
colorbar.ax.spines['bottom'].set_color('#f9f2d7')
colorbar.ax.spines['top'].set_color('#f9f2d7')
colorbar.ax.spines['right'].set_color('#f9f2d7')
colorbar.ax.spines['left'].set_color('#f9f2d7')

fig.canvas.show()

Solution

  • Instead of colorbar.ax.spines ... you can use:

    colorbar.outline.set_edgecolor('#f9f2d7')
    

    This applies to all 4 edges/spines of the colorbar.