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

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

在plotly中的“%{variable}”語法中可以使用什么變量

在plotly中的“%{variable}”語法中可以使用什么變量

慕沐林林 2023-09-21 17:02:26
問題是關于'%{variable}'下面引用的代碼中的語法,特別是函數中的和hovertemplate, textkwargscustomdatago.Bar()fig = go.Figure(    data=[        go.Bar(            name=coln, x=df.index, y=df[coln],             # coln means columnName                        hovertemplate = '%{x}' + '%{y}' # <============= this thing here            + '%{text}' + '%{customdata} + %{coln}',             # can access x,y,text,customdata. ie. all kwargs of this trace ("trace" is a plotly technical term, meaning, this bar plot)            # can NOT access any other my own variables like df or coln            customdata = [coln]*len(df.index),            text = [                '<b>colname==</b>:'+ another_df.loc[coln] + somefunction(coln)                # can only do things around df and coln, not x nor y   <============= and this thing here            ]*len(df.index),        ) for coln in df.columns    ],  )我無法規避的限制:在hovertemplatekwarg 中,我只能訪問x, y, text and customdata,%{coln}無法工作在textand customdatakwarg 中,我可以訪問colnand df,但是,%{x} 不起作用,x因為變量不起作用。另外,text這 customdata是我可以訪問的唯一地方coln,沒有其他“自定義數據持有者”text 另外,由于我們在這里,構建customdata一個與繪圖 x 軸長度相同的列表非常麻煩,即*len(df.index)我正在尋找的是:類似 python f 字符串的f"{x} {y} {coln}"語法,可以自由訪問x, y, coln,以及在跟蹤中自由使用x y, like:df.at[x, y]或。myfoo(x, y)PS雖然問題要求的是特定的東西,但主要目標是尋求一種更靈活的方式來構建懸停文本。由于我是plotly和javascript的新手,我可能會錯過很多大圖片,所以我可能會以錯誤的方式處理這種情況,如果是這種情況,請也指出這一點。
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

通常,texttemplate和hovertemplate可以訪問跟蹤級別的任何屬性,即go.Bar()本例中的屬性。因此x、y、 和customdata是可訪問的:


import plotly.graph_objects as go


go.Figure(go.Bar(

    x=["a","b"], y=[1,2], customdata=[["hi", "there"], ["hello", "there"]],

    hovertemplate="x is %{x}, y is %{y}, custom1 is %{customdata[1]}",

    texttemplate="x is %{x}, y is %{y}, custom1 is %{customdata[1]}",

    textposition="auto"

))

customdata這里可以是列表的列表(如上所述)或字典的列表,如下訪問:


import plotly.graph_objects as go


go.Figure(go.Bar(

    x=["a","b"], y=[1,2], customdata=[dict(hi="there"), dict(hi="here")],

    hovertemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",

    texttemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",

    textposition="auto"

))

該customdata格式與 Pandas 的格式兼容to_dict('records'):


import plotly.graph_objects as go

import pandas as pd


df = pd.DataFrame(dict(

    x=["a","b"], y=[1,2], hi=["there", "here"]

))


go.Figure(go.Bar(

    x=df.x, y=df.y, customdata=df[["hi"]].to_dict('records'),

    hovertemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",

    texttemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",

    textposition="auto"

))


查看完整回答
反對 回復 2023-09-21
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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