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
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.
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.
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
Related
I am using imshow() to create pseudo-coloured maps of matrices of values (2d numpy arrays). Using clim argument one can set the range of values (below 1 and 6) to be represented within the colour scale (see below). This way for example all outliers (whether 7 or 7000000) will be yellow. This skews the perception of the image, as the reader doesn't know if this pixel is 6 or 700000.
Does anyone know of any way to colour all values outside of this range some other fixed colour of choice, for example, magenta?
Heres my input image:
I am plotting histogram of this image using the following code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('red.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.show()
Here is the plotted histogram output: On the left hand side is the original histogram and on the right hand side is the zoomed version:
My starting point is 255 and ending point is zero.
All my important data lies on the range of 235 to 255. As at 235 the line becomes straight (pl. see right hand side of histogram)
I want to write a python - opencv code which finds out when red line of histogram becomes straight and once the number is found after which the line shows minimum deviation delete all the remaining pixels from the image. In above case delete pixels having value (0 to 235). How can this be achieved ?
Histogram is basically arrays (bins).
The opencv histogram bins you create, you can check for the number of values & mean values in each bin, and compare it with previous bin. (More like a sliding window). If you find the difference to be greater than a threshold, then consider them to be chosen bins(pixels).
This is a technique used to identify peaks in a 1D array.
In my Python code I draw some points on a canvas. To each point there is an associated quantity f(P) where f is a function taking values between [0,f_{max}].
I would like to color the points such that the color corresponds to the value of f(P), and the mapping should be continuous.
The issue is colors in python are represented in RGB format in other words a function of 3 variables, so I am unsure how to approach this.
You could map from HSV (hue, saturation, value) to RGB, varying the hue according to your scalar value and setting saturation and value to constants.
import colorsys
def scalar_to_rgb(scalar):
return colorsys.hsv_to_rgb(scalar, 1, 1)
Scale the value to between 0 and 1 before passing it to the function. The colours wrap around, 1 will give the same result as 0. If this is an issue you could use only part of the range between 0 and 1.
I have a (geographic) raster-image in RGB. I also have an external legend displaying the heights according to a certain color. In below figure, I have sampled this legend, hopefully revealing its RGB-characteristics. I have plotted these values to their actual height values on the X-axis.
Now, is it possible to directly derive height from the pixel's RGB-value? Ultimately, I'm looking for a simple formula which is able to translate my RGB values into one height value (e.g. H = aR + bG + c*B) Any hint or tips? Is it even possible at all?
In Matplotlib, I have several sets of points to fill in with different colours, one per set. Now, the number of sets varies, so I would like to make sure that that the same colour doesn't appear more than once in the same plot.
Right now I just do:
colors = itertools.cycle(['r','g','b','c','y','m'])
# ...
ax.plot(Xs_for_a_set, Ys_for_a_set, c=colors.next())
... but I am limited to that number of colors. From the documentation I don't get how to specify a random color in RGB...
You can specify the color as a hex value in a string.
color = '#eeefff'
Using this you essentially have access to all colors via RGB and can create as many colors as you need