FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: Horizontal colorbars drawn incorrectly with hatches · Issue #23456 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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

Expected outcome

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

Activity

  1. changed the title [-][Bug]: [/-] [+][Bug]: Horizontal colorbars drawn incorrectly with hatches[/+] on Jul 20, 2022
  2. jklymak commented on Jul 20, 2022

    Member

    Did this work before and we broke it, or did it never work? There were some changes to colorbars, but I'm not sure why they would cause this to happen. Thanks.

  3. hCraker commented on Jul 21, 2022

    Author

    I'm not sure but I can try running my code using older versions of matplotlib. What version do you think might be the culprit?

  4. jklymak commented on Jul 21, 2022

    Member

    Try 3.4.2. But if you don't know if this ever worked it probably never did. My guess would be there is a typo in the code that makes the hatch. Maybe east to fix if you grep hatch in the colorbar.py

  5. hCraker commented on Jul 21, 2022

    Author

    The bug still happens in v3.4.2. I did find a solution that fixes the issue without messing up vertical colorbars with hatches. I works in both 3.5.2 and 3.4.2
    In _add_solids_patches, change the indices for creating xy from

        def _add_solids_patches(self, X, Y, C, mappable):
            hatches = mappable.hatches * len(C)  # Have enough hatches.
            patches = []
            for i in range(len(X) - 1):
               xy = np.array([[X[i, 0], Y[i, 0]],
                               [X[i, 1], Y[i, 0]],
                               [X[i + 1, 1], Y[i + 1, 0]],
                               [X[i + 1, 0], Y[i + 1, 1]]])

    To this

        def _add_solids_patches(self, X, Y, C, mappable):
            hatches = mappable.hatches * len(C)  # Have enough hatches.
            patches = []
            for i in range(len(X) - 1):
               xy = np.array([[X[i, 0], Y[i, 1]],  # this is the only line that needs a change
                               [X[i, 1], Y[i, 0]],
                               [X[i + 1, 1], Y[i + 1, 0]],
                               [X[i + 1, 0], Y[i + 1, 1]]])
  6. jklymak commented on Jul 21, 2022

    Member

    Yep, that looks like an indexing bug... If you have a patch, feel free to try a pull request. Maybe also add a test to test_colorbar.py? It would require an image comparison. https://matplotlib.org/stable/devel/index.html for more guidance, also feel free to ask at https://gitter.im/matplotlib/matplotlib

  7. oscargus commented on Aug 20, 2022

    Member

    I was about to implement this, but I found another issue as well. A slightly different setting gave this (after the patch above was introduced):

    Clearly the colors and hatches are off by one.

    That seems to be a problem for vertical as well (and maybe easier to see there):

    The same thing can be seen in the original picture although not as obvious there. Rather, there are eight different levels in both the contour and the colorbar, but the left extended level in the colorbar is not matching the darkest in the contour. However, of the three lightest/right-most three levels in the contour, only two of them are in the colorbar.

    Not sure if it is worthwhile doing a patch to get the rectangle correct as it probably doesn't make sense to add a test image since the colorbar in itself doesn't match the contour-levels.

  8. oscargus commented on Aug 20, 2022

    Member

    The problem is related to extend. Without it, it is all good.

  9. oscargus commented on Aug 20, 2022

    Member

    So, _add_solids_patches duplicates the hatches:

    hatches = mappable.hatches * len(C) # Have enough hatches.

    and then picks one at a time starting from 0:
    hatch=hatches[i], linewidth=0,

    The extend patches picks the first and last hatch pattern of the original list:

    hatch=hatches[0], clip_on=False,

    transform=self.ax.transAxes, hatch=hatches[-1], clip_on=False,

  10. added this to the v3.6.1 milestone on Oct 6, 2022
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL