0

The following figure (exported as a pdf) contains overly detailed vector images that load slowly in a PDF viewer:

enter image description here

I'm trying to rasterize the outlines of the polygons displayed. However, when I use ax.set_rasterization_zorder(0), the following occurs:

enter image description here

I believe it is a backend issue but cannot figure out how to fix the problem.

import numpy as np
import matplotlib as mpl
from matplotlib.collections import PatchCollection
fig, ax = plt.subplots()
ax.imshow(dem, zorder=-2, rasterized=True)
parts = mpl.patches.Polygon(vertices)
polygon = PatchCollection(parts, zorder=-1)
ax.add_collection(polygon)
ax.set_rasterization_zorder(0)
wblack
  • 343
  • 1
  • 2
  • 11

1 Answers1

0

I was not setting rasterized=True when creating the PatchCollection objects that were drawing the polygons. When using rasterized=True without ax.set_rasterization_zorder(), each polygon would be rasterized individually, creating a large file; however, it was at least in the right location.

By rasterizing the polygon initially at the PatchCollection level and combining it with ax.set_rasterization_zorder() prior to calling plt.savefig(), all of the polygon objects were combined with the underlying DEM into one raster, creating a file manageable by Adobe PDF viewers.

import numpy as np
import matplotlib as mpl
from matplotlib.collections import PatchCollection
fig, ax = plt.subplots()
ax.imshow(dem, zorder=-2, rasterized=True)
parts = mpl.patches.Polygon(vertices)
polygon = PatchCollection(parts, zorder=-1, rasterized=True)
ax.add_collection(polygon)
ax.set_rasterization_zorder(0)
wblack
  • 343
  • 1
  • 2
  • 11