[英]Plotly Express: Plot Scatter MapBox with Feature Colors from DataFrame Column (px.scatter_mapbox)
How do you make Plotly Express plot points in the colors specified in a DataFrame column? DataFrame列中指定的colors如何让Plotly表示plot点?
I have a DataFrame with columns: names, latitudes, longitudes, and colors as below:我有一个 DataFrame 列:名称、纬度、经度和 colors,如下所示:
df = pd.DataFrame({'name': pd.Series((1, 2, 3, 4), dtype='int32'),
'LAT': pd.Series((40.62718, 40.63718, 40.74718, 40.75718), dtype='float64'),
'LON': pd.Series((-73.29494, -73.30494, -73.31494, -73.32494), dtype='float64'),
'color': ('#222A2A', '#2E91E5', '#FC0080', '#750D86')})
I cannot get Plotly Express to plot points in the color that I have designated in my DataFrame. It plots whatever color it wants, even with color_discrete_map='identity'
and color='colors'
, which is what the docs say to do.我无法以我在 DataFrame 中指定的颜色获得 Plotly Express 到 plot 点。它绘制它想要的任何颜色,即使使用
color_discrete_map='identity'
和color='colors'
,这是文档所说的。
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 颜色用于覆盖 color_discrete_sequence 以将特定的 colors 分配给与特定值对应的标记。 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.
或者,如果颜色的值是有效的 colors,则可以传递字符串“identity”以直接使用它们。
( https://plotly.github.io/plotly.py-docs/generated/plotly.express.scatter_mapbox.html )( https://plotly.github.io/plotly.py-docs/generated/plotly.express.scatter_mapbox.html )
fig = px.scatter_mapbox(df, lat="LAT", lon="LON", hover_name='name',
hover_data=["name", "LAT", "LON", "color"],
color_discrete_map='identity', color='color', center=dict(lat=40.95004, lon=-73.07169),
zoom=7, height=300)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(autosize=False, width=1500, height=500)
fig.show()
Please help.请帮忙。
I did this just now in a plot I'm using.我刚才在我正在使用的 plot 中做了这个。 In my dataframe I have a series called Color that is just 'green' or 'blue' depending on the type of plot point.
在我的 dataframe 中,我有一个名为 Color 的系列,它只是“绿色”或“蓝色”,具体取决于 plot 点的类型。
My fig assignment looks like this:我的无花果作业如下所示:
fig = px.scatter_mapbox(branches, lat="lat", lon="long", hover_name="nm_short", hover_data=["nm_short", "city", "State"],
color_discrete_sequence=[df.Color], zoom=3.5, height=300)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.