1

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.

yvrob
  • 105
  • 10
  • Potentially an artifact of the finite resolution rendered by your viewer. What happens if you zoom in on the apparent gap, or save out the result to a pdf or svg? – Paul Brodersen Aug 12 '22 at 10:51
  • 1
    Thank you for your answer. The same happens if I zoom in (even a lot), and same if I visualize the image saved in svg or pdf. – yvrob Aug 12 '22 at 11:04

1 Answers1

1

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')
Paul Brodersen
  • 11,221
  • 21
  • 38
  • 1
    Actually, if I have several polygons of different colors, the edge color would stay like the first one if I use that solution. Instead: pc.set_edgecolor('face') does the trick. – yvrob Aug 12 '22 at 12:13
  • Good suggestion, I will update the answer. – Paul Brodersen Aug 12 '22 at 12:17