Search code examples
pythonmatplotlibaplpy

APLpy show markers normalized by a colormap


I am using APLpy to plot a fits file and want to overplot markers on that fits file at certain ra,dec values. I want the colours of the markers to be coded with another parameter, lets say a magnitude.

So what I do is I read the marker coordinates and the corresponding magnitudes from a textfile (positions.dat):

ra         = np.genfromtxt('positions.dat', dtype=float, comments='%', delimiter=';', missing_values='_', skip_header=1, usecols = (0))                                 
dec        = np.genfromtxt('positions.dat', dtype=float, comments='%', delimiter=';', missing_values='_', skip_header=1, usecols = (1))                                 
magnitude  = np.genfromtxt('positions.dat', dtype=float, comments='%', delimiter=';', missing_values='_', skip_header=1, usecols = (2))                                   

I define a colormap and its normalization:

cmap1 = mpl.cm.YlOrBr                                                           
norm1 = mpl.colors.Normalize(10,20)                                                           

My magnitudes in the positions.dat file are all between 10 and 20 to test the code.

I tried to plot the markers as follows:

fits1.show_markers(ra,dec, cmap=cmap1, norm=norm1, edgecolor=magnitude, facecolor='none', marker='x', s= 4, linewidths=0.8)

When I do this, I always get the error:

ValueError: Color array must be two-dimensional

The positions.dat file looks like that:

  ra    ;      dec     ;   magnitude
330.45  ;  -31.958333  ;      10.0
330.46  ;  -31.958333  ;      11.0
330.47  ;  -31.958333  ;      12.0
330.48  ;  -31.958333  ;      13.0
330.49  ;  -31.958333  ;      14.0
330.50  ;  -31.958333  ;      15.0
330.51  ;  -31.958333  ;      16.0
330.52  ;  -31.958333  ;      17.0
330.53  ;  -31.958333  ;      18.0
330.54  ;  -31.958333  ;      19.0
330.55  ;  -31.958333  ;      20.0

Solution

  • The issue is that matplotlib's scatter can't take values for edgecolor and facecolor that are not color arrays (that is, arrays of RGB values). The correct way to achieve what you are trying to do is to use scatter's c argument, so in this case:

    fits1.show_markers(ra,dec, cmap=cmap1, norm=norm1,
                       c=magnitude, facecolor='none',
                       marker='x', s=4, linewidths=0.8)