# Problem with a very heavy EPS image - scatter plot too heavy as EPS

Using Stata, I produced a scatter plot of some variables. I saved the image as an EPS file. There are around a 1000 dots in the scatter plot - and this makes the EPS file to be 26MB. I need to use this image in my LaTeX file (I use TeXnicCenter and compile as LaTeX => PS => PDF). The problem is that it produces a PDF file that takes a long time to load the page displaying the image. I don't need this image to be super sharp - there is no real point in zooming it in too much. Furthermore, the PDF I get is quite heavy for a 16 pages document. Is there anything I could do to improve the loading time of the page with the drawing, and decreasing the size of the PDF file? For example,

1. Would it be an improvement if I convert the image to JPEG, PNG, or BMP? Would it decrease the image size?

2. Is there a way to tell LaTeX to keep using the EPS image, but decrease the quality of the image?

-
Take a snapshot of your screen when the .eps file open and paste it in Paint, save as .png. You will see the difference immediately :) – percusse Jan 3 '12 at 20:47
More seriously, you can convert a .eps file to a .png of defined resolution using tools such as ImageMagick (command line) or IrfanView (Windows GUI, uses GhostScript). – Joseph Wright Jan 3 '12 at 21:12
Furthering Joseph's suggestion, if you strip off the axes and any annotations, convert to png and crop the result, then pgfplots provides a way to wrap this with axes for the best of both worlds. I may add this as an answer later (at work right now). – qubyte Jan 4 '12 at 5:12
1000 dots should not require a 26MB eps file, or anything close. Can you make the file available to download (or click my name to get my address and email it to me)? I have some experience in fixing oversized postscript code. – Ian Thompson Jan 4 '12 at 22:56

As noted in the comments on the question, EPS is not an ideal way to store the plot. For scatter plots with many points and also for pseudo colour plots, it's a better idea to store the plot itself (without axes and annotations) as a raster graphic such as a PNG. In the case of a pseudo colour plot, this is actually minimal and no information is lost. See this answer for an example I did of one.

As a sample, I plotted random points in octave and then turned off the axes. After saving as a PNG file, I used imagemagick to trim the margins from the plot to produce scatter.png:

Using pgfplots I wrapped this PNG in axes, thus minimising the the space and processing required by the points in the plot. This requires the use of pdflatex rather than latex with a PNG file.

\documentclass[10pt]{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis} [
scale only axis,        % Plot size does not include axes.
enlargelimits=false,    % Shrink wrap the PNG.
axis on top,            % Axes placed over PNG to avoid obscuring the lines.
xlabel=$x$,
ylabel=$y$,
]
xmin=0,
xmax=1,
ymin=0,
ymax=1,
] {scatter.png};
\end{axis}
\end{tikzpicture}
\caption{A simple example.}
\end{figure}

\end{document}

-

You could load the epstopdf package or the epspdfconversion package to convert the .eps file to a .pdf file "on the fly". The first time you compile your LaTeX program (going directly from .tex to .pdf, without the "detour" via .ps), a format conversion from .eps to .pdf will be performed on the graphics file. Thereafter, LaTeX will know to load the (much smaller) PDF-formatted graphics file directly, saving much compilation time; the size of the output pdf file may shrink as well, but that depends importantly on the nature of the contents of the eps file that's being converted to pdf.

Here's an interesting excerpt from the user guide of the epspdfconversion package:

I [the package's author] am using this package for the inclusion of EPS-ﬁgures (or .pdf or .ps) that are produced en-masse by a software packages like Stata... The package makes sure that I can include the EPS-ﬁgures easily and the updating of the corresponding PDF's is done "on-the-ﬂy".

For more information on embedding various graphics files using pdf(la)tex, I recommend you go to Imported graphics in PDFLaTeX from the UK TeX FAQ document.

-
Isn't the main reason for the pdf file being smaller the fact that it is compressed? It still contains the same large amount of vector data. If that's the case, will this smaller size of the included file translate into a smaller size of the resulting pdf document? Will it speed up the display of the page in a pdf reader? – Jan Hlavacek Jan 4 '12 at 0:34
@JanHlavacek: I'm afraid I don't have enough detail knowledge about the eps and pdf file formats to be able to answer your questions directly. – Mico Jan 4 '12 at 3:31
I tried it, created a scatterplot with 3000 points in R, saved is as an eps file. Using epstopdf package with pdflatex (actually, lualatex) creates a file that is slightly larger than using latex -> dvips -> ps2pdf. – Jan Hlavacek Jan 4 '12 at 4:54
@JanHlavacek: Thanks for this. Did you try to compile your program with pdflatex as well? I've clarified my answer to emphasize that while there's a gain in terms of compilation speed, any gain in terms of size will depend on the nature of the eps file. – Mico Jan 4 '12 at 13:08
Pdflatex creates a file only about 20 bytes smaller than lualatex. Tried it again with a larger file, the results pretty much the same. Incidentally, a scatterplot with 100000 points created by R takes only about 3.8 MB in an eps file. And creating a scatterplot with 100000 uniformly distributed points is an interesting way of coloring a square black. – Jan Hlavacek Jan 4 '12 at 15:13

I already saw comments where you got a recommendation to use ImageMagick to convert your EPS file to PNG image file format. Based on my own experience I would suggest that you use GraphicsMagick (ImageMagick's cousin) to convert your image in one of lossy compression image file formats like JPEG for example before converting it back to EPS format. Then compare the size of your original EPS file and new EPS file. For anybody who would like to start ImageMagick vs GraphicsMagick flame war I have one comment. Please, use both tools on the same image or even on the large collection of images and then compare the size of resulting image files, quality of images and if you have little time on your hands do the speed test. Then make your pick.

-
I do not think it is even necessary to use a lossy compression. Simply downsampling the image when going from vector format to a raster format should seriously reduce the size, without introducing jpeg compression artifacts. – Jan Hlavacek Jan 4 '12 at 0:22
Also, if the OP wants to switch from latex based workflow to pdflatex, he/she would not have to convert the raster image back to eps, since pdflatex can include jpef , gif and png images. – Jan Hlavacek Jan 4 '12 at 0:24
@Jan, for sure, it (pdfTeX) cannot include Gif. – AlexG Jan 4 '12 at 8:43