It is possible to do hatching of regions using contourf function in matplotlib. Below is a working example I took from the matplotlib website. What I would like to do is to change the color of the hatching if it is possible. I tried to specify edgecolor, color and facecolor but it does not seem to work.. Thanks
import matplotlib.pyplot as plt
import numpy as np
# invent some numbers, turning the x and y arrays into simple
# 2d arrays, which make combining them together easier.
x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)
# we no longer need x and y to be 2 dimensional, so flatten them.
x, y = x.flatten(), y.flatten()
# a plot of hatches without color with a legend
plt.figure()
n_levels = 6
plt.contour(x, y, z, n_levels, colors='black', linestyles='-')
cs = plt.contourf(x, y, z, n_levels, colors='none',
hatches=['.', '/', '\\', None, '\\\\', '*'],
extend='lower', edgecolor = "r", facecolor="blue", color = "red"
)
# create a legend for the contour set
artists, labels = cs.legend_elements()
plt.legend(artists, labels, handleheight=2)
plt.savefig("mpl_test.jpeg")
Funnily enough, I was also trying to do coloured hatching with contourf at the same time as @HenryDayHall, and he posted to this 3 year old issue while I was composing my own reply.
Here is how I got contourf with different colour hatching for each level. To do it, you run contourf once, and then loop over all the levels in the output to change the colour of the hatches. This means you can have a different colour for the hatches in each level.