Creating a simple plot in SunPy and saving at an image's native resolution

280 views
Skip to first unread message

Keith Hughitt

unread,
Jan 20, 2012, 9:21:20 AM1/20/12
to su...@googlegroups.com
The plot() and show() methods of SunPy Map objects currently include axes labels, a title and color bar.

Further, saving such a plot will result in an image with a different number of pixels than the image that was used to make the Map, e.g.:

import sunpy
plot = sunpy.make_map(sunpy.AIA_171_IMAGE).plot()
plot.savefig('test.png')

Plotting an image in Matplotlib and saving it at it's natural scale without any extra labels or padding turns out to be a bit akward:

import sunpy
import matplotlib.pyplot as plt

aia = sunpy.make_map(sunpy.AIA_171_IMAGE)

fig = plt.figure(frameon=False)
fig.set_size_inches(10.24, 10.24)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)

ax.imshow(aia, aspect='normal', norm=aia.norm(), cmap=aia.cmap)
fig.savefig('test.png', dpi=100)

Compare this with saving an (unscaled/grayscale) image with PIL:

import sunpy
import Image

aia = sunpy.make_map(sunpy.AIA_171_IMAGE)
im = Image.fromarray(aia)
im.convert('L').save('test2.png')

I'm thinking about adding a method to the BaseMap to help make it easier to achieve the above effect. One approach would be to
create a method like "save_image" to achieve the above effect. I wonder if we should also take a closer look at .plot() though
and consider making it possible to toggle on/off things like title, colorbar, and axes labels?

Keith

Richard Schwartz

unread,
Jan 20, 2012, 2:19:35 PM1/20/12
to su...@googlegroups.com
I think I just updated by Sunpy
cd C:\sunpy\
"%ProgramFiles(x86)%\Git\bin\git" pull

Good things appeared to happen
Then I opened Spyder using Python(x,y) to try Keith's second code block and it didn't like it.  Anyone care to tell me what has happened?  I use Windows 7.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.

>>> import sunpy
>>> import matplotlib.pyplot as plt
>>> aia = sunpy.make_map(sunpy.AIA_171_IMAGE)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "c:\sunpy\sunpy\map\__init__.py", line 58, in make_map
    return BaseMap.read(args[0])
  File "c:\sunpy\sunpy\map\basemap.py", line 238, in read
    header, data = cls.parse_file(filepath)
  File "c:\sunpy\sunpy\map\basemap.py", line 211, in parse_file
    from sunpy.io import read_file
  File "c:\sunpy\sunpy\io\__init__.py", line 3, in <module>
    from sunpy.io import fits, jp2
  File "c:\sunpy\sunpy\io\fits.py", line 27, in <module>
    import pyfits
  File "C:\Python27\lib\site-packages\pyfits-2.4.0-py2.7-win32.egg\pyfits\__init__.py", line 17, in <module>
    from core import *
  File "C:\Python27\lib\site-packages\pyfits-2.4.0-py2.7-win32.egg\pyfits\core.py", line 34, in <module>
    import gzip
  File "C:\Python27\lib\gzip.py", line 36, in <module>
    class GzipFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'
>>> 
--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

Shih, Albert Y. (GSFC-6710)

unread,
Jan 20, 2012, 2:25:29 PM1/20/12
to su...@googlegroups.com

Hi, Richard,

     I think the first thing to try is to upgrade your PyFITS.  It looks like you’re running 2.4.0, and the latest version is 3.0.4.

 

Albert

Keith Hughitt

unread,
Jan 20, 2012, 2:26:21 PM1/20/12
to su...@googlegroups.com
I was just going to suggest the same thing... just to be sure though-are you able to run the example with the latest version of SunPy, Albert?

Shih, Albert Y. (GSFC-6710)

unread,
Jan 20, 2012, 2:30:19 PM1/20/12
to su...@googlegroups.com

