Skip to content

[Bug]: Colorbar extend patches do not have correct alpha #22970

Closed
@ortk95

Description

@ortk95

Bug summary

When a colorbar has extend != 'neither', the triangles at the end of the colorbar always have alpha=1, even when the rest of the colorbar has a different alpha value.

Code for reproduction

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

img = np.random.rand(5, 10)
alpha = 0.5

plt.subplot(1, 2, 1)
plt.imshow(img, alpha=alpha, cmap='RdBu')
plt.colorbar(extend='both', orientation='horizontal', label='Current behaviour')

# Temporary fix to manually set the alpha of the extend patches
# doesn't look perfect, but is an improvement over the current behaviour
plt.subplot(1, 2, 2)
plt.imshow(img, alpha=alpha, cmap='RdBu')
cb = plt.colorbar(extend='both', orientation='horizontal', label='Temporary fix')
for patch in cb._extend_patches:
    patch.set_alpha(alpha)

plt.show()

Actual outcome

image

Expected outcome

The triangles at the end of the colorbar should have the same alpha as the colorbar (i.e. roughly like the temporary fix above)

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.5.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.10.4

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions