Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 925
Chess Master Avatar asked Oct 18 '25 16:10

Chess Master


1 Answers

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

like image 176
JohanC Avatar answered Oct 20 '25 04:10

JohanC