Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

python - Map values to colors in matplotlib

I have a list of numbers as follows:

lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 
       1.2427632053442292, 1.1809971732733273, 0.91960143581348919, 
       1.1106310149587162, 1.1106310149587162, 1.1527004351293346, 
       0.87318084435885079, 1.1666132876686799, 1.1666132876686799]

I want to convert these numbers to colors for display. I want gray scale but when I am using these numbers as it is, it gives me an error:

ValueError: to_rgba: Invalid rgba arg "1.35252299785"
to_rgb: Invalid rgb arg "1.35252299785"
gray (string) must be in range 0-1 

...which I understand is due to it exceeding 1.

I next tried to divide the items in the list with the highest number in the list to give values less than 1. But this gives a very narrow color scale with hardly any difference between values.

Is there any way in which I can give some min and max range to colors and convert these values to color? I am using matplotlib.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The matplotlib.colors module is what you are looking for. This provides a number of classes to map from values to colourmap values.

import matplotlib
import matplotlib.cm as cm

lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 1.2427632053442292, 
       1.1809971732733273, 0.91960143581348919, 1.1106310149587162, 1.1106310149587162, 
       1.1527004351293346, 0.87318084435885079, 1.1666132876686799, 1.1666132876686799]

minima = min(lst)
maxima = max(lst)

norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.Greys_r)

for v in lst:
    print(mapper.to_rgba(v))

The general approach is find the minima and maxima in your data. Use these to create a Normalize instance (other normalisation classes are available, e.g. log scale). Next you create a ScalarMappable using the Normalize instance and your chosen colormap. You can then use mapper.to_rgba(v) to map from an input value v, via your normalised scale, to a target color.

for v in sorted(lst):
    print("%.4f: %.4f" % (v, mapper.to_rgba(v)[0]) )

Produces the output:

0.8732: 0.0000
0.9196: 0.0501
1.1106: 0.2842
1.1106: 0.2842
1.1527: 0.3348
1.1666: 0.3469
1.1666: 0.3469
1.1810: 0.3632
1.2085: 0.3875
1.2133: 0.3916
1.2428: 0.4200
1.9378: 1.0000

The matplotlib.colors module documentation has more information if needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...