tags:

views:

349

answers:

3

Hello, I am new to python and am having trouble finding the correct syntax to use. I want to plot some supernovae data onto a hammer projection. The data has coordinates alpha and beta. For each data point there is also a value delta describing a property of the SN. I would like to create a colour scale that ranges from min. value of delta to max. value of delta and goes from red to violet. This would mean that when I came to plot the data I could simply write:

subplot(111,projection="hammer")

p=plot([alpha],[beta],'o',mfc='delta')

where delta would represent a colour somewhere in the spectrum between red and violet. I.e if delta =0, the marker would be red and if delta =delta max. the marker would be violet and if delta =(delta. max)/2 the marker would be yellow.

Could anyone help me out with the syntax to do this?

Many thanks

Angela

+1  A: 

If you are thinking of a fixed color table, just map your delta values into the index range for that table. For example, you can construct a color table with color names recognized by your plot package:

>>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

The range of possible delta values, from your example, is 0 to delta.max. Mapping that to the length of the color tables, gives the step:

>>> step = delta.max / len(colors)

And the computation required to get a color name matching a given data point is:

>>> color = colors[math.trunc(data / step)]

This method works for any set of pre-selected colors, for example RGB values expressed as hex numbers.

A quick google search discovered Johnny Lin's Python Library. It contains color maps, including Rainbow (red to violet, 790-380 nm). You also need his wavelen2rgb.py (Calculate RGB values given the wavelength of visible light). Note that this library generates the colors as RGB triplets - you have to figure how your plotting library expects such color values.

gimel
Thank you, that is very helpful. I would like to use many colours, do you know if there is an inbuilt colour gradient so that instead of writing all the colours out that I want I can type:colors=[e.g. rainbow]where rainbow will be an array of equally graded colours (I would like about 100).
See added links to a python library that can generate color tables.
gimel
Thanks, I have used this link below:http://www.on-the-matrix.com/webtools/HtmlColorCodes.aspxto convert equally spaced hsl values into hex values and then placed them in a large array called colour as above. I will also have a look at the link you have posted.
A: 

I'm not familiar with plotting, but a nice method for generating rainbow colors is using the HSV (hue, saturation, value) colorspace. Set saturation and value to the maximum values, and vary the hue.

import colorsys

def color(value):
    return colorsys.hsv_to_rgb(value / delta.max / (1.1), 1, 1)

This wil get you RGB triplets for the rainbow colors. The (1.1) is there to end at violet at delta.max instead of going all the way back to red. So, instead of selecting from a list, you call the function.

You can also try experimenting with the saturation and value (the 1's in the function above) if the returned colors are too bright.

Petr Viktorin
+1  A: 
cyberthanasis