Chess Master
Chess Master

Reputation: 56

Why is the hatch not showing?

I am testing out a program in matplotlib that will change the color and hatch of a bar. However, it looks like the hatch is the same color as the bar when I use the .set_color() method.

What I Tried

I looked up how to change the hatch color and found you could change it with the .set_hatch_color(), which I did not find in the documentation.

Here is the error message:

AttributeError: 'Rectangle' object has no attribute 'set_hatch_color'

Here is my code:

import matplotlib.pyplot as plt

teams = ["Brazil", "Uruguay", "Peru"]
points = [6, 3, 1]

bars = plt.bar(teams, points)

bars[0].set_color("r")
bars[0].set_hatch("/")

plt.show()

Upvotes: 4

Views: 1131

Answers (1)

JohanC
JohanC

Reputation: 80339

Call set_color sets both the inner color and the color of the lines. Hatching uses the line color, so it will be invisible if both are the same. Setting a different line color will remedy this.

import matplotlib.pyplot as plt

teams = ["Brazil", "Uruguay", "Peru"]
points = [6, 3, 1]

bars = plt.bar(teams, points)

bars[0].set_facecolor("r")
bars[0].set_edgecolor("b")
bars[0].set_hatch("/")

plt.show()

example plot

Upvotes: 3

Related Questions