Font problems with generated EPSs

Hi,

I'm trying to use matplotlib solely for my plotting needs.
Problem is, the fonts are embedded in each EPS file, and
when I import several plots (I have tens of them...) into a
single Latex, the resulting file is HUGE.

Any suggestions?

Thanks,
Alex

There are currently two options: you can either set ps.useafm = True, or you
can set text.usetex = True in your rc settings. Since you are importing
figures into latex, I suggest the usetex option. That way, your figure fonts
can be the same as your text fonts. You'll take a bit of a speed hit with the
latter option, but in my opinion, its the only way to go for generating plots
for publication.

Darren

···

On Tuesday 06 December 2005 09:10, Alex Gontmakher wrote:

Hi,

I'm trying to use matplotlib solely for my plotting needs.
Problem is, the fonts are embedded in each EPS file, and
when I import several plots (I have tens of them...) into a
single Latex, the resulting file is HUGE.

Any suggestions?

Darren Dale wrote:

···

On Tuesday 06 December 2005 09:10, Alex Gontmakher wrote:

Hi,

I'm trying to use matplotlib solely for my plotting needs.
Problem is, the fonts are embedded in each EPS file, and
when I import several plots (I have tens of them...) into a
single Latex, the resulting file is HUGE.

Any suggestions?

There are currently two options: you can either set ps.useafm = True, or you
can set text.usetex = True in your rc settings. Since you are importing
figures into latex, I suggest the usetex option. That way, your figure fonts
can be the same as your text fonts. You'll take a bit of a speed hit with the
latter option, but in my opinion, its the only way to go for generating plots
for publication.

When using tex for font rendering I noticed that parts of the text are not
converted to polygons but embedded as bitmaps. That makes the files big again.
Do you now how to avoid that? E.g. using the r'C\_\{12\} will produce two images
for the numbers and a polygon for the 'C'.

Regards, Christian

I made a wiki entry a while back about how to work around this problem, but it
looks like someone deleted it in mid-November, and I dont have a backup copy.
My solution requires ghostview and xpdf, which is why we dont include it in
mpl by default.

You can remove the following block in backend_ps.py, starting around line
1144:

            command = 'latex -interaction=nonstopmode "%s"' % texfile

            verbose.report(command, 'debug-annoying')
            stdin, stdout, stderr = os.popen3(command)
            verbose.report(stdout.read(), 'debug-annoying')
            verbose.report(stderr.read(), 'helpful')
            command = 'dvips -R -T %fin,%fin -o "%s" "%s"' % (pw, ph, psfile,
dvifile)
            verbose.report(command, 'debug-annoying')
            stdin, stdout, stderr = os.popen3(command)
            verbose.report(stdout.read(), 'debug-annoying')
            verbose.report(stderr.read(), 'helpful')
            os.remove(epsfile)
            if ext.startswith('.ep'):
                dpi = rcParams['ps.distiller.res']

                if sys.platform == 'win32':
                    command = 'gswin32c -dBATCH -dNOPAUSE -dSAFER -r%d \
                      -sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
                      -sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
                else:
                    command = 'gs -dBATCH -dNOPAUSE -dSAFER -r%d \
                    -sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
                    -sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)

                verbose.report(command, 'debug-annoying')
                stdin, stdout, stderr = os.popen3(command)
                verbose.report(stdout.read(), 'debug-annoying')
                verbose.report(stderr.read(), 'helpful')
                shutil.move(epsfile, outfile)
            else: shutil.move(psfile, outfile)

and replace it with this:

            command = 'latex -interaction=nonstopmode "%s"' % texfile
            verbose.report(command, 'debug-annoying')
            stdin, stdout, stderr = os.popen3(command)
            verbose.report(stdout.read(), 'debug-annoying')
            verbose.report(stderr.read(), 'helpful')
            command = 'dvips -R -T %fin,%fin -o "%s" "%s"' % (pw, ph, psfile,
dvifile)
            verbose.report(command, 'debug-annoying')
            stdin, stdout, stderr = os.popen3(command)
            verbose.report(stdout.read(), 'debug-annoying')
            verbose.report(stderr.read(), 'helpful')
            os.remove(epsfile)
            pdffile = tmpname + '.pdf'
            if ext.startswith('.ep'):
                command = 'ps2pdf "%s"'% psfile
                os.system(command)
                command = 'pdftops -level2 "%s" "%s"'% (pdffile, psfile)
                os.system(command)
                os.remove(pdffile)
                command = '/usr/local/bin/ps2eps -l "%s"'% psfile
                stdin, stderr = os.popen4(command)
                verbose.report(stderr.read(), 'helpful')
                command = 'epstopdf "%s"'% epsfile
                os.system(command)
                shutil.move(epsfile, outfile)
                shutil.move(pdffile, basename+'.pdf')
            else:
                command = 'ps2pdf "%s" "%s"'% (psfile, pdffile)
                stdin, stderr = os.popen4(command)
                verbose.report(stderr.read(), 'helpful')
                os.remove(psfile)
                command = 'pdftops -paperw %d -paperh %d -level2 "%s" "%s"'% \
                            (int(pw*72), int(ph*72), pdffile, psfile)
                os.system(command)
                shutil.move(psfile, outfile)

Darren

···

On Tuesday 06 December 2005 10:03, Christian Kristukat wrote:

Darren Dale wrote:
> On Tuesday 06 December 2005 09:10, Alex Gontmakher wrote:
>>Hi,
>>
>>I'm trying to use matplotlib solely for my plotting needs.
>>Problem is, the fonts are embedded in each EPS file, and
>>when I import several plots (I have tens of them...) into a
>>single Latex, the resulting file is HUGE.
>>
>>Any suggestions?
>
> There are currently two options: you can either set ps.useafm = True, or
> you can set text.usetex = True in your rc settings. Since you are
> importing figures into latex, I suggest the usetex option. That way, your
> figure fonts can be the same as your text fonts. You'll take a bit of a
> speed hit with the latter option, but in my opinion, its the only way to
> go for generating plots for publication.

When using tex for font rendering I noticed that parts of the text are not
converted to polygons but embedded as bitmaps. That makes the files big
again. Do you now how to avoid that? E.g. using the r'C\_\{12\} will produce
two images for the numbers and a polygon for the 'C'.

Darren Dale wrote:

···

On Tuesday 06 December 2005 10:03, Christian Kristukat wrote:

Darren Dale wrote:

On Tuesday 06 December 2005 09:10, Alex Gontmakher wrote:

Hi,

I'm trying to use matplotlib solely for my plotting needs.
Problem is, the fonts are embedded in each EPS file, and
when I import several plots (I have tens of them...) into a
single Latex, the resulting file is HUGE.

Any suggestions?

There are currently two options: you can either set ps.useafm = True, or
you can set text.usetex = True in your rc settings. Since you are
importing figures into latex, I suggest the usetex option. That way, your
figure fonts can be the same as your text fonts. You'll take a bit of a
speed hit with the latter option, but in my opinion, its the only way to
go for generating plots for publication.

When using tex for font rendering I noticed that parts of the text are not
converted to polygons but embedded as bitmaps. That makes the files big
again. Do you now how to avoid that? E.g. using the r'C\_\{12\} will produce
two images for the numbers and a polygon for the 'C'.

I made a wiki entry a while back about how to work around this problem, but it
looks like someone deleted it in mid-November, and I dont have a backup copy.
My solution requires ghostview and xpdf, which is why we dont include it in
mpl by default.

Thanks!
Has using pdflatex for tex labels been considered? Or is that too much
dependency? It could possibly even be faster.

Christian

I'm guessing pdflatex is provided by default by every up-to-date latex
installation out there. However, mpl uses a latex package called PSFrag to
render the text in an intermediate postscript file, which is not compatible
with pdflatex. Unfortunately, if you embed that intermediate postscript file
in a new document, the text will frequently be upside down, because PSFrag
uses some commands that are illegal in embedded documents. So by default, mpl
uses ghostscript to "distill" the intermediate postscript file, converting
the fonts to outlines and circumventing the problem.

The code I posted last time replaces the distilling step with a ridiculous
renormalization step (converting the file to pdf using ghostscript, and then
converting back to eps using xpdf) which generates a new eps file that can be
embedded in another document. This seems to be pretty robust, convoluted
though it is. I generated all the figures in my dissertation this way.

A side note, if you intend to compile a document with pdflatex, you can
convert mpl's eps files with epstopdf, which is included in recent versions
of TeTeX.

Darren

···

On Wednesday 07 December 2005 08:41, Christian Kristukat wrote:

Darren Dale wrote:
> On Tuesday 06 December 2005 10:03, Christian Kristukat wrote:
>>Darren Dale wrote:
>>>On Tuesday 06 December 2005 09:10, Alex Gontmakher wrote:
>>>>Hi,
>>>>
>>>>I'm trying to use matplotlib solely for my plotting needs.
>>>>Problem is, the fonts are embedded in each EPS file, and
>>>>when I import several plots (I have tens of them...) into a
>>>>single Latex, the resulting file is HUGE.
>>>>
>>>>Any suggestions?
>>>
>>>There are currently two options: you can either set ps.useafm = True, or
>>>you can set text.usetex = True in your rc settings. Since you are
>>>importing figures into latex, I suggest the usetex option. That way,
>>> your figure fonts can be the same as your text fonts. You'll take a bit
>>> of a speed hit with the latter option, but in my opinion, its the only
>>> way to go for generating plots for publication.
>>
>>When using tex for font rendering I noticed that parts of the text are
>> not converted to polygons but embedded as bitmaps. That makes the files
>> big again. Do you now how to avoid that? E.g. using the r'C\_\{12\} will
>> produce two images for the numbers and a polygon for the 'C'.
>
> I made a wiki entry a while back about how to work around this problem,
> but it looks like someone deleted it in mid-November, and I dont have a
> backup copy. My solution requires ghostview and xpdf, which is why we
> dont include it in mpl by default.

Thanks!
Has using pdflatex for tex labels been considered? Or is that too much
dependency? It could possibly even be faster.

Darren Dale wrote:

Darren Dale wrote:

Darren Dale wrote:

Hi,

I'm trying to use matplotlib solely for my plotting needs.
Problem is, the fonts are embedded in each EPS file, and
when I import several plots (I have tens of them...) into a
single Latex, the resulting file is HUGE.

Any suggestions?

There are currently two options: you can either set ps.useafm = True, or
you can set text.usetex = True in your rc settings. Since you are
importing figures into latex, I suggest the usetex option. That way,
your figure fonts can be the same as your text fonts. You'll take a bit
of a speed hit with the latter option, but in my opinion, its the only
way to go for generating plots for publication.

When using tex for font rendering I noticed that parts of the text are
not converted to polygons but embedded as bitmaps. That makes the files
big again. Do you now how to avoid that? E.g. using the r'C\_\{12\} will
produce two images for the numbers and a polygon for the 'C'.

I made a wiki entry a while back about how to work around this problem,
but it looks like someone deleted it in mid-November, and I dont have a
backup copy. My solution requires ghostview and xpdf, which is why we
dont include it in mpl by default.

Thanks!
Has using pdflatex for tex labels been considered? Or is that too much
dependency? It could possibly even be faster.

I'm guessing pdflatex is provided by default by every up-to-date latex
installation out there. However, mpl uses a latex package called PSFrag to
render the text in an intermediate postscript file, which is not compatible
with pdflatex. Unfortunately, if you embed that intermediate postscript file
in a new document, the text will frequently be upside down, because PSFrag
uses some commands that are illegal in embedded documents. So by default, mpl
uses ghostscript to "distill" the intermediate postscript file, converting
the fonts to outlines and circumventing the problem.

The code I posted last time replaces the distilling step with a ridiculous
renormalization step (converting the file to pdf using ghostscript, and then
converting back to eps using xpdf) which generates a new eps file that can be
embedded in another document. This seems to be pretty robust, convoluted
though it is. I generated all the figures in my dissertation this way.

I see. Thanks for the explanation. Your code seems to export both an eps and pdf
file, right? Anyway, I don't have the program ps2eps on my system so I'll remove
that part.

A side note, if you intend to compile a document with pdflatex, you can
convert mpl's eps files with epstopdf, which is included in recent versions
of TeTeX.

That's the way I do it, too. But there's always a polygon at the left bottom
corner of a letter page which makes the bbox large. It's too annoying to set the
bbox by hand so Iusually convert the .eps to .sk with pstoedit, edit it with
skencil, print it as eps and convert it to pdf. I'd be happy if some of those
steps would vanish in future.

Regards, Christian

···

On Wednesday 07 December 2005 08:41, Christian Kristukat wrote:

On Tuesday 06 December 2005 10:03, Christian Kristukat wrote:

On Tuesday 06 December 2005 09:10, Alex Gontmakher wrote:

Dont let that stand in your way:
http://www.tm.uka.de/~bless/ps2eps

···

On Wednesday 07 December 2005 10:51, Christian Kristukat wrote:

I see. Thanks for the explanation. Your code seems to export both an eps
and pdf file, right? Anyway, I don't have the program ps2eps on my system
so I'll remove that part.

Darren Dale wrote:

···

On Wednesday 07 December 2005 10:51, Christian Kristukat wrote:

I see. Thanks for the explanation. Your code seems to export both an eps
and pdf file, right? Anyway, I don't have the program ps2eps on my system
so I'll remove that part.

Dont let that stand in your way:
http://www.tm.uka.de/~bless/ps2eps

You're right. That's worth having.
Thank you again for your hints.
Christian