简体   繁体   English

如何正确使用颜色图来绘制表达线地图框?

[英]How to correctly use colormaps for plotly express line mapbox?

I have trouble getting plotly.express.line_mapbox() present the lines with correct colors.我无法让plotly.express.line_mapbox()以正确的颜色呈现线条。 The lines have a value 0..100%, which represents the usage of each line.线条的值为 0..100%,代表每条线条的使用情况。 From other SO questions and websites I am approaching it like this:从其他 SO 问题和网站,我是这样处理的:

norm = matplotlib.colors.Normalize(0, 100)
colors = [[norm(0), "green"],[norm(60), "yellow"],[norm(100), "red"]]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)

Then I specify each color by:然后我通过以下方式指定每种颜色:

RGBa = cmap(percUsage)    
colors.append(f'rgb({int(255 * RGBa[0])}, {int(255 * RGBa[1])}, {int(255 * RGBa[2])})')

Where percUsage = 0..100 .其中percUsage = 0..100

In respect to that, I am building my Dataframe with each lines data and then create the figure. Dataframe而言,我正在使用每行数据构建我的数据Dataframe ,然后创建图形。

df = pd.DataFrame(data=dict(name=names, lat=lats, lon=lons, load=loads, color=colors, hover=hoverText))
fig = px.line_mapbox(df, lat='lat', lon='lon', line_group='name', color='color')

But while c.test_colormap(cmap) shows the colors as expected, plotly.express.line_mapbox() behaves totally weird in terms of colors.但是,虽然c.test_colormap(cmap)按预期显示颜色,但plotly.express.line_mapbox()在颜色方面表现得非常奇怪。 The screenshot shows a line with the usage value of 9. I do not get, why it prints a blue color, which should not even be in my spectrum of color.屏幕截图显示了一条使用值为 9 的行。我不明白为什么它会打印蓝色,甚至不应该在我的颜色范围内。 Others are colored green or red but in no relation to percUsage .其他的颜色为绿色或红色,但与percUsage I seem to miss something important here.我似乎错过了一些重要的东西。

Any help is appreciated :)任何帮助表示赞赏:)

c.test_colormap(cmap) plotly.express.line_mapbox()

So i managed to solve my problem and would like to share my findings, in case anyone stumbles on this question.所以我设法解决了我的问题,并想分享我的发现,以防有人偶然发现这个问题。 Here you find the docs .在这里您可以找到文档

TL;DR and my key takeaways: TL;DR和我的主要收获:

Using a discrete color map( dict ) with str -keys and hex-colors( str ), while the dataframes 's(df) "colors" column is filled with the corresponding keys used in discr_map .使用离散色彩映射( dict )与str -keys和己颜色( str ),而dataframes的(DF)‘颜色’列中填充了所用的相应的密钥discr_map

from colormap import rgb2hex
import matplotlib.colors

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("gyr", [[0., 'green'], [0.5, 'yellow'], [1.0, 'red']], N=101)
discr_map = {}
for i in range(0, 101, 1):
    discr_map.update({"c"+str(i): rgb2hex(int(255 * cmap(i)[0]), int(255 * cmap(i)[1]), int(255 * cmap(i)[2]))})

fig = px.line_mapbox(df, lat='lat', lon='lon', line_group='name', color='color', color_discrete_map=discr_map)

I do not really understand, why the dataframe 's colors column does not work with the format rgb(255,100,0) .我真的不明白,为什么在dataframecolors列不以格式工作rgb(255,100,0) Maybe hex format is needed here, as well - I'll check that out sometime.也许这里也需要十六进制格式 - 有时间我会检查一下。 This was confusing, since other mapbox s can handle rgb format.这令人困惑,因为其他mapbox可以处理rgb格式。 For that reason it was falling back to its standard color range, which obviously confused me.出于这个原因,它正在回落到它的标准颜色范围,这显然让我感到困惑。

color (str or int or Series or array-like) – Either a name of a column in data_frame, or a pandas Series or array_like object. color (str or int or Series or array-like) – data_frame 中列的名称,或者 pandas Series 或 array_like 对象。 Values from this column or array_like are used to assign color to marks.此列或array_like 中的值用于为标记分配颜色。

Then I looked into the color_discrete_map argument and made that finally work.然后我查看了color_discrete_map参数并使其最终起作用。

color_discrete_map (dict with str keys and str values (default {})) – String values should define valid CSS-colors Used to override color_discrete_sequence to assign a specific colors to marks corresponding with specific values. color_discrete_map (dict with str keys and str values (default {})) – 字符串值应该定义有效的 CSS-colors 用于覆盖 color_discrete_sequence 以将特定颜色分配给与特定值对应的标记。 Keys in color_discrete_map should be values in the column denoted by color. color_discrete_map 中的键应该是颜色表示的列中的值。 Alternatively, if the values of color are valid colors, the string 'identity' may be passed to cause them to be used directly.或者,如果颜色的值是有效颜色,则可以传递字符串 'identity' 以直接使用它们。

This is how I create the discrete colormap.这就是我创建离散颜色图的方式。 I think the most important catch here, is to use N=101 (or whatever value range you need) to normalize the spectrum to values ranging from 0..100 .我认为这里最重要的问题是使用N=101 (或您需要的任何值范围)将频谱归一化为0..100范围内的值。

# initialize discrete colormap
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("gyr", [[0., 'green'], [0.5, 'yellow'], [1.0, 'red']], N=101)
discr_map = {}
for i in range(0, 101, 1):
    discr_map.update({"c"+str(i): rgb2hex(int(255 * cmap(i)[0]), int(255 * cmap(i)[1]), int(255 * cmap(i)[2]))})

Which is then passed into the figures color_discrete_map argument:然后将其传递给数字color_discrete_map参数:

fig = px.line_mapbox(df, lat='lat', lon='lon', line_group='name', color='color', color_discrete_map=discr_map)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM