3

When creating a matplotlib colorbar, it is possible to set drawedges to True which separates the colors of the colorbar with black lines. However, when the colorbar is extended using extend='both', the black lines at the extremities do not show up. Is that a bug? Is there a possibility to draw those edges otherwise?

Here is the code:

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt  
from matplotlib.colors import from_levels_and_colors

my_cmap = mpl.cm.viridis
bounds = np.arange(10)
nb_colors = len(bounds) + 1
colors = my_cmap(np.linspace(100, 255, nb_colors).astype(int))
my_cmap, my_norm = from_levels_and_colors(bounds, colors, extend='both')

plt.figure(figsize=(5, 1))
ax = plt.subplot(111)
cbar = mpl.colorbar.ColorbarBase(ax, cmap=my_cmap, norm=my_norm, orientation='horizontal', drawedges=True)
plt.subplots_adjust(left=0.05, bottom=0.4, right=0.95, top=0.9)
plt.show()

and the figure it gives:

enter image description here

TVG
  • 370
  • 2
  • 11
  • I see you have posted on github and have been identified as a bug. Congratulations, you have been identified as a bug. Hopefully it will be addressed in the next version. +1 – r-beginners Apr 21 '22 at 13:27

1 Answers1

2

I looked into it from your question and found a way to change the color of the border and vertical lines of the color bar. I used that to change them to red. The result I got was that the extended outline was red, so my guess is that I just pulled the short sides of the normal color bar rectangle to the left and right. I found this response helpful.

cbar.outline.set_edgecolor('red')
cbar.dividers.set_color('red')

enter image description here

So I think the only way to do this is to add vertical lines.

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt  
from matplotlib.colors import from_levels_and_colors

my_cmap = mpl.cm.viridis
bounds = np.arange(10)
nb_colors = len(bounds) + 1
colors = my_cmap(np.linspace(100, 255, nb_colors).astype(int))
my_cmap, my_norm = from_levels_and_colors(bounds, colors, extend='both')

plt.figure(figsize=(6, 2))
ax = plt.subplot(111)
cbar = mpl.colorbar.ColorbarBase(ax, cmap=my_cmap, norm=my_norm, orientation='horizontal', drawedges=True)
# update
cbar.outline.set_edgecolor('red')
cbar.dividers.set_color('red')
plt.axvline(max(bounds), color='red', alpha=0.3, linewidth=3.5)
plt.axvline(min(bounds), color='red', alpha=0.3, linewidth=3.5)

plt.subplots_adjust(left=0.05, bottom=0.4, right=0.95, top=0.9)
plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Many thanks! Adding vertical lines works fine. However, I think the normal behavior of `drawedges=True` should be to plot those edges. It looks like a bug. Do you know where it is possible to report it? – TVG Apr 20 '22 at 14:10
  • 1
    My personal opinion is that it is just pulling the short side to the left or right, which seems to be the spec. [Here](https://github.com/matplotlib/matplotlib/issues) is the place to report it. You can also ask questions here.t. – r-beginners Apr 20 '22 at 14:17