Font size and savefig

Hi,

I am using the savefig method to save plots - however, I am finding that the font size is systematically larger in the saved images than in the WxAgg window. It seems that text is ~30% larger in PNG and PDF files compared to the WxAgg display (relative to the axes box size). This can be a little frustrating, as in some cases it can lead to label overlap in the PNG file, when the displayed version looked fine. The following script produces a PNG and a PDF file in which the font size is ~30% larger than in the WxAgg display.

···

---

from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as mpl

fig = mpl.figure()
ax = fig.add_subplot(111)

font = FontProperties()
font.set_size('small')

for tick in ax.get_xticklabels():
     tick.set_fontproperties(font)

for tick in ax.get_yticklabels():
     tick.set_fontproperties(font)

ax.set_xlabel("Hello")
ax.set_ylabel("World")

fig.canvas.draw()

fig.savefig('test.png')
fig.savefig('test.pdf')

--

Is there a way to ensure that the font sizes are as close as possible in the files to what is displayed?

Thanks,

Thomas

If you want the relative fontsizes in the figure window and saved figure to agree, pass the same “dpi” to the figure command and savefig command.

JDH

···

On Tue, Apr 28, 2009 at 9:52 PM, Thomas Robitaille <thomas.robitaille@…287…> wrote:

Hi,

I am using the savefig method to save plots - however, I am finding

that the font size is systematically larger in the saved images than

in the WxAgg window. It seems that text is ~30% larger in PNG and PDF

files compared to the WxAgg display (relative to the axes box size).

This can be a little frustrating, as in some cases it can lead to

label overlap in the PNG file, when the displayed version looked fine.

The following script produces a PNG and a PDF file in which the font

size is ~30% larger than in the WxAgg display.

John,
I thought the font size (which is specified in points) is independent
of dpi, i.e., font size in "pixel" actually scales with the dpi. I
think it should be filed as a bug if the relative font size depends on
the dpi.

Anyhow, I just did a quick test and the (relative) font size does not
seem to vary with dpi.

Thomas,
What version of mpl are you using?
With the mpl from the svn trunk, I don't see any significant change as
you described.
The WxAgg figure and the png output are actually drawn by an identical
backend, so there should be no significant difference. There can be
some subtle difference due to different dpi, but I don't see a
difference as large as 30%. Can you post a some sample images? i.e., a
screenshot of WxAgg figure and the png output.

I can see that the text in pdf output occupies a bit larger area than
png (when usetex=False), but, to me, this seems to be due to different
amount of kernings (it seems that no kerning is applied for pdf text)
instead of different font size.

So, can you first check if the difference goes away when you use same
dpi as John suggested? And if that is the case, can you try the latest
svn and check if the relative font size still depends on the dpi?

Regards,

-JJ

···

On Tue, Apr 28, 2009 at 11:09 PM, John Hunter <jdh2358@...287...> wrote:

If you want the relative fontsizes in the figure window and saved figure to
agree, pass the same "dpi" to the figure command and savefig command.

Hmm, I must have been confused. In older versions of mpl, as you increased the dpi the fonts looked larger in relation to the rest of the figure, and that is what I was remembering. I just ran a few tests and they do scale as expected, so sorry for the noise

···

On Tue, Apr 28, 2009 at 11:24 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

On Tue, Apr 28, 2009 at 11:09 PM, John Hunter <jdh2358@…287…> wrote:

If you want the relative fontsizes in the figure window and saved figure to

agree, pass the same “dpi” to the figure command and savefig command.

John,

I thought the font size (which is specified in points) is independent

of dpi, i.e., font size in “pixel” actually scales with the dpi. I

think it should be filed as a bug if the relative font size depends on

the dpi.

Anyhow, I just did a quick test and the (relative) font size does not

seem to vary with dpi.

Hi Jae-Jong and John,

Thanks for your replies! While experimenting with this to send screenshots, I realized that my default backend was set to MacOSX, not WXAgg. The WXAgg output to the screen actually agrees with the PNG output in terms of font sizes. But the font sizes differ between the MacOSX and WXAgg backends. Attached are screenshots using the MacOSX and WXAgg backends. You can see the font size is different. Is this a bug?

Thanks,

Thomas

wxagg.png

macosx.png