Home > Net >  Matplotlib patchcollection polygons do not touch
Matplotlib patchcollection polygons do not touch

Time:08-13

I have two polygons which are supposed to touch each other horizontally (no gap). However, when trying to plot them with PatchCollection, there seem to be a gap between the two, they are not touching:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon

fig, ax = plt.subplots()
points1 = [[ 0.,  0.],
           [10.,  0.],
           [10., 10.],
           [ 0.,  8.]]
points2 = [[10.,  0.],
           [10., 10.],
           [20., 10.],
           [18.,  0.]]
pc = PatchCollection([Polygon(points1), Polygon(points2)])
ax.add_collection(pc)
ax.autoscale_view()
plt.show()

enter image description here

Is there any reason for that in my code, and can I fix it? Thank you.

CodePudding user response:

The issue may be backend-specific as I can't reproduce it.

enter image description here

However, you are not alone, as there is a long-standing open issue on matplotlib.

A suggested solution is to set the edge color, i.e. in your case:

pc.set_edgecolor('face')
  • Related