Hi, Keith,

     Yeah, I just tried your code block to create “test.png”, and it works fine with the latest SunPy and PyFITS 3.0.3.  It may be relevant that Python(x,y) appears to be broken on my machine right now – I have no clue why – so I ran the code from the command line.

 

Albert

Richard Schwartz

unread,
Jan 20, 2012, 2:51:03 PM1/20/12
to su...@googlegroups.com
sunpy.org does not discuss the upgrade/update procedure for Pyfits.� I just downloaded and ran pyfits-3.0.3.win32-py2.7(2).exe and that wasn't enough.� I'll look around to see if I can figure this out.� I hoped that I would have found out how under Sunpy.org

Richard

On 1/20/2012 2:30 PM, Shih, Albert Y. (GSFC-6710) wrote:

Hi, Keith,

���� Yeah, I just tried your code block to create �test.png�, and it works fine with the latest SunPy and PyFITS 3.0.3.� It may be relevant that Python(x,y) appears to be broken on my machine right now � I have no clue why � so I ran the code from the command line.

�

Albert

�

From: su...@googlegroups.com [mailto:su...@googlegroups.com] On Behalf Of Keith Hughitt
Sent: Friday, January 20, 2012 2:26 PM
To: su...@googlegroups.com
Subject: Re: {SunPy} Creating a simple plot in SunPy and saving at an image's native resolution

�

I was just going to suggest the same thing... just to be sure though-are you able to run the example with the latest version of SunPy, Albert?

�

On Fri, Jan 20, 2012 at 2:25 PM, Shih, Albert Y. (GSFC-6710) <albert...@nasa.gov> wrote:

Hi, Richard,

���� I think the first thing to try is to upgrade your PyFITS.� It looks like you�re running 2.4.0, and the latest version is 3.0.4.

�

Albert

�

From: su...@googlegroups.com [mailto:su...@googlegroups.com] On Behalf Of Richard Schwartz
Sent: Friday, January 20, 2012 2:20 PM
To: su...@googlegroups.com
Subject: Re: {SunPy} Creating a simple plot in SunPy and saving at an image's native resolution

�

I think I just updated by Sunpy

cd C:\sunpy\
"%ProgramFiles(x86)%\Git\bin\git" pull
�
Good things appeared to happen
Then I opened Spyder using Python(x,y) to try Keith's second code block and it didn't like it.� Anyone care to tell me what has happened?� I use Windows 7.
�
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
�
>>> import sunpy
>>> import matplotlib.pyplot as plt
>>> aia = sunpy.make_map(sunpy.AIA_171_IMAGE)
Traceback (most recent call last):
� File "<console>", line 1, in <module>
� File "c:\sunpy\sunpy\map\__init__.py", line 58, in make_map
��� return BaseMap.read(args[0])
� File "c:\sunpy\sunpy\map\basemap.py", line 238, in read
��� header, data = cls.parse_file(filepath)
�
� File "c:\sunpy\sunpy\map\basemap.py", line 211, in parse_file
��� from sunpy.io import read_file
� File "c:\sunpy\sunpy\io\__init__.py", line 3, in <module>
��� from sunpy.io import fits, jp2
� File "c:\sunpy\sunpy\io\fits.py", line 27, in <module>
��� import pyfits
� File "C:\Python27\lib\site-packages\pyfits-2.4.0-py2.7-win32.egg\pyfits\__init__.py", line 17, in <module>
��� from core import *
� File "C:\Python27\lib\site-packages\pyfits-2.4.0-py2.7-win32.egg\pyfits\core.py", line 34, in <module>
��� import gzip
� File "C:\Python27\lib\gzip.py", line 36, in <module>
��� class GzipFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'
>>> 



On 1/20/2012 9:21 AM, Keith Hughitt wrote:

The plot() and show() methods of SunPy Map objects currently include axes labels, a title and color bar.

�

Further, saving such a plot will result in an image with a different number of pixels than the image that was used to make the Map, e.g.:

�

import sunpy

plot = sunpy.make_map(sunpy.AIA_171_IMAGE).plot()

