Using this SO post, I can plot images instead of points:
def _implot(self, x, y, image, ax, zoom=1):
im = OffsetImage(image, zoom=zoom, cmap='gray_r')
ab = AnnotationBbox(im, (x, y), xycoords='data', frameon=False)
ax.add_artist(ab)
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(10, 10)
for img, x in zip(Y, X):
img = img.reshape(28, 28)
self._implot(x[0], x[1], img, ax=ax, zoom=0.5)
Above, Y
is an Nx(28*28)
matrix of flattened images and X
is an Nx2
matrix of locations. My issue is that many of these images overlap, making the figure hard to read:
I'd like to skip plotting any image if it would overlap with an existing image. How can I do this? I could manually track which (x, y) coordinates have been plotted and also account for the image width and height, but that seems brittle, and I'm hoping Matplotlib has a built-in way to check for overlapping artists.