如何正确显示 plotly density_mapbox?
Posted
技术标签:
【中文标题】如何正确显示 plotly density_mapbox?【英文标题】:How to correctly display plotly density_mapbox? 【发布时间】:2021-05-08 17:07:33 【问题描述】:我开始学习情节色标。 我有这个代码:
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.default = 'firefox'
fig = px.density_mapbox(df_chestie_psq_2018, lat='Lat', lon='Long', z='Psq', radius=30,
center=go.layout.mapbox.Center (
lat=44.439663,
lon=26.096306
), zoom=10, color_continuous_scale= [
[0.0, "green"],
[0.5, "green"],
[0.51111111, "yellow"],
[0.71111111, "yellow"],
[0.71111112, "red"],
[1, "red"]],
opacity = 0.5
)
fig.update_layout(mapbox_style="dark", mapbox_accesstoken='some_token')
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()
df_chestie_psq_2018 是一个数据帧,我想通过 Psq 区分该数据帧中的值。问题是它显示了以下地图:
问题是绿色应该是主色,如您所见,它显示红色。另外,如果我放大,颜色会消失。我只想看到一些值之间的绿色值,其他值是黄色,剩下的是红色。但我可以成功地做到这一点。
具有相同问题且易于重现的简单示例:
import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = px.density_mapbox(df, lat='centroid_lat', lon='centroid_lon', z='car_hours', radius=30,
color_continuous_scale= [
[0.0, "green"],
[0.5, "green"],
[0.51111111, "yellow"],
[0.71111111, "yellow"],
[0.71111112, "red"],
[1, "red"]],
opacity = 0.5
)
fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()
我只想根据 car_hours 值查看绿色、红色和黄色。
【问题讨论】:
嗨,您介意生成mcve吗?特别是,为了重现它,我们需要您共享数据。 编辑:我添加了可以复制的代码 【参考方案1】:您应该改用 scatter_mapbox
import plotly.io as pio
import plotly.express as px
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
df = px.data.carshare()
fig = px.scatter_mapbox(df, lat='centroid_lat', lon='centroid_lon', color='car_hours',
color_continuous_scale= [
[0.0, "green"],
[0.5, "green"],
[0.51111111, "yellow"],
[0.71111111, "yellow"],
[0.71111112, "red"],
[1, "red"]],
opacity = 0.5
)
fig.update_layout(mapbox_style="carto-darkmatter")
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()
enter image description here
【讨论】:
以上是关于如何正确显示 plotly density_mapbox?的主要内容,如果未能解决你的问题,请参考以下文章
Dash Plotly中的烛台图和折线图,它会随着回调而更新,但会消失
Pandas、matplotlib 和 plotly - 如何修复系列图例?
python使用matplotlib可视化包含倒影的柱状图(bar plot with shadow)配置rcParams坐标轴正确显示负号(-)