Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fill a region with only hatch (no background colour) in matplotlib 2.0

With the recent updates to matplotlib fill_between and hatch (see links here), hatch filled regions are no longer behaving as they did previously. That is, the region is either filled with color, and hatches are black or the region isn't filled with color and hatches aren't visible.

Here is a side-by-side comparison of plots from the same code (from this answer)

import matplotlib.pyplot as plt
import matplotlib as mpl
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()

enter image description here

is there a way to reproduce the 1.X plot in 2.X? I'm not at all familiar with back-ends but mpl.rcParams['hatch.color'] = 'b' and variations of the keywords color, edgecolor aren't helping.

Thanks in advance for helping to clear this up.

like image 663
FriskyGrub Avatar asked Jan 30 '17 05:01

FriskyGrub


People also ask

How do I make the background transparent in MatPlotLib?

If you want to make something completely transparent, simply set the alpha value of the corresponding color to 0. For plt. savefig , there is also a "lazy" option by setting the rc-parameter savefig. transparent to True , which sets the alpha of all facecolors to 0%.

How do I shade an area in MatPlotLib?

MatPlotLib with Python To shade an area parallel to X-axis, initialize two variables, y1 and y2. To add horizontal span across the axes, use axhspan() method with y1, y2, green as shade color,and alpha for transprency of the shade. To display the figure, use show() method.

How do I make a color transparent in MatPlotLib?

MatPlotLib with Python Create x_data and y_data(sin(x_data)), using numpy. Plot curve using x_data and y_data, with marker style and marker size. By changing the alpha, we can make it transparent to opaque.


1 Answers

matplotlib > 2.0.1

Since there was a lot of discussion on GitHub about hatching, there were now some changes introduced which make hatching much more intuitive.

The example from the question now works again as expected, if the facecolor argument is used instead of the color argument.

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()

enter image description here

matplotlib 2.0.0

To keep the original post, which has lead to this issue:
In matplotlib 2.0.0 you can get the old style back by using plt.style.use('classic').

##Classic!
import matplotlib.pyplot as plt
plt.style.use('classic')
plt.rcParams['hatch.color'] = 'b'

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()

Without setting the old style the following works by not setting the color to none, but instead make it transparent.

## New
import matplotlib.pyplot as plt
plt.rcParams['hatch.color'] = 'b'

plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], hatch="X", linewidth=0.0, alpha=0.0)

plt.show()

Both methods rely on setting the hatch-color via plt.rcParams['hatch.color'] = 'b'.

Unfortunately, there is currently no other way of setting the hatch color in matplotlib 2.0.0.
The matplotlib page that explains the changes states

There is no API level control of the hatch color or linewidth.

There is an issue on this topic open at github and API control may be (re)added in an upcoming version (which is indeed done with version 2.0.1).

like image 54
ImportanceOfBeingErnest Avatar answered Oct 21 '22 01:10

ImportanceOfBeingErnest