Reputation:
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
Upvotes: 2
Views: 2378
Reputation: 415
Using the wavelen2rgb function of Johnny Lin's Python Library (as gimel suggested), the following code plots the SNs as filled circles. The code uses Tkinter which is always available with Python. You can get wavelen2rgb.py here.
def sn():
"Plot a diagram of supernovae, assuming wavelengths between 380 and 645nm."
from Tkinter import *
from random import Random
root = Tk() # initialize gui
dc = Canvas(root) # Create a canvas
dc.grid() # Show canvas
r = Random() # intitialize random number generator
for i in xrange(100): # plot 100 random SNs
a = r.randint(10, 400)
b = r.randint(10, 200)
wav = r.uniform(380.0, 645.0)
rgb = wavelen2rgb(wav, MaxIntensity=255) # Calculate color as RGB
col = "#%02x%02x%02x" % tuple(rgb) # Calculate color in the fornat that Tkinter expects
dc.create_oval(a-5, b-5, a+5, b+5, outline=col, fill=col) # Plot a filled circle
root.mainloop()
sn()
Here's the outpout:
alt text http://img38.imageshack.us/img38/3449/83921879.jpg
Upvotes: 1
Reputation: 67000
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.
Upvotes: 1
Reputation: 86472
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.
Upvotes: 2