Jdog
Jdog

Reputation: 10721

matplotlib hatched fill_between without edges?

I have a region I'd like to hatch which borders on an existing plot line (of the same colour) that is dashed.

However, when I use fill_between the region to be hatched has a border drawn around it also. This border seems share properties with the lines that create the hatching so I cannot set edgecolour to "none" or set linestyle as "--" as the hatching is similarly affected.

import matplotlib.pyploy as plt
plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1],color="none",hatch="X",edgecolor="b")
plt.show()

enter image description here

In this example I'd want the diagonal line from 0,0 to 1,1 to be dashed.

Many thanks in advance.

Upvotes: 44

Views: 59200

Answers (2)

cottontail
cottontail

Reputation: 23111

Yet another way is to set the linestyle (a.k.a. ls) to be the same as that of the line plot (instead of the linewidth).

import matplotlib.pyplot as plt
plt.plot([0,1], [0,1], ls="--", c="b")
plt.fill_between([0,1], [0,1], fc="none", hatch="X", ec="b", ls="--")
#                                                            ^^^^^^^   <--- here
plt.show()

This works because linewidth and linestyle are both passed on to Axes.collections object which is created by fill_between(). Note that unlike ls=0 (as in Greg's answer), it draws the edges with the given style.

result

Upvotes: 0

Greg
Greg

Reputation: 12234

>2.0.1 Update As commented by @CatherineHolloway you need to use facecolor instead of color now:

import matplotlib.pyplot as plt
plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], facecolor="none", hatch="X", edgecolor="b", linewidth=0.0)
plt.show()

Former answer

This seems to do the trick!

import matplotlib.pyplot as plt
plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], color="none", hatch="X", edgecolor="b", linewidth=0.0)
plt.show()

import matplotlib.pyplot as plt

Upvotes: 63

Related Questions