Hatching color in contourf function.

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

Author: Fantashit

1 thought on “Hatching color in contourf function.

  1. 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.

    # Content as in example
    # ------------------------------
    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',
    )
    
    plt.title('Before')
    plt.savefig("mpl_test_before.jpeg")
    
    # ------------------------------
    # New bit here that handles changing the color of hatches
    colors = ['maroon', 'red', 'darkorange', 'gold', 'forestgreen',
              'darkturquoise', 'dodgerblue', 'darkviolet']
    # For each level, we set the color of its hatch 
    for i, collection in enumerate(cs.collections):
        collection.set_edgecolor(colors[i %% len(colors)])
    # Doing this also colors in the box around each level
    # We can remove the colored line around the levels by setting the linewidth to 0
    for collection in cs.collections:
        collection.set_linewidth(0.)
    # ------------------------------
    
    plt.title('After')
    plt.savefig("mpl_test_after.jpeg")

    mpl_test_before
    mpl_test_after

Comments are closed.