plot.savefig('test.png')

�

Plotting an image in Matplotlib and saving it at it's natural scale without any extra labels or padding turns out to be a bit akward:

�

import sunpy

import matplotlib.pyplot as plt

�

aia = sunpy.make_map(sunpy.AIA_171_IMAGE)

�

fig = plt.figure(frameon=False)

fig.set_size_inches(10.24, 10.24)

ax = plt.Axes(fig, [0., 0., 1., 1.])

ax.set_axis_off()

fig.add_axes(ax)

�

ax.imshow(aia, aspect='normal', norm=aia.norm(), cmap=aia.cmap)

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

�

Compare this with saving an (unscaled/grayscale) image with PIL:

�

import sunpy

import Image

�

aia = sunpy.make_map(sunpy.AIA_171_IMAGE)

im = Image.fromarray(aia)

im.convert('L').save('test2.png')

�

I'm thinking about adding a method to the BaseMap to help make it easier to achieve the above effect. One approach would be to

create a method like "save_image" to achieve the above effect. I wonder if we should also take a closer look at .plot() though

and consider making it possible to toggle on/off things like title, colorbar, and axes labels?

�

Keith

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

�

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

�

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

Shih, Albert Y. (GSFC-6710)

unread,
Jan 20, 2012, 3:04:33 PM1/20/12
to su...@googlegroups.com

Hi, all,

     I just helped Richard remove the older version of PyFITS.  It had been installed by easy_install, which does not play nice with a subsequent upgrade using a Windows installer.  The code block does not crash for him on the command line, but rather only in Spyder.  Since I can’t run Spyder at all right now for whatever reason, I can’t duplicate his bug.  We’ll try to figure out where the problem is in Spyder and/or Python(x,y).

     But on a related note, it’d probably be good to have documentation telling people how to keep their packages up to date.

 

Albert

 

From: su...@googlegroups.com [mailto:su...@googlegroups.com] On Behalf Of Richard Schwartz
Sent: Friday, January 20, 2012 2:51 PM
To: su...@googlegroups.com
Subject: updating Pyfits. Re: {SunPy} Creating a simple plot in SunPy and saving at an image's native resolution

 

sunpy.org does not discuss the upgrade/update procedure for Pyfits.  I just downloaded and ran pyfits-3.0.3.win32-py2.7(2).exe and that wasn't enough.  I'll look around to see if I can figure this out.  I hoped that I would have found out how under Sunpy.org



Richard

On 1/20/2012 2:30 PM, Shih, Albert Y. (GSFC-6710) wrote:

Hi, Keith,

     Yeah, I just tried your code block to create “test.png”, and it works fine with the latest SunPy and PyFITS 3.0.3.  It may be relevant that Python(x,y) appears to be broken on my machine right now – I have no clue why – so I ran the code from the command line.

 

Albert

 

From: su...@googlegroups.com [mailto:su...@googlegroups.com] On Behalf Of Keith Hughitt
Sent: Friday, January 20, 2012 2:26 PM
To: su...@googlegroups.com
Subject: Re: {SunPy} Creating a simple plot in SunPy and saving at an image's native resolution

 

I was just going to suggest the same thing... just to be sure though-are you able to run the example with the latest version of SunPy, Albert?

 

On Fri, Jan 20, 2012 at 2:25 PM, Shih, Albert Y. (GSFC-6710) <albert...@nasa.gov> wrote:

Hi, Richard,

     I think the first thing to try is to upgrade your PyFITS.  It looks like you’re running 2.4.0, and the latest version is 3.0.4.

 

Albert

 

From: su...@googlegroups.com [mailto:su...@googlegroups.com] On Behalf Of Richard Schwartz
Sent: Friday, January 20, 2012 2:20 PM
To: su...@googlegroups.com
Subject: Re: {SunPy} Creating a simple plot in SunPy and saving at an image's native resolution

 

I think I just updated by Sunpy

cd C:\sunpy\
"%ProgramFiles(x86)%\Git\bin\git" pull
 
