PNG images and image area

I'm attempting to output an image with a predictable bounding box so
that it can be placed into a KML document and be correctly
georeferenced.

Essentially I need a PNG that has NO labeling and the size of the
image be exactly the size of the plot bounding box and no more, no
less.

I can get exactly what I want with the top and bottom of the image with:

fig.add_axes((0,0,1,1)

However, I'm still left with undesired space on the left and right.
How can I bring the left and right edges of the bounding box to match
the image width?

Also, this might be a candidate for a handy function for
pyplot.figure(). This could be very useful for anyone needing to make
KML-friendly figures.

Thanks for any ideas!

Bruce

···

---------------------------------------
Bruce W. Ford
Clear Science, Inc.
bruce@...2905...
http://www.ClearScienceInc.com
Phone/Fax: 904-379-9704
8241 Parkridge Circle N.
Jacksonville, FL 32211
Skype: bruce.w.ford
Google Talk: fordbw@...287...

The question has been answered I think in the thread
"Graph gains a blank space at the right hand side"
just some seconds ago.

Am I wrong?

Friedrich

It is best to create a figure of a right size in the first place.

If this cannot be done, try something like below.

dpi = 80
fig=figure(1, dpi=dpi)
ax = axes((0,0,1,1))
ax.set_aspect(1)

from matplotlib.transforms import TransformedBbox, Affine2D
w, h = fig.get_size_inches()
bbox = TransformedBbox(ax.bbox,
fig.transFigure.inverted()+Affine2D().scale(w, h))

savefig("a.png", bbox_inches=bbox, dpi=dpi)

Note that the size of the output will be different from the original
figure size.
Regards,

-JJ

···

On Tue, Feb 23, 2010 at 3:37 PM, Bruce Ford <bruce@...2905...> wrote:

I'm attempting to output an image with a predictable bounding box so
that it can be placed into a KML document and be correctly
georeferenced.

Essentially I need a PNG that has NO labeling and the size of the
image be exactly the size of the plot bounding box and no more, no
less.

I can get exactly what I want with the top and bottom of the image with:

fig.add_axes((0,0,1,1)

However, I'm still left with undesired space on the left and right.
How can I bring the left and right edges of the bounding box to match
the image width?

Also, this might be a candidate for a handy function for
pyplot.figure(). This could be very useful for anyone needing to make
KML-friendly figures.

Thanks for any ideas!

Bruce
---------------------------------------
Bruce W. Ford
Clear Science, Inc.
bruce@...2905...
http://www.ClearScienceInc.com
Phone/Fax: 904-379-9704
8241 Parkridge Circle N.
Jacksonville, FL 32211
Skype: bruce.w.ford
Google Talk: fordbw@...287...

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi! I'm trying to loop through all the built-in colormaps, applying each to an image before printing it to a file, then moving on to the next one.

from matplotlib import cm
for cmap in dir(cm): # cmap in cm doesn't work 'cause cm is a module
   ax.imshow(image, cmap)
   canvas.print_figure('image_'+cmap)

works until cmap == 'LUTSIZE', which evaluates to an integer and thus raises an exception. I tried putting it in a try/except:

for cmap in dir(cm):
   try:
       ax.imshow(image, cmap)
       canvas.print_figure('image_'+cmap)
   except:
       pass

but despite this, after 'LUTSIZE', every cmap - even valid ones - also raises the exception, and thus doesn't get used. So I tried just by-passing cmap == 'LUTSIZE' (in the obvious way), but then encountered cmap == 'ScalarMapable', which resulted in the same subsequent behavior as 'LUTSIZE'.

At that point I decided I should try a "positive filter," so I figured out that cmaps are instances of matplotlib.colors.LinearSegmentedColormap (which I imported as LSC) and tried adding an "if isinstance(cmap, LSC)", but of course that didn't work, 'cause the elements of dir(cm) are strings, not LSC's.

At this point I've decided I've wasted too much time trying to figure this out on my own, so:

0) is there some "elegant" way to do what I want to do?

1) why doesn't this:

for cmap in dir(cm):
   try:
       ax.imshow(image, cmap)
       canvas.print_figure('image_'+cmap)
   except:
       pass

"work" (i.e., simply bypass those elements of dir(cm) which cause imshow to raise an exception, but then continue on as if nothing had happened)? Is this a bug?

Thanks!

DG

Hi! I'm trying to loop through all the built-in colormaps, applying each
to an image before printing it to a file, then moving on to the next one.

>>> from matplotlib import cm
>>> for cmap in dir(cm): # cmap in cm doesn't work 'cause cm is a module
>>> ax.imshow(image, cmap)
>>> canvas.print_figure('image_'+cmap)

works until cmap == 'LUTSIZE', which evaluates to an integer and thus raises

