Hi.
i wanted to make a colorbar for a dataset. dataset has 50 different values.
so instead of entering a different color for 50 values one by one, i tried
the code below.
here is what happens:
1- two different data value is painted same color.
changing the color number to some value, sometimes works, sometimes doesnt.
for example, if i change the N (number of colors) in the code below, from
50 to 52,
problem continues, but with different indices. (29-30) are same instead of
(28-29).
but if i change it to 60 colors, problem "seems to" go away for given data
values in the example.
2- colors in image are not "exactly" taken from colorbar. they are
different a tiny bit.
if you check that darkblue color color picker, there is no such color in
colorbar!
second 'data' line (RGB 21-1-167)
the most close one is color index 28 (RGB 22-2-168)
and even correctly indexed data colors are off a little bit (RGB-values, i
mean).
PS: since colors are very similar and look same, you need to use
a RGB color picker/finder app. (i use Color Selector 1.0)
thanks in advance.
#
===========================================================================
from matplotlib import colors, colorbar, pyplot
import numpy as np
# make a dict for colors.
color_info = {}
color_info["red"] = [[0.0, 0.00, 0.00], # black
[0.2, 0.40, 0.40], # brown
[0.4, 0.60, 0.60], # purple
[0.6, 0.00, 0.00], # darkblue
[0.7, 0.00, 0.00], # green
[0.8, 1.00, 1.00], # yellow
[0.9, 1.00, 1.00], # red
[1.0, 1.00, 1.00]] # white
color_info["green"] = [[0.0, 0.00, 0.00],
[0.2, 0.20, 0.20],
[0.4, 0.05, 0.05],
[0.6, 0.00, 0.00],
[0.7, 1.00, 1.00],
[0.8, 1.00, 1.00],
[0.9, 0.00, 0.00],
[1.0, 1.00, 1.00]]
color_info["blue"] = [[0.0, 0.00, 0.00],
[0.2, 0.00, 0.00],
[0.4, 0.70, 0.70],
[0.6, 0.65, 0.65],
[0.7, 0.00, 0.00],
[0.8, 0.00, 0.00],
[0.9, 0.18, 0.18],
[1.0, 1.00, 1.00]]
# divide colorbar into 50 colors. you can change this (Ex: N=15 and use
data2)
my_cmap = colors.LinearSegmentedColormap('my_colormap', color_info, N=50)
N = my_cmap.N
# we put tickmarks into the middle of colors, by shifting half a segment.
ticklabels = np.arange(N)
ind = np.arange(N)
ticks = [(i / float(N)) + (1 / float(2 * N)) for i in ind]
def my_cbar(cbar_ax, cmap=my_cmap):
"""adds cbar to image."""
base = colorbar.ColorbarBase(cbar_ax, cmap=my_cmap,
ticks=ticks,
orientation="horizontal")
xlabels = cbar_ax.get_xticklabels()
for label in xlabels:
label.set_rotation(60)
base.set_ticklabels(ticklabels)
# labels are very close so dont forget to maximize plot window to see
better.
fig = pyplot.figure(figsize=(7, 7))
data = [[27, 28],
[28, 29], # both are same colors!
[29, 30],
[30, 31],
[31, 32],
[32, 33]]
# data2 is just to show it works as expected for N=15.
data2 = [[7.999, 8],
[8.999, 9],
[9.999, 10],
[10, 11],
[11, 12],
[12, 13]]
# with these vmin/vmax, data value 0-0.99999 goes to 'color index 0',
# data value X - X.9999 goes to 'color index X' ...etc
# (change 'data' to 'data2' when N=15)
pyplot.imshow(data, cmap=my_cmap,
vmin=0, vmax=N, interpolation="nearest")
cx = fig.add_axes((0.2, 0.05, 0.6, 0.03))
my_cbar(cx, cmap=my_cmap)
# but data values 28 and 29 goes to same color. maybe this happens for other
# data values as well ???
# similarly interesting, if you check that darkblue color with a
# rgb color picker/reader, there is no such color in colorbar! the most
close
# one is color index 28.
# RGB values ->>> 'data[1, :]=(21, 1, 167)' vs 'color index 28=(22, 2, 168)'
# and even correctly indexed colors RGB values are off a little bit.
pyplot.show()
--
Yasin Selçuk Berber
"Bismillah, her hayrın başıdır."
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users