簡體   English   中英

如何在 matplotlib 散點圖中標准化顏色圖?

[英]How can I normalize colormap in matplotlib scatter plot?

matplotlib 文檔詳細解釋了如何對 pcolormesh 的顏色圖進行標准化,但是如何正確地為散點圖執行此操作?

normalize = mcolors.Normalize(vmin=-1, vmax=1)
plt.scatter(x,y,z,cmap=colormap(normalize),marker='*',s=5)

不起作用(類型錯誤TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'

只是 z 數據不完全從 -1 到 1,我正在繪制多個數據集,這些數據集的限制在 +/- 0.93 - 98 左右,但我希望顏色以零為中心並從 -1 到 1 所以我對所有各種數據集都有相同的參考。

哦,當我不嘗試規范化時,我得到TypeError: scatter() got multiple values for keyword argument 's' 顯然我不知道如何在散點圖中使用顏色圖。

您使用的語法與鏈接文檔中的語法完全不同。 規范化散射或pcolor(網格)或任何其他標量可映射對象之間基本沒有區別。

總是如此

colormap = plt.cm.bwr #or any other colormap
normalize = matplotlib.colors.Normalize(vmin=-1, vmax=1)
plt.scatter(x, y, c=z, s=5, cmap=colormap, norm=normalize, marker='*')
# define the range of values in the data (100 is arbitrary)
my_range = np.linspace(-1,1,100)

# cmap is a function. It returns (rgba) colors base on a range 0-1. Therefore, 
# transform your values to 0-1 to use them as input for cmap() 
cmap = cm.get_cmap('viridis', 100)
my_transformed_range = (my_range - np.min(my_range)) / (np.max(my_range) - np.min(my_range))

# colors should be supplied as a single color or a list (here using the cmap fx)
plt.scatter(my_range, np.ones(100), color=[cmap(i) for i in my_transformed_range])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM