3

I want to change the density of hatch lines using Matplotlib v2.2.2 and the contourf() function (specifically, I want to increase the density). I have read that you can increase the density of a hatch by increasing how many times you use the hatch figure (e.g. replace x with xx). However, that change is having no effect for me. My backend is Qt5Agg, and I'm using Python v3.6.4.

MWE:

import matplotlib.pyplot as plt
import numpy             as np

def main():
    x = np.arange( 0, 1.01, 0.01 )
    X, Y = np.meshgrid( x, x )
    Z = X + Y

    fig, (ax1, ax2) = plt.subplots( 1, 2 )
    ax1.contourf( X, Y, Z, [1,2], colors='none', hatches='x' )
    ax2.contourf( X, Y, Z, [1,2], colors='none', hatches='xx' )

    plt.show()

main()

which produces the output

hatch density example

Possible Duplicates:

This question is 7 years old and requires defining a custom class. Is this still the best option?

This question is basically exactly what I'm asking, but the MWE was a bit complicated, and didn't attract any answers.

David M.
  • 381
  • 3
  • 10

1 Answers1

9

It's in general no problem to make the hatching more dense. This is indeed done by repeating the hatching pattern. E.g. /, //, ///.

Here, you have two contour regions/levels. Hence you need two hatches.

import matplotlib.pyplot as plt
import numpy             as np

def main():
    x = np.arange( 0, 1.01, 0.01 )
    X, Y = np.meshgrid( x, x )
    Z = X + Y

    fig, (ax1, ax2) = plt.subplots( 1, 2 )
    ax1.contourf( X, Y, Z, [1,2], colors='none', hatches=['/',None] )
    ax2.contourf( X, Y, Z, [1,2], colors='none', hatches=['//',None] )

    plt.show()

main()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712