Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill a polygon with a custom hatch in matplotlib?

I'm using python and matplotlib to create several closed polygons. I then need to fill them with a hatch, which can be done through set_hatch.

http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch.set_hatch

http://matplotlib.org/examples/pylab_examples/hatch_demo.html

Unfortunately I am working with greyscale images, and I need more hatches than provided by default - I would prefer to provide a bitmap (or some similar image) which could be tiled instead of using these hatches with varying densities.

I am open to other python libraries (pyglet, pygame, PIL, etc) however I would prefer the solution to be in python.

like image 964
Josiah Avatar asked Jun 24 '13 21:06

Josiah


People also ask

How do I fill a polygon in Matplotlib?

pyplot. fill() function is used to fill the area enclosed by polygon /curve. sequence of x,y = To traverse the boundaries of the polygon or curve defined by lists of x and y positions of its nodes. color = To change the default fill color to the desired one.

How do I draw a polygon in Matplotlib?

import matplotlib. pyplot as plt coord = [[1,1], [2,1], [2,2], [1,2], [0.5,1.5]] coord. append(coord[0]) #repeat the first point to create a 'closed loop' xs, ys = zip(*coord) #create lists of x and y values plt. figure() plt.

What is Matplotlib hatch?

Hatches can be added to most polygons in Matplotlib, including bar , fill_between , contourf , and children of Polygon . They are currently supported in the PS, PDF, SVG, OSX, and Agg backends. The WX and Cairo backends do not currently support hatching.


1 Answers

You can subclass matplotlib.hatch.Shapes and define a custom hatch based on any reference path drawn inside unit square [[-0.5, 0.5] x [-0.5, 0.5]].

Tentative:

import numpy as np
import matplotlib.hatch
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Polygon


house_path = Polygon(
    [[-0.3, -0.4], [0.3, -0.4], [0.3, 0.1], [0., 0.4], [-0.3, 0.1]],
    closed=True, fill=False).get_path()

class CustomHatch(matplotlib.hatch.Shapes):
    """
    Custom hatches defined by a path drawn inside [-0.5, 0.5] square.
    Identifier 'c'.
    """
    filled = True
    size = 1.0
    path = house_path

    def __init__(self, hatch, density):
        self.num_rows = (hatch.count('c')) * density
        self.shape_vertices = self.path.vertices
        self.shape_codes = self.path.codes
        matplotlib.hatch.Shapes.__init__(self, hatch, density)

matplotlib.hatch._hatch_types.append(CustomHatch)

fig = plt.figure()
ax = fig.add_subplot(111)

ellipse = ax.add_patch(Ellipse((0.5, 0.5), 0.3, 0.5, fill=False))
ellipse.set_hatch('c')
ellipse.set_color('red')
plt.show()

Giving:

enter image description here

like image 112
GBy Avatar answered Sep 28 '22 17:09

GBy