[英]Python matplotlib barbs/quiver map colors to different sets of values
[英]Map values to colors in matplotlib
我有一個數字列表如下:
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]
我想將這些數字轉換為顏色以供顯示。 我想要灰度,但當我使用這些數字時,它給了我一個錯誤:
ValueError: to_rgba: Invalid rgba arg "1.35252299785"
to_rgb: Invalid rgb arg "1.35252299785"
gray (string) must be in range 0-1
...我明白是因為它超過了1。
我接下來試圖將列表中的項目除以列表中的最大數字,以給出小於1的值。但是這給出了非常窄的顏色標度,幾乎沒有值之間的任何差異。
有什么方法可以給出一些最小和最大范圍的顏色並將這些值轉換為顏色? 我正在使用matplotlib。
matplotlib.colors
模塊正是您所需要的。 這提供了許多類,可以從值映射到colourmap值。
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))
一般方法是在數據中找到minima
和maxima
。 使用這些來創建Normalize
實例(可以使用其他規范化類,例如對數比例)。 接下來,使用Normalize
實例和您選擇的ScalarMappable
映射創建ScalarMappable
。 然后,您可以使用mapper.to_rgba(v)
從輸入值v
(通過標准化比例mapper.to_rgba(v)
映射到目標顏色。
for v in sorted(lst):
print("%.4f: %.4f" % (v, mapper.to_rgba(v)[0]) )
產生輸出:
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
如果需要, matplotlib.colors
模塊文檔包含更多信息。
色彩映射是強大的,但(a)你經常可以做一些更簡單的事情,(b)因為它們很強大,它們有時做的比我想象的要多。 擴展mfitzp的例子:
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)
for v in lst:
print(mapper.to_rgba(v))
# really simple grayscale answer
algebra_list = [(x-minima)/(maxima-minima) for x in lst]
# let's compare the mapper and the algebra
mapper_list = [mapper.to_rgba(x)[0] for x in lst]
matplotlib.pyplot.plot(lst, mapper_list, color='red', label='ScalarMappable')
matplotlib.pyplot.plot(lst, algebra_list, color='blue', label='Algebra')
# I did not expect them to go in opposite directions. Also, interesting how
# Greys uses wider spacing for darker colors.
# You could use Greys_r (reversed)
# Also, you can do the colormapping in a call to scatter (for instance)
# it will do the normalizing itself
matplotlib.pyplot.scatter(lst, lst, c=lst, cmap=cm.Greys, label='Default norm, Greys')
matplotlib.pyplot.scatter(lst, [x-0.25 for x in lst], marker='s', c=lst,
cmap=cm.Greys_r, label='Reversed Greys, default norm')
matplotlib.pyplot.legend(bbox_to_anchor=(0.5, 1.05))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.