poltly express`sctity_mapbox`带有`animation_frame`和'颜色'

发布于 2025-01-18 17:54:38 字数 2214 浏览 5 评论 0 原文

如果具有带有列的数据:“ e_lat”,“ e_lon”,“année”和“expéditeur”。 我正在尝试制作一个scats_mapbox,并用“Année”动画制作框架,并基于'expéditeur'颜色(这是分类的,是人的名字)。

# I created a row df['_color'] = df['Expéditeur'] because in the end I want to change the way _color is defined.

fig = px.scatter_mapbox(df,
                        lat='e_lat',
                        lon='e_lon',
                        hover_name='Ville Expéditeur',
                        hover_data=['Expéditeur', 'Destinataire', 'Ville Expéditeur', 'Année'],
                        animation_frame='Année',
                        mapbox_style='carto-positron',
                        zoom=4,
                        color='_color',
                        )
fig.show()

问题是,我的麻烦是我得到了类似的东西这 : (只有第一种颜色显示的点) 但缺少一些要点。

“动画帧可以, 如果我评论 animation_frame ='année'',,我会得到: “颜色还可以,但当然没有动画框架。”

我做了很多研究完全相同并且有不同的结果([例如](例如]( https://towardsdatascience.com/how-to-animate-scatterplots-mapbox-mapbox-usish-plotly-plotly-3bb49fe6a5d 此stackoverflow问题,...)

我的情节lib是最新的。 。

我在这里做错了什么?很多。


[编辑] 实际上,这与这里(未解决)。 这似乎是因为A 已记录的plotly 的错误。 “如果所有颜色都用于第一帧,则问题消失了。”

我通过添加“假数据”(在地图的范围之外)解决了它:在动画的第一帧中,每种颜色一个。

new_rows = []
for color in df['_color'].unique():
    new_row = {'_color': color, 'Année': df['Année'].min(), 'lat': 0, 'lon': 0}
    new_rows.append(new_row)
df = df.append(new_rows, ignore_index=True)

希望这对某人有帮助。

If have data with columns : 'e_lat', 'e_lon', 'Année' and 'Expéditeur'.
I'm trying to make a scatter_mapbox, with frames animated by 'Année", and color based on 'Expéditeur' (which is categorical, the names of the persons).

# I created a row df['_color'] = df['Expéditeur'] because in the end I want to change the way _color is defined.

fig = px.scatter_mapbox(df,
                        lat='e_lat',
                        lon='e_lon',
                        hover_name='Ville Expéditeur',
                        hover_data=['Expéditeur', 'Destinataire', 'Ville Expéditeur', 'Année'],
                        animation_frame='Année',
                        mapbox_style='carto-positron',
                        zoom=4,
                        color='_color',
                        )
fig.show()

The trouble is I get something like this :
(only the points that have the first color are showing)
Animation frame ok but missing some points.

If I comment out color='_color',, I get :
Animation frame ok but no color of course.
If I comment out animation_frame='Année'',, I get :
Color are ok but no animation frames of course.

I did a lot of research but it always seems to me that people are doing exactly the same and have different results ([this tutorial for example](https://towardsdatascience.com/how-to-animate-scatterplots-on-mapbox-using-plotly-express-3bb49fe6a5d, this stackoverflow issue, ...)

My plotly lib is up-to-date.

I tried a lot of different things with `animation group, color_discrete_map, color_discrete_sequence etc. but without any success).

What am I doing wrong here ? Thx a lot.


[EDIT]
In fact it's exaclty the same problem than here (not resolved).
This seems to be because of a documented bug of Plotly.
"If all colors are used for the first frame, the problem disappears."

I solved it by adding "fake data" (outside of the scope of the map) : one for each color, in the first frame of the animation.

new_rows = []
for color in df['_color'].unique():
    new_row = {'_color': color, 'Année': df['Année'].min(), 'lat': 0, 'lon': 0}
    new_rows.append(new_row)
df = df.append(new_rows, ignore_index=True)

Hope this will help someone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2025-01-25 17:54:38

实际上,这与这里(未解决)。
这似乎是因为A 已记录的plotly 的错误。
“如果所有颜色都用于第一帧,则问题消失了。”

我通过添加“假数据”(在地图的范围之外)解决了它:在动画的第一帧中,每种颜色一个。

new_rows = []
for color in df['_color'].unique():
    new_row = {'_color': color, 'Année': df['Année'].min(), 'lat': 0, 'lon': 0}
    new_rows.append(new_row)
df = df.append(new_rows, ignore_index=True)

希望这对某人有帮助。

In fact it's exaclty the same problem than here (not resolved).
This seems to be because of a documented bug of Plotly.
"If all colors are used for the first frame, the problem disappears."

I solved it by adding "fake data" (outside of the scope of the map) : one for each color, in the first frame of the animation.

new_rows = []
for color in df['_color'].unique():
    new_row = {'_color': color, 'Année': df['Année'].min(), 'lat': 0, 'lon': 0}
    new_rows.append(new_row)
df = df.append(new_rows, ignore_index=True)

Hope this will help someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文