Search code examples
pythonmatplotlibaplpy

aplpy show_polygons() with colorised faces


I've got some polygons I'd like to plot in Right Ascension/Declination space with APLpy, with the polygons colored by another 1D list, but I can't get show_polygons() to work.

I tried to adapt the answer from APLpy show markers normalized by a colormap , but when I run it, show_polygons() doesn't understand the kwargs cmap, norm, or c the same way show_markers() does.

My adapted, standalone script:

import aplpy, numpy
from numpy import array
import matplotlib.pyplot as plt
from matplotlib import cm, colors

polygons = [array([[ 46.33681474,  34.75536787],
   [ 45.04752709,  35.37650737],
   [ 44.63035494,  34.73768723],
   [ 46.33681474,  34.75536787]]), array([[ 46.45913142,  34.69050337],
   [ 45.04717721,  35.37189917],
   [ 44.6205633 ,  34.72362768],
   [ 46.45913142,  34.69050337]]), array([[ 46.52741447,  34.64997822],
   [ 45.04457814,  35.36619781],
   [ 44.60486296,  34.70107236],
   [ 46.52741447,  34.64997822]])]
zvalues = [  1.02018589e-10,   9.38471764e-12,   2.15806865e-11]

cmap1 = cm.YlOrBr
norm1 = colors.Normalize( numpy.min(zvalues), numpy.max(zvalues) )
fig   = aplpy.FITSFigure( numpy.zeros( (10,10) ) )
fig.show_polygons( polygons, cmap=cmap1, norm=norm1, c=zvalues, facecolor='none' )
plt.save( fname='plot.png' )

Running this causes show_polygons() to raise different AttributeErrors:

AttributeError: Unknown property cmap
AttributeError: Unknown property norm
AttributeError: Unknown property c

My versions of things:

$ python --version
Python 3.5.1 :: Continuum Analytics, Inc.
$ python
>>> import matplotlib
>>> print(matplotlib.__version__)
1.5.1
>>> import aplpy
>>> print(aplpy.__version__)
1.1.1

How can I get show_polygons() to work?


Solution

  • First off, the polygon coordinates you're trying to plot don't match with the pixel array you're plotting - although I assume that's not a problem for a real fits figure you're plotting. So you should change your code to something like this instead:

    # fixed so that the polygons fall inside the image
    fig = aplpy.FITSFigure(numpy.zeros((50,50)))
    # zoom in on the coord values of the polygons
    fig.recenter(45.5, 35, 1.5)
    

    Now, actually answering the question: different methods of aplpy.FITSFigure pass keyword arguments to different matplotib routines - so you shouldn't expect show_polygons and show_markers to behave in the same way. In fact, their docstrings state that show_poligons passes its kwargs to a PatchCollection class, while show_markers would send them along to plt.scatter. That's why the attribute errors keep happening.

    So, how to color the patches by a 1d color list then? As far as I know, there's no one-liner solution, but you can loop over the polygons and color them individually:

    for p, c in zip(polygons, zvalues):
            fig.show_polygons([p], facecolor=cmap1(norm1(c)),
                              edgecolor='none', alpha=0.3)
    plt.show()
    

    Which produces the following figure for me:

    those elusive polygons