Skip to content
  • Sponsor matplotlib/matplotlib

  • Notifications You must be signed in to change notification settings
  • Fork 7.9k

[Bug]: Colorbar with drawedges=True and extend='both' does not draw edges at extremities #22864

@tvaillantdeguelis

Description

@tvaillantdeguelis

Bug summary

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.

Code for reproduction

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()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

No response

Matplotlib Version

3.5.1

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

No response

Activity

oscargus

oscargus commented on Apr 20, 2022

@oscargus
Member

Fixed in #22865

added this to the v3.6.0 milestone on Jun 15, 2022
QuLogic

QuLogic commented on Jun 15, 2022

@QuLogic
Member

@oscargus running the example from the OP on main, I see a boundary on the left side, but not the right side.

modified the milestones: v3.6.0, v3.5.3 on Jun 15, 2022
oscargus

oscargus commented on Aug 3, 2022

@oscargus
Member

Sorry for not getting back to this until now. I can just confirm your observation. However,

In [3]: cbar.dividers.get_segments()
Out[3]: 
[array([[0., 0.],
        [0., 1.]]),
 array([[1., 0.],
        [1., 1.]]),
 array([[2., 0.],
        [2., 1.]]),
 array([[3., 0.],
        [3., 1.]]),
 array([[4., 0.],
        [4., 1.]]),
 array([[5., 0.],
        [5., 1.]]),
 array([[6., 0.],
        [6., 1.]]),
 array([[7., 0.],
        [7., 1.]]),
 array([[8., 0.],
        [8., 1.]]),
 array([[9., 0.],
        [9., 1.]])]

So the right-most line is definitely included, not just drawn,,.

For the svg and pdf output, the first and last line are about half the width of the other lines. I am wondering if maybe there is something strange with the positioning.

Looking at the png, it is clear that the right-most tick is actually positioned what seems like a bit too far to the right:
image
So maybe the line is rendered below the extend patch? I am not really sure how to get around this?

tacaswell

tacaswell commented on Aug 4, 2022

@tacaswell
Member

@QuLogic and I talked about this on our checkin today. We think the solution is to turn off clipping on the dividers.

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)
cbar.dividers.set_lw(5)
cbar.dividers.set_clip_on(False)  # drop this line and you see the lines cut in half at the edges.
plt.show()
tacaswell

tacaswell commented on Aug 4, 2022

@tacaswell
Member

and in the Agg case there is definitly an issues with low dpi and pixel snapping.

added a commit that references this issue on Aug 4, 2022
298a23f
added a commit that references this issue on Aug 9, 2022
8fa6bd5

2 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      [Bug]: Colorbar with drawedges=True and extend='both' does not draw edges at extremities · Issue #22864 · matplotlib/matplotlib