You're translating a histogram of your data into a colormap, yes?
The matplotlib histogram returns bins and patches, which you could translate into color intensities; but I bet scipy.stats.histogram would be easier. Then the bin centers are the segment boundaries of the colormap, and the weight in each bin is the respective color intensity.
Also, color has a finite extent but the bin weight might not. You'll need to choose a nominal max value to norm the colors to, and decide whether to use the same max value all the time (so early plots might all be light, late plots all dark) or calculate it from the data each time you plot (in which case the colorbar this month might not mean the same thing as the color bar last month).
I think using all three of RGB is too confusing -- do it bluescale or grayscale.
&C
···
On Nov 5, 2012, at 7:13 AM, rand0m@...4228... wrote:
Hi Chloe
Thank you for answering.
I agree the way you suggest. Currently I have done this:
import matplotlib
import matplotlib.pyplot as plt# http://matplotlib.org/examples/api/colorbar_only.html
#
http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap# The lookup table is generated using linear interpolation for each
primary color, with the 0-1 domain divided into any number of segments.
# x, y0, y1
cdict = {'red': [(0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)],'green': [(0.0, 0.0, 0.0),
(0.25, 0.0, 0.0),
(0.75, 1.0, 1.0),
(1.0, 1.0, 1.0)],'blue': [(0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 1.0, 1.0)]}# create colormap
my_cmap = matplotlib.colors.LinearSegmentedColormap("my_colormap",
cdict, N=256, gamma=1.0)# optional: register colormap
#plt.register_cmap(name='my_colormap', data=cdict)fig = plt.figure(figsize=(5,1))
fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99)
plt.axis("off")
import numpy as np
a = np.linspace(0, 1, 256).reshape(1,-1)
a = np.vstack((a,a))
plt.imshow(a, aspect='auto', cmap=my_cmap, origin='lower')plt.show()
Now the tricky part has still to be done. I have a varying number (ca.
500, increasing) of values between 60 and 90. Those values must be
represented in the colorbar. White if there is no value, blue towards
black the more values are in the same area.
For this, I guess, I have to set a x for each value (and three x since
the color is calculated using RGB). And the closer it is to the previous
one the more I have to calculate the color between blue and black.Or do you suggest another way to implement this?
I do not know of any other software that this issue has been implemented.
cheers!
On 10/26/2012 07:47 PM, Chloe Lewis wrote:
you'll be doing something like the second color bar, but making the
boundary and color definitions a lot more flexible. Where the discrete
color bar usescmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c'])
bounds = [1, 2, 4, 7, 8]you'll be making a whole LinearSegmentedColormap, see
http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap
and check out specifically the ascii-art explanation of interpolation between row[i] and row[i+1]. Red, green, blue will break based on your data density and how you want to express 'intensity'. And depending on whether you'll make it red-green-colorblindness neutral!
Interesting problem. Has it been implemented in some other software?
Chloe Lewis
PhD candidate, Harte Lab
Division of Ecosystem Sciences, ESPM
University of California, Berkeley
137 Mulford Hall
Berkeley, CA 94720
chlewis@...1016... <mailto:chlewis@…1016…>