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?
What is the easiest way to display an array with only very few different values as an image plot, e.g. with imshow from matplotlib.pyplot, assigning fixed colors to each of the values that show up in the array? For instance, if the array contained only the binary values 0 and 1, I would like to be able to draw all value-0-pixels in yellow and all value-1-pixels in green, say. Can this be achieved using a customised colormap or is it necessary to write a new array containing the appropriate rgb tuple for each original value?
Some of the standard matplotlib cmaps, such as viridis or jet show dark colors in small values. While this is what I need, I like them to show nothing, i.e. white background if the value is exactly zero. For non zero values the usual colors of that color map are fine.
Is it possible to do this?
You would rather mask zero out of your data, e.g. setting those values to nan or use a masked array. Then you can just set_bad("white") for your colormap.
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?
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