-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[ENH]: Different edgecolor and hatch color in bar plot #26074
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
Comments
There is partial support for this in the code base. The backends support a separate hatch color and collections store the hatch color as If anyone wants to give it a go, please remember that it cannot break the current behavior. I assume adding a keyword argument to bar which is None by default and is only used when set is the way to go. This will mean that There is also a (long-term) idea to completely rework the hatch handling by introducing "hatch styles", but no one has had the time to do this. |
Thank you for the hint! It has helped me to find the following workaround that meets my requirements. I am unsure whether to leave this issue open in case someone wants to implement a "proper" solution. import matplotlib.pyplot as plt
import numpy as np
width = 0.35
x = np.arange(4) + 1
y_red = [1, 3, 1, 4]
y_blue = [2, 2, 4, 1]
collection = plt.bar(x - 0.5 * width, y_red, width, label="Height of red bars", hatch="////", facecolor=(0, 0, 0, 0), edgecolor="black")
for patch in collection.patches:
patch._hatch_color = (1.0, 0.0, 0.0, 1.0)
collection = plt.bar(x + 0.5 * width, y_blue, width, label="Height of blue bars", hatch=r"\\", facecolor=(0, 0, 0, 0), edgecolor="black")
for patch in collection.patches:
patch._hatch_color = (0.0, 0.0, 1.0, 1.0)
plt.xticks(x)
plt.yticks([0, 1, 2, 3, 4])
plt.legend()
plt.savefig("hatch.png")
plt.show() |
Um, if we do this (and I'm very pro, edit: give or take a proper hatch API), I think it should be a keyword on patch and propagated up |
What is a logic behind this code? matplotlib/lib/matplotlib/patches.py Lines 315 to 323 in 0b8bd96
'set_hatch_color = False' allows to change simultaneously hatchcolor through rcParams and edgecolor. |
I have worked on this issue. Please review it
The code can be edited in the following way to provide a different hatchcolor and edgecolor |
I got the following error with your latest commit at the time, but it seems that new commits are still being made.
|
Hi Thomas @99991 , can you try again Issue26074 branch and try. I already resolved this issue before pushing in the first commit. In the newer commits, I am just solving the flaky issues (like empty line with extra whitespace) . |
@Vashesh08 Great PR, reviewers should review this code, and this function should be merged into matplotlib. |
Problem
Colored edges look ugly:
I want bars with black edges and colored hatch:
My current workaround is to draw the bars twice, once with colored hatch and then a second time only with black edges:
This workaround is not optimal because the legend is wrong.
Proposed solution
I propose a
hatchcolor
parameter which is independent ofedgecolor
.The text was updated successfully, but these errors were encountered: