How to decouple hatch and edge color in matplotlib?
Asked Answered
C

3

34

I would like to draw a bar in matplotlib with white as fill color, red as hatch color and black as edge color. However, it looks like the edge color changes also the color of hatch. So, I am not able to decouple the color of edges and hatch. Do you have any suggestion? Thanks.

Chateau answered 3/7, 2016 at 10:20 Comment(0)
S
35

Plot bar plot twice:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Polygon

fig = plt.figure()
ax1 = fig.add_subplot(111)
# draw hatch
ax1.bar(range(1, 5), range(1, 5), color='none', edgecolor='red', hatch="/", lw=1., zorder = 0)
# draw edge
ax1.bar(range(1, 5), range(1, 5), color='none', edgecolor='k', zorder=1, lw=2.)

ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
plt.show()

enter image description here

Subbasement answered 3/7, 2016 at 10:54 Comment(6)
Thanks a lot! That is exactly what I need.Chateau
Do you have suggestions also for the legend? I still get red hatch and red edges thereChateau
Add to legend black-edged patch.Subbasement
It seems like this doesn't work with matplotlib version 2.0? When I run this code, I get a black hatch pattern.Effendi
@Effendi In Matplotlib 2.0 you can now configure the hatch colour directly with rcParamsDurwyn
This trick causes issues for stacked bar plots.Hickok
D
11

In Matplotlib 2.0 you can now configure the hatch colour directly with rcParams.

The color and width of the lines in a hatch pattern are now configurable by the rcParams hatch.color and hatch.linewidth, with defaults of black and 1 point, respectively... There is no API level control of the hatch color or linewidth.

Hatching patterns are now rendered at a consistent density, regardless of DPI. Formerly, high DPI figures would be more dense than the default, and low DPI figures would be less dense. This old behavior cannot be directly restored, but the density may be increased by repeating the hatch specifier.

Durwyn answered 8/3, 2017 at 13:35 Comment(5)
that means I can only hatch in one color per plot... this is annoying...Obelisk
seems to be in planing for 2.0.1 github.com/matplotlib/matplotlib/issues/7901Obelisk
I have matplotlib 3.1.1 and setting plt.rcParams['hatch.color'] doesn't change anything for me. It works for linewidth thoughPaulapauldron
For me, the plt.rcParams['hatch.color'] did not work because another plot set the edge color. Apparently, this has higher precedence.Koslo
Another pitfall: Patch(color='red', hatch='/') will not make the hatch visible. Use Patch(facecolor='red', hatch='/') instead!Koslo
O
0

It's possible to manually overwrite the _hatch_color. However, this is a private property, so it can change with with every new version of matplotlib.

import matplotlib as mpl
import matplotlib.pyplot as plt

f, ax = plt.subplots()

barcontainer = ax.bar(
    range(1, 5),
    range(1, 5),
    color='none',
    edgecolor='k',
    hatch="//",
    lw=1.,
    label="legend entry"
)

for bc in barcontainer:
    bc._hatch_color = mpl.colors.to_rgba("r")
    bc.stale = True

ax.legend()

Bar plot with different hatch and edge colors

Obelisk answered 15/7 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.