亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly 中的 Python_DF 排序和自定義數據

Plotly 中的 Python_DF 排序和自定義數據

慕標5832272 2022-12-20 15:18:11
我在下面的代碼中懸停時遇到錯誤的反射數據問題。請參閱每個塊的注釋代碼。我在下面的代碼中懸停時遇到錯誤的反射數據問題。請參閱每個塊的注釋代碼。import plotly.express as pximport pandas as pdimport plotly.graph_objects as gorows=[['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 creationdf=pd.DataFrame(data=rows, columns=colmns)#Ordering for labels in legendorder = ['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 vizfig=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 hoversfig.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 labelsfig.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()所以最后我猜想這個問題是由訂購引起的,也許我需要在自定義數據行中重新制作 smth,但無法理解如何解決它。將感謝您幫助修復它。
查看完整描述

1 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

在這種情況下,我很難使用自定義懸停模板(你最終可以看到這個文檔)但我認為我可以在不添加額外跟蹤的情況下實現你正在尋找的輸出。


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”未保留??赡艿慕鉀Q方法如下


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()


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號