question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tight_layout() doesn't work properly on plots with a WCSAxes projection

See original GitHub issue

A Matpltolib Axes created with projection=wcs does not play well with Matplotlib’s tight_layout() method, that is supposed to re-size elements of a figure such that there are no overlapping labels/axes etc.

Consdier the following example:

import matplotlib.pyplot as plt

from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)

fig = plt.figure(figsize=(8, 6))
n = 2
for i in range(n):
    projection = wcs
    # projection = None
    plt.subplot(n, 1, i + 1, projection=projection)
    # plt.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    plt.grid(color='white', ls='solid')
    plt.xlabel('Galactic Longitude')
    plt.ylabel('Galactic Latitude')
    plt.title('WCSAxes plot')

fig.tight_layout()
plt.show()

projection=None, no tight_layout: Figure_1 projection=None, with tight_layout: Figure_1 (note how this re-organises things nicely) projection='wcs', no tight_layout: Figure_1 projection='wcs', with tight_layout: Figure_1 Note how things actually get worse!

  • astropy 3.1.2
  • Matplotlib 3.0.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
dstansbycommented, Oct 4, 2020

For reference, some simpler code to reproduce this:

import matplotlib.pyplot as plt

from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)

fig = plt.figure(figsize=(8, 6))
axs = [fig.add_subplot(2, 1, i, projection=wcs) for i in [1, 2]]

fig.tight_layout()
plt.show()
1reaction
dstansbycommented, Oct 5, 2020

As I slowly descend into madness trying to work this out, a note that the end goal to get this working is to have TickLabels and AxisLabels have their own get_tight_bbox() methods that can calculate their own Bboxes. This is going to need a lot of refactoring, but I suspect in the long run it should significantly tidy up the WCSAxes code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python Matplotlib tight_layout() never work well - Stack Overflow
Summary: I want to use Matplotlib.tight_layout() to automatically optimize the layout of figures that contains any ...
Read more >
Making plots with world coordinates (WCSAxes) — Astropy v5.2
Making plots with world coordinates (WCSAxes)¶. WCSAxes is a framework for making plots of Astronomical data in Matplotlib. It was previously distributed as ......
Read more >
Tutorial maputils module — Kapteyn Package 2.3b6 ...
FITSimage.set_imageaxes() Then a helper function to get the right input is available. It is called maputils.prompt_imageaxes() which works ...
Read more >
Tutorial maputils module — Kapteyn 3.0.0-beta documentation
FITSimage.set_imageaxes() Then a helper function to get the right input is ... is always plotted as a straight line, whatever the projection (so...
Read more >
From sky coordinates to pixels (and back) - Joseph Long
She had some Herschel data in a FITS image with world coordinate system information (WCS) as well as some derived data in pixel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found