Plotly中的Python_DF排序和customdata [英] Python_DF ordering and customdata in Plotly

查看:67
本文介绍了Plotly中的Python_DF排序和customdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中徘徊在错误的反射数据上有问题.请参阅每个块的注释代码.

I have issue with wrong reflection data in hovers in the code below. See code with comments for each block.

我在下面的代码中徘徊在错误的反射数据上有问题.请参阅每个块的注释代码.

I have issue with wrong reflection data in hovers in the code below. See code with comments for each block.

import plotly.express as px
import pandas as pd
import plotly.graph_objects as go

rows=[['501-600','15','122.58333','45.36667','Name1'],
      ['till 500','4','12.5','27.5','Name2'],
      ['more 601','41','-115.53333','38.08','Name3'],
      ['till 500', '26', '65.5', '29.5','Name4'],
      ['501-600','35','12.58333','55.36667','Name5'],
      ['more 601','9','55.53333','-38.08','Name6'],
      ]

colmns=['bins','data','longitude','latitude','names']
#Df creation
df=pd.DataFrame(data=rows, columns=colmns)
#Ordering for labels in legend
order = ['till 500', '501-600', 'more 601']
df = df.set_index('bins')
df_ordered = df.T[order].T.reset_index()
df_ordered = df_ordered.astype({"data": int})
#Plotting viz
fig=px.scatter_geo(df_ordered,lon='longitude', lat='latitude',color='bins',
                   color_discrete_sequence=px.colors.qualitative.Set1,
                   hover_name="names",
                   size='data',opacity=0.7,text='data',
                   projection="equirectangular",size_max=35,
                   )
#Adding custom data for hovers
fig.update_traces(customdata=df_ordered)
fig.update_traces(hovertemplate="<b>Name: %{customdata[4]} </b><br><br>Bin: %{customdata[0]}<br>"
                                "Data: %{customdata[1]:.2f}<extra></extra>")
#Adding marker labels
fig.add_trace(go.Scattergeo(lon=df_ordered["longitude"],
              lat=df_ordered["latitude"],
              text=df_ordered["names"],
              textposition="middle left",
              mode='text',
              textfont=dict(size=12,color="black"),
              showlegend=False,
              texttemplate="       %{text}",
              hoverinfo='skip',
              ))
fig.show()

因此,最后我想这个问题是由于订购引起的,也许我需要在customdata行中重新制作smth,但不知道如何解决它.感谢您提供修复方面的帮助.

So at the end I guess that this problems raise from ordering and maybe I need to remake smth in customdata lines, but can't understand how to fix it. Will appreciate for help in fixing it.

推荐答案

在这种情况下,我很难使用自定义悬停模板(您最终可以看到此

I had quite hard time playing with the custom hovertemplate in this case (you can eventually see this doc) But I think that I could achieve the output your looking for without add an extra trace.

fig=px.scatter_geo(df_ordered,
                   lon='longitude',
                   lat='latitude',
                   color='bins',
                   color_discrete_sequence=px.colors.qualitative.Set1,
                   hover_name="names",
                   size='data',
                   opacity=0.7,
                   text='names',
                   projection="equirectangular",
                   size_max=35,
                   # by default every column go to hover
                   # you can eventually use formatting here
                   hover_data={"longitude": False,
                               "latitude": False,
                               "names": False,
                               "data": ":.2f"},
                   # if you don't want to change column names
                   # you can just change them here
                   labels={"bins": "Bin",
                           "data": "Data"}
                   )

fig.update_traces(mode="markers+text",
                  textposition="middle left",
                  textfont=dict(size=12,
                                color="black")
                  showlegend=False,
                 )

# Here I just change the `=` for `: ` in every trace
for data in fig.data:
    data.hovertemplate = data.hovertemplate.replace("=", ": ")

fig.show()

更新我刚刚意识到,存在 labels hover_data 一起使用的错误,特别是如果您使用 labels 由于某些原因,未保留格式数据":":. 2f".可能的解决方法如下:

Update I just realized that there is a bug with labels used together with hover_data in particular if you use labels for some reasons the formatting "data": ":.2f" is not preserved. A possible workaround is the following

fig = px.scatter_geo(df_ordered,
                     lon='longitude',
                     lat='latitude',
                     color='bins',
                     color_discrete_sequence=px.colors.qualitative.Set1,
                     hover_name="names",
                     size='data',
                     opacity=0.7,
                     text='names',
                     projection="equirectangular",
                     size_max=35,
                     # by default every column go to hover
                     # you can eventually use formatting here
                     hover_data={"longitude": False,
                                 "latitude": False,
                                 "names": False,
                                 "data": ":.2f"}
                    )

fig.update_traces(mode="markers+text",
                  textposition="middle left",
                  textfont=dict(size=12,
                                color="black"),
                  showlegend=False,
                 )

# it's pretty verbose but now the output should be
# exactly as you expect
for data in fig.data:
    template = data.hovertemplate
    template = template.replace("<b>", "<b>Name: ")\
                       .replace("bins=", "Bin: ")\
                       .replace("data=", "Data: ")
    data.hovertemplate = template

fig.show()

这篇关于Plotly中的Python_DF排序和customdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