plot with polycollection disappears when polygons get too small

I'm using a PolyCollection to plot data of various sizes. Sometimes the polygons are very small. If they are too small, they don't get plotted at all. I would expect the outline at least to show up so you'd have an idea that some data is there. Is there a a setting to control this?

Here's some code to reproduce the problem:

import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

verts = []

for i in range(5):
    w = 0.5 * 10**(-i)

    xs = [i - w, i - w, i + w, i + w, i - w]
    ys = [-w, w, w, -w, -w]

    verts.append(list(zip(xs, ys)))


fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(-1, 6)
ax.set_ylim(-2, 2)

poly = PolyCollection(verts, lw=5, edgecolor='black', facecolor='gray')
ax.add_collection(poly)

plt.show()

Here's the output. Notice that only three polygons are visible, whereas there should be five.

plot_example

Edit: I understand I could zoom in to see the others, but I was hoping to see a dot or the outline or something without doing this.

1 answer

  • answered 2018-11-13 06:51 Patol75

    Zooming in your plotting window, you would notice that your two remaining polygons are being plotted. They are just too small for you to see them. One way to be convinced of this is to replace

    ax.set_xlim(-1, 6)
    ax.set_ylim(-2, 2)
    

    by

    ax.set_xlim(1e-1, 1e1)
    ax.set_ylim(1e-5, 1e0)
    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.set_aspect('equal')
    

    Your five polygons are now visible, but on the downside the log scale restrains you to the positive side of the axes. enter image description here