an exception. I tried putting it in a try/except:

>>> for cmap in dir(cm):
>>> try:
>>> ax.imshow(image, cmap)
>>> canvas.print_figure('image_'+cmap)
>>> except:
>>> pass

but despite this, after 'LUTSIZE', every cmap - even valid ones - also
raises the exception, and thus doesn't get used. So I tried just
by-passing cmap == 'LUTSIZE' (in the obvious way), but then encountered
cmap == 'ScalarMapable', which resulted in the same subsequent behavior as
'LUTSIZE'.

At that point I decided I should try a "positive filter," so I figured out
that cmaps are instances of matplotlib.colors.LinearSegmentedColormap
(which I imported as LSC) and tried adding an "if isinstance(cmap, LSC)",
but of course that didn't work, 'cause the elements of dir(cm) are strings,
not LSC's.

At this point I've decided I've wasted too much time trying to figure this
out on my own, so:

0) is there some "elegant" way to do what I want to do?

1) why doesn't this:
>>> for cmap in dir(cm):
>>> try:
>>> ax.imshow(image, cmap)
>>> canvas.print_figure('image_'+cmap)
>>> except:
>>> pass

"work" (i.e., simply bypass those elements of dir(cm) which cause imshow to
raise an exception, but then continue on as if nothing had happened)? Is
this a bug?

Thanks!

DG

Hi,

some time ago somebody proposed an example on the list to circle through all
possible colormaps. In this time cm had an attribute "cm.cmapnames", which
hold all these names, but nowerdays (svn-HEAD) this attribute has be removed
and in my opinion 'cm.cmap_d.keys()' is an appropriate replacement for this.

In your example, you cycle through all tools/functions/variables of the
cm-module and therefore encounter non-cmaps (like LUTSIZE, ...).

To make my point: I think you have to replace
    for cmap in dir(cm):
with
    for i in cm.cmap_d.keys():

Kind regards,
Matthias

···

On Wednesday 24 February 2010 00:45:56 David Goldsmith wrote:

The names are available as "cm._cmapnames". However, this list does
not include any reverse map names (i.e., names like "jet_r").
So, yes, you should use cmap_d instead.

Regards,

-JJ

···

On Wed, Feb 24, 2010 at 3:06 AM, Matthias Michler <MatthiasMichler@...361...> wrote:

some time ago somebody proposed an example on the list to circle through all
possible colormaps. In this time cm had an attribute "cm.cmapnames", which
hold all these names, but nowerdays (svn-HEAD) this attribute has be removed
and in my opinion 'cm.cmap_d.keys()' is an appropriate replacement for this.

I believe this happens because you never clear your figure (or axes)
between print_figure.
imshow does not erase an existing image and you end up with bunch of
images overlapped.
And the exceptions are keep being raised as the image you created with
cmap="LUTSIZE" is still there.

calling ax.cla() before ax.imshow works for me.

Regards,

-JJ

···

On Tue, Feb 23, 2010 at 6:45 PM, David Goldsmith <d_l_goldsmith@...9...> wrote:

1) why doesn't this:

for cmap in dir(cm):
try:
ax.imshow(image, cmap)
canvas.print_figure('image_'+cmap)
except:
pass

"work" (i.e., simply bypass those elements of dir(cm) which cause imshow to raise an exception, but then continue on as if nothing had happened)? Is this a bug?

The following was sent unintentionally in private e-mail (my e-mail
program always selects the sender as recipient first :frowning: ). I think
the solution by Jae-Joon is also elegant, but nevertheless the
following may be useful also (and maybe also in other places):

···

---------- Forwarded message ----------
From: Friedrich Romstedt <friedrichromstedt@...287...>
Date: 2010/2/24
Subject: Re: [Matplotlib-users] Looping through all the built-in colormaps
To: David Goldsmith <d_l_goldsmith@...9...>

0) is there some "elegant" way to do what I want to do?

Don't know whether it's elegant or not, but it should do the job:

for cmap_name in dir(cm):
   cmap_object = getattr(cm, cmap_name)
   if isinstance(cmap_object, matplotlib.colors.LinearSegmentedColormap):
       [...]

1) why doesn't this:

for cmap in dir(cm):
   try:
       ax.imshow(image, cmap)
       canvas.print_figure('image_'+cmap)
   except:
       pass

"work" (i.e., simply bypass those elements of dir(cm) which cause imshow to raise an exception, but then continue on as if nothing had happened)? Is this a bug?

I guess it's because you have messed up with the internals of the
axes, when passing an invalid entry. It gets stored somewhere without
check, and then causes subsequent error occuring before the next
element is applied fully, I guess. It's more a bug of your code than
of matplotlib, because your argument did not fulfil specification :slight_smile:

Friedrich