Skip to content

[Bug]: no longer able to set multiple hatch colors #28990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kafitzgerald opened this issue Oct 17, 2024 · 3 comments · Fixed by #28993
Closed

[Bug]: no longer able to set multiple hatch colors #28990

kafitzgerald opened this issue Oct 17, 2024 · 3 comments · Fixed by #28993

Comments

@kafitzgerald
Copy link

Bug summary

Possible bug or maybe a usage issue following some recent updates...

In short, it used to be possible to set the hatch color for each filled contour area to a different color by iterating over the collections and then setting the edgecolor for each, but that's no longer the case with Matplotlib 3.10 and the removal of collections.

I've been trying to find a workaround after the recent updates, but am struggling a bit. Using set_edgecolors only sets a single hatch color for all filled contours (which seems unexpected based upon the docs and like a potential bug) and rcParams['hatch.color'] also just sets a single color (less suprising).

It's entirely possible I'm missing something here, but didn't see much in the docs to help either.

Code for reproduction

# The new approach I was hoping would work, but got unexpected results from

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)

cs.set_edgecolors(["blue","grey","yellow","red"])
fig1.colorbar(cs)



# The older approach that now fails with Matplotlib 3.10

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)

colors = ["blue","grey","yellow","red"]
for i, collection in enumerate(cs.collections):
    collection.set_edgecolor(colors[i % len(colors)])
fig1.colorbar(cs)

Actual outcome

Screen Shot 2024-10-17 at 4 19 18 PM

Expected outcome

Screen Shot 2024-10-17 at 4 18 22 PM

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.9.2

Matplotlib Backend

'module://matplotlib_inline.backend_inline'

Python version

3.11.7

Jupyter version

4.0.9

Installation

conda

@timhoffm
Copy link
Member

This comes in through #25247, which aggregates all contours in one Collection rather than having a separate Collection per contour. While the old approach set one color per collection, the new approach requires setting individual colors per item in the collection. Conceptually, that should be possible, and cs.set_edgecolors(["blue","grey","yellow","red"]) is indeed accepted without complaint. The issue is that the first color is used for all elements instead of cycling edgecolors.

I believe, the culprit is the special-casing for hatches in

def draw(self, renderer):
paths = self._paths
n_paths = len(paths)
if not self.filled or all(hatch is None for hatch in self.hatches):
super().draw(renderer)
return
# In presence of hatching, draw contours one at a time.
for idx in range(n_paths):
with cbook._setattr_cm(self, _paths=[paths[idx]]), self._cm_set(
hatch=self.hatches[idx % len(self.hatches)],
array=[self.get_array()[idx]],
linewidths=[self.get_linewidths()[idx % len(self.get_linewidths())]],
linestyles=[self.get_linestyles()[idx % len(self.get_linestyles())]],
):
super().draw(renderer)

one gets different edge colors when removing the hatching. The hatch case has to be extended to loop through edgecolors.

@rcomer
Copy link
Member

rcomer commented Oct 19, 2024

It seems that previously if you wanted different hatch colours you also had to accept coloured edges on the contours. Is that the desired behaviour? I’m thinking since contourf has a bespoke hatches keyword, it could also have a bespoke hatchcolors keyword. I assume the different colours could be applied at draw using rc_context, though I have not tried.

@timhoffm
Copy link
Member

Separating hatch color from edge color is a separate topic. See #26074 and the PRs linked therein.

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

Successfully merging a pull request may close this issue.

4 participants