Setting color scale in ipython
I am new to python and am having a hard time finding the correct syntax to use. I want to plot some supernova data on the hammer projection. The data has coordinates alpha and beta. For each data point, there is also a delta value that describes the SN property. I would like to create a color scale that ranges from min. delta value up to max. delta value and goes from red to purple. This would mean that when I started plotting the data, I could simply write:
subplot (111, projection = "hammer")
p = plot ([alpha], [beta], 'o', mk = 'delta')
where delta will represent a color somewhere in the spectrum between red and purple. Ie, if delta = 0, the marker will be red, and if delta = delta max. the marker will be purple, and if delta = (delta max) / 2, the marker will be yellow.
Can anyone help me with the syntax to do this?
Many thanks
Angela
If you are thinking of a fixed color table, just map your delta values โโacross the range of indices for that table. For example, you can create a color table with the color names recognized by your plot package:
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
The range of possible values delta
, from your example, is 0
to delta.max
. Comparing this along the length of the color tables produces step
:
>>> step = delta.max / len(colors)
And the calculation required to get the name of the color corresponding to a given point data
is:
>>> color = colors[math.trunc(data / step)]
This method works for any set of preselected colors, such as RGB values โโexpressed as hexadecimal numbers.
A quick google search discovered the Johnny Lin Python Library . It contains colored cards including Rainbow (red to violet, 790-380 nm)
. You will also need wavelen2rgb.py
(calculate RGB values โโbased on visible light wavelength). Note that this library generates colors as RGB triggers - you need to understand how your graphics library expects such color values.
source to share
I'm not familiar with graphics, but a good way to generate rainbow colors is to use the HSV (hue, saturation, value) color space. Set saturation and value to their maximum values โโand change hue.
import colorsys
def color(value):
return colorsys.hsv_to_rgb(value / delta.max / (1.1), 1, 1)
This will help you RGB triplets for rainbow colors. (1.1) ends in purple at delta.max rather than reverting to red completely. Thus, instead of selecting from the list, you call the function.
You can also try experimenting with saturation and value (1 in the function above) if the returned colors are too bright.
source to share
Using the Johnny Lin Python library's wavelen2rgb function (as Gimel suggested), the following code displays SN as filled circles. The code uses Tkinter, which is always available from 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:
source to share