Suggestions for auto scaling fonts , etc for producing images at various scales

Greetings matplotlib users,

Firstly, thank you so much for a great python plotting library. I use
it daily and find the library very intuitive :slight_smile: My question deals
with generating raster images at multiple scales without heavy code
modification. My work flow is to generate two versions of the same
plot, one thumbnail (~ 320x320) and then one 'full size' around (~
800x800) in PNG format for the web.

My current methodology is to generate a postscript file and then send
it through ImageMagick's convert to generate the two different sized
images. I find that this works 'good enough for me', but I often run
into problems when I have transparency in the plot and that
information is lost in the translation of formats... I also get
fairly bulky file sizes, but that is probably my fault for not using
the proper convert flags, anyway...

I have tried messing around with the dpi and figsize settings to the
initial: fig = plt.figure() and fig.savefig() , but I can't seem to
get similiar quality to my hacky method outlined above. Many times,
the fonts look nasty :slight_smile:

Any tips or tricks to make this happen? Thanks again and I sincerely
apologize if I missed a FAQ item , etc on this...

daryl

Daryl Herzmann, on 2011-01-21 15:42, wrote:

Greetings matplotlib users,

Firstly, thank you so much for a great python plotting library. I use
it daily and find the library very intuitive :slight_smile: My question deals
with generating raster images at multiple scales without heavy code
modification. My work flow is to generate two versions of the same
plot, one thumbnail (~ 320x320) and then one 'full size' around (~
800x800) in PNG format for the web.

My current methodology is to generate a postscript file and then send
it through ImageMagick's convert to generate the two different sized
images. I find that this works 'good enough for me', but I often run
into problems when I have transparency in the plot and that
information is lost in the translation of formats... I also get
fairly bulky file sizes, but that is probably my fault for not using
the proper convert flags, anyway...

I have tried messing around with the dpi and figsize settings to the
initial: fig = plt.figure() and fig.savefig() , but I can't seem to
get similiar quality to my hacky method outlined above. Many times,
the fonts look nasty :slight_smile:

Any tips or tricks to make this happen? Thanks again and I sincerely
apologize if I missed a FAQ item , etc on this...

Hi Daryl,

I'm not sure I understand what it is that you want, but if the
issue is related to scaling fonts depending on output figure size
and/or dpi - have you tried playing around with the 'font.size'
rcParam, and defining your font sized using 'xx-small',
'x-large', etc, instead of specifying a point size directly?

From .matplotlibrc:

  # note that font.size controls default text sizes. To configure
  # special text sizes tick labels, axes, labels, title, etc, see the rc
  # settings for axes and ticks. Special text sizes can be defined
  # relative to font.size, using the following values: xx-small, x-small,
  # small, medium, large, x-large, xx-large, larger, or smaller
  #font.size : 12.0

I guess I'm not sure what you meant by the fonts looking 'nasty',
so if font.size doesn't address your issue, could you post a
small example that does the wrong thing, along with the type of
output you were hoping to get.

My feeling is that there shouldn't be a need to use ImageMagick -
but depending on the size and dpi of your desired figures, is the
problem that the text is not being antialiased?

best,

路路路

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

Greetings matplotlib users,

Firstly, thank you so much for a great python plotting library. I use
it daily and find the library very intuitive :slight_smile: My question deals
with generating raster images at multiple scales without heavy code
modification. My work flow is to generate two versions of the same
plot, one thumbnail (~ 320x320) and then one 'full size' around (~
800x800) in PNG format for the web.

My current methodology is to generate a postscript file and then send
it through ImageMagick's convert to generate the two different sized
images. I find that this works 'good enough for me', but I often run
into problems when I have transparency in the plot and that
information is lost in the translation of formats... I also get
fairly bulky file sizes, but that is probably my fault for not using
the proper convert flags, anyway...

I have tried messing around with the dpi and figsize settings to the
initial: fig = plt.figure() and fig.savefig() , but I can't seem to
get similiar quality to my hacky method outlined above. Many times,
the fonts look nasty :slight_smile:

I don't know what would be causing that; we routinely generate png files directly from mpl for a thumbnail and a full-size figure, simply changing the dpi kwarg in savefig, and the results have been satisfactory. Can you post a simple script and pair of png files illustrating the problem?

(Here is a page of such thumbnails; clicking on one brings up the full-size version. http://currents.soest.hawaii.edu/uhdas_fromships/kilomoana/figs/)

Eric

路路路

On 01/21/2011 11:42 AM, Daryl Herzmann wrote:

Any tips or tricks to make this happen? Thanks again and I sincerely
apologize if I missed a FAQ item , etc on this...

daryl

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks for the responses and sorry to not be clear. Here is an example script:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,8))

ax = fig.add_subplot(111)

ax.plot( [0,100], [0,100] )
ax.set_xlabel("Temperature after the sun goes down ^\{\\circ\}F")
ax.set_ylabel("Temperature when the sun goes up ^\{\\circ\}F")
ax.set_title("My Fancy Plot!!!")

fig.savefig('test.png', dpi=(40))

with 3 different outputs. First two numbers are figsize settings and
last is DPI.

In my perfect world, I would like to simple to this at the end of my script:

