Problems with rasterizing multiple elements

Hi Everyone,

I’m having problems when rasterizing many lines in a plot using the rasterized=True keyword using the pdf output.

Some version info:

matplotlib version 1.1.1rc

ubuntu 12.04

python 2.7.3

Here’s a basic example that demonstrates my problem:

Import matplotlib to create a pdf document

import matplotlib

matplotlib.use(‘Agg’)

from matplotlib.backends.backend_pdf import PdfPages

pdf = PdfPages(‘rasterized_test.pdf’)

import matplotlib.pylab as plt

some test data

import numpy as np

ts = np.linspace(0,2*np.pi,100) * np.ones((200,100))

ts += (np.linspace(0, np.pi, 200)[np.newaxis] * np.ones((100,200))).T

ys = np.sin(ts)

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot(ts[0], ys.T, color=‘r’, lw=0.5, alpha=0.5, rasterized=True)

pdf.savefig()

pdf.close()

Essentially, I have a lot (200 in this case) of closely overlapping lines which makes the resulting figure (not rasterized) overly difficult to load. I would like to rasterize these lines, such that the axis labels (and other elements of the plot, not shown) remain vectors while the solution trajectories are flattened to a single raster background. However, using the code above, the image still takes a long time to load since each trajectory is independently rasterized, resulting in multiple layers. (If I open the resulting pdf with a program like inkscape, I can manipulate each trajectory independently.)

Is it possible to flatten all of the rasterized elements into a single layer, so the pdf size would be greatly reduced?

Thanks,

–Peter

I recommend you to use LineCollection as it is rasterized as a single image.

For example,

from matplotlib.collections import LineCollection
d = [np.array([ts[0], ys1]).T for ys1 in ys]

lc = LineCollection(d, color='r', lw=0.5, alpha=0.5,
                    rasterized=True)
ax.add_collection(lc)

ax.set_xlim(0, 7)
ax.set_ylim(-1, 1)

If you need to stick to plot command somehow, you need to create your
own artist.
There is a similar example,

http://matplotlib.sourceforge.net/examples/pylab_examples/demo_agg_filter.html

Take a look at the FilteredArtistList class. Your draw method should
be something like

    def draw(self, renderer):
        renderer.start_rasterizing()
        for a in self._artist_list:
            a.draw(renderer)
        renderer.stop_rasterizing()

IHTH,

-JJ

···

On Wed, Aug 22, 2012 at 2:45 AM, Peter St. John <peterc.stjohn@...287...> wrote:

Hi Everyone,

I'm having problems when rasterizing many lines in a plot using the
rasterized=True keyword using the pdf output.
Some version info:
matplotlib version 1.1.1rc
ubuntu 12.04
python 2.7.3

Here's a basic example that demonstrates my problem:
# Import matplotlib to create a pdf document
import matplotlib
matplotlib.use('Agg')
from matplotlib.backends.backend_pdf import PdfPages
pdf = PdfPages('rasterized_test.pdf')

import matplotlib.pylab as plt

# some test data
import numpy as np
ts = np.linspace(0,2*np.pi,100) * np.ones((200,100))
ts += (np.linspace(0, np.pi, 200)[np.newaxis] * np.ones((100,200))).T
ys = np.sin(ts)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(ts[0], ys.T, color='r', lw=0.5, alpha=0.5, rasterized=True)
pdf.savefig()

pdf.close()

Essentially, I have a lot (200 in this case) of closely overlapping lines
which makes the resulting figure (not rasterized) overly difficult to load.
I would like to rasterize these lines, such that the axis labels (and other
elements of the plot, not shown) remain vectors while the solution
trajectories are flattened to a single raster background. However, using the
code above, the image still takes a long time to load since each trajectory
is independently rasterized, resulting in multiple layers. (If I open the
resulting pdf with a program like inkscape, I can manipulate each trajectory
independently.)

Is it possible to flatten all of the rasterized elements into a single
layer, so the pdf size would be greatly reduced?

Thanks,
--Peter

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users