Good things appeared to happen
Then I opened Spyder using Python(x,y) to try Keith's second code block and it didn't like it.  Anyone care to tell me what has happened?  I use Windows 7.
 
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
 
>>> import sunpy
>>> import matplotlib.pyplot as plt
>>> aia = sunpy.make_map(sunpy.AIA_171_IMAGE)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "c:\sunpy\sunpy\map\__init__.py", line 58, in make_map
    return BaseMap.read(args[0])
  File "c:\sunpy\sunpy\map\basemap.py", line 238, in read
    header, data = cls.parse_file(filepath)
 
  File "c:\sunpy\sunpy\map\basemap.py", line 211, in parse_file
    from sunpy.io import read_file
  File "c:\sunpy\sunpy\io\__init__.py", line 3, in <module>
    from sunpy.io import fits, jp2
  File "c:\sunpy\sunpy\io\fits.py", line 27, in <module>
    import pyfits
  File "C:\Python27\lib\site-packages\pyfits-2.4.0-py2.7-win32.egg\pyfits\__init__.py", line 17, in <module>
    from core import *
  File "C:\Python27\lib\site-packages\pyfits-2.4.0-py2.7-win32.egg\pyfits\core.py", line 34, in <module>
    import gzip
  File "C:\Python27\lib\gzip.py", line 36, in <module>
    class GzipFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'
>>> 



On 1/20/2012 9:21 AM, Keith Hughitt wrote:

The plot() and show() methods of SunPy Map objects currently include axes labels, a title and color bar.

 

Further, saving such a plot will result in an image with a different number of pixels than the image that was used to make the Map, e.g.:

 

import sunpy

plot = sunpy.make_map(sunpy.AIA_171_IMAGE).plot()

plot.savefig('test.png')

 

Plotting an image in Matplotlib and saving it at it's natural scale without any extra labels or padding turns out to be a bit akward:

 

import sunpy

import matplotlib.pyplot as plt

 

aia = sunpy.make_map(sunpy.AIA_171_IMAGE)

 

fig = plt.figure(frameon=False)

fig.set_size_inches(10.24, 10.24)

ax = plt.Axes(fig, [0., 0., 1., 1.])

ax.set_axis_off()

fig.add_axes(ax)

 

ax.imshow(aia, aspect='normal', norm=aia.norm(), cmap=aia.cmap)

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

 

Compare this with saving an (unscaled/grayscale) image with PIL:

 

import sunpy

import Image

 

aia = sunpy.make_map(sunpy.AIA_171_IMAGE)

im = Image.fromarray(aia)

im.convert('L').save('test2.png')

 

I'm thinking about adding a method to the BaseMap to help make it easier to achieve the above effect. One approach would be to

create a method like "save_image" to achieve the above effect. I wonder if we should also take a closer look at .plot() though

and consider making it possible to toggle on/off things like title, colorbar, and axes labels?

 

Keith

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

 

--

You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

--
You received this message because you are subscribed to the Google Groups "SunPy" group.
To post to this group, send email to su...@googlegroups.com.
To unsubscribe from this group, send email to sunpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sunpy?hl=en.

 

--

Keith Hughitt

unread,
Jan 20, 2012, 3:10:18 PM1/20/12
to su...@googlegroups.com
Probably a good idea. I will try and write a section on that for Linux. If others can help out with similar notes for windows/mac that would be useful.

Also, you might want to check out "pip". It is an alternative to easy_install which is part of the distutils package and supports uninstallings/upgrading packages.

Cheers,
Keith

Shih, Albert Y. (GSFC-6710)

unread,
Jan 20, 2012, 3:12:08 PM1/20/12
to su...@googlegroups.com

Hi, Keith,

     Oh, I agree: pip is definitely the way to go over easy_install for packages without Windows installers.  I do think that interacting with Windows installers are still preferable if possible, since Windows users will have a higher degree of familiarity with how to work with them.

Reply all
Reply to author
Forward
0 new messages