fig.savefig('thumbnail.png', .....)
fig.savefig('fullsize.png', ......)

and get two clean looking images. If I have to rerun the script with
different options to get a thumbnail and then a fullsize, that is okay
too. I just can't figure out what all needs to be tweeked / how to do
it..

thanks,
  daryl

test_4_4_80.png

test_8_8_40.png

路路路

On Fri, Jan 21, 2011 at 4:15 PM, Paul Ivanov <pivanov314@...287...> wrote:

I guess I'm not sure what you meant by the fonts looking 'nasty',
so if font.size doesn't address your issue, could you post a
small example that does the wrong thing, along with the type of
output you were hoping to get.

Daryl Herzmann, on 2011-01-21 16:41, wrote:

> I guess I'm not sure what you meant by the fonts looking 'nasty',
> so if font.size doesn't address your issue, could you post a
> small example that does the wrong thing, along with the type of
> output you were hoping to get.

Thanks for the responses and sorry to not be clear. Here is an example script:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,8))

ax = fig.add_subplot(111)

ax.plot( [0,100], [0,100] )
ax.set_xlabel("Temperature after the sun goes down ^\{\\circ\}F")
ax.set_ylabel("Temperature when the sun goes up ^\{\\circ\}F")
ax.set_title("My Fancy Plot!!!")

fig.savefig('test.png', dpi=(40))

with 3 different outputs. First two numbers are figsize settings and
last is DPI.

In my perfect world, I would like to simple to this at the end of my script:

fig.savefig('thumbnail.png', .....)
fig.savefig('fullsize.png', ......)

and get two clean looking images. If I have to rerun the script with
different options to get a thumbnail and then a fullsize, that is okay
too. I just can't figure out what all needs to be tweeked / how to do
it..

Daryl,

ok, much clearer now - what you want is for your text to not be
cut-off the way it is in the 8x8 80dpi plot? In other words,
there's not enough space left in the figure for the axis labels
to be completely displayed.

At the moment, I don't think there's a simple way of doing it,
and the quick way I find myself doing is by adjusting the subplot
parameters using:

  plt.subplots_adjust(left=..., bottom=...)

I'm almost certain that one *can* write a function to do this
pro grammatically (without having to hand tweak anything), by
looking at say, the .get_window_extent() but I haven't found the
time to scratch that itch, yet.

If someone wants to beat me to it, here's the sort of thing that
you can do:

  def show_labels_by_shrinking(ax):
    " adjust subplot parameters to fit the yaxis label"
    f = ax.figure
    textwidth = ax.yaxis.get_label().get_window_extent().width
    labelwidth = max([lab.get_window_extent().width for lab in ax.get_yticklabels()])
    plt.subplots_adjust(left=(textwidth+labelwidth+3*ax.yaxis.labelpad)/f.get_window_extent().width)
    # the 3 *ax.yaxis.labelpad is just a fudge factor for now, need
    # to look into the sourcecode to figure out what the
    # appropriate placement is normally, to know how to adjust
    # properly
  
  ax.set_ylabel('foo')
  show_labels_by_shrinking(ax)
  ax.set_ylabel("a\nmulti\nline\nexample")
  show_labels_by_shrinking(ax)
  
best,

路路路

On Fri, Jan 21, 2011 at 4:15 PM, Paul Ivanov <pivanov314@...287...> wrote:

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

Hi Paul,

Sorry for the delayed response....

ok, much clearer now - what you want is for your text to not be
cut-off the way it is in the 8x8 80dpi plot? In other words,
there's not enough space left in the figure for the axis labels
to be completely displayed.

Even if the text was displayed, it appears to be rather smaller than
it could be.

At the moment, I don't think there's a simple way of doing it,
and the quick way I find myself doing is by adjusting the subplot
parameters using:

Thanks for helping me so much with this. I'll continue to use my
convert hack until better things come down the pipe. Sorry that I am
not able to write patches to help this situation. :frowning:

daryl

路路路

On Fri, Jan 21, 2011 at 5:20 PM, Paul Ivanov <pivanov314@...287...> wrote:

You might have more luck using a vectorized image format such as svg
or eps. In those formats, the text is saved as text and renderers can
scale the font properly based on the display size. Note that it won't
reflow your text or anything like that, but it will keep it somewhat
readable in smaller sizes.

Just my 2 cents.

Ben Root

路路路

On Tuesday, January 25, 2011, Daryl Herzmann <akrherz@...287...> wrote:

Hi Paul,

Sorry for the delayed response....

On Fri, Jan 21, 2011 at 5:20 PM, Paul Ivanov <pivanov314@...287...> wrote:

ok, much clearer now - what you want is for your text to not be
cut-off the way it is in the 8x8 80dpi plot? In other words,
there's not enough space left in the figure for the axis labels
to be completely displayed.

Even if the text was displayed, it appears to be rather smaller than
it could be.

At the moment, I don't think there's a simple way of doing it,
and the quick way I find myself doing is by adjusting the subplot
parameters using:

Thanks for helping me so much with this. I'll continue to use my
convert hack until better things come down the pipe. Sorry that I am
not able to write patches to help this situation. :frowning:

daryl

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options