Skip to content

[Bug]: Horizontal colorbars drawn incorrectly with hatches #23456

Closed
@hCraker

Description

@hCraker

Bug summary

When specifying hatches for contourf, the colorbar is drawn correctly for vertical colorbars but not for horizontal ones. The Hatches and colorfill only fill the lower diagonal of the colorbar boxes.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.pyplot as plt
import numpy as np

# invent some numbers, turning the x and y arrays into simple
# 2d arrays, which make combining them together easier.
x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)

# we no longer need x and y to be 2 dimensional, so flatten them.
x, y = x.flatten(), y.flatten()

fig1, ax1 = plt.subplots()
cs = ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
                  cmap='gray', extend='both', alpha=0.5)

fig1.colorbar(cs, orientation='horizontal')
plt.show()

Actual outcome

actual_colobar

Expected outcome

expected_colorbar

Additional information

The bug occurs when the colorbar is horizontal and the contourf call uses hatches. Changing the kwargs extend, cmap, colors, alpha, etc when calling contourf does not resolve the bug.

The bug does not occur when the colorbar is vertical with hatches nor when the colorbar is horizontal and no hatches are used.

Potential Solution:
In colorbar.py, change line 637 to the following:
xy = np.array([[X[i, 0], Y[i, 1]],

This doesn't cause issues when hatches are not used. It also doesn't cause problems with vertical colorbars.

Operating system

macOS Big Sur Version 11.3.1 with Intel Silicon

Matplotlib Version

3.5.2

Matplotlib Backend

module://backend_interagg

Python version

3.10.5

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