簡體   English   中英

使用按鈕和自定義數據更改可見的跟蹤 - python plotly

[英]Change which traces are visible with button and customdata - python plotly

我在 python-plotly 中有一個項目符號圖,我想使用一個查看 customdata 字段的按鈕來更改哪些痕跡是可見的。

整個圖應顯示用戶在按鈕/下拉列表中選擇的時間段的 11 個項目符號圖(10 個位置中的每個位置 1 個,然后是總計)。 我在制作圖形的地方設置得很好,添加了每個跟蹤(對於一個時期),然后fig.show()在那個時期看起來很棒。 為了嘗試完成接下來的步驟,我添加了所有周期的跟蹤(我有 2 + YTD 總共 3 個周期,乘以 11 個圖表,每個圖表有 33 個跟蹤)並將它們全部設置為visible=False 當我運行fig.show()時,它們不可見,正如預期的那樣。

為了嘗試存儲我的期間信息,對我來說最有意義(對建議開放)使用customdata上的自定義數據並將其設置為我正在制作跟蹤的期間。 我目前已經得到它,因此每個跟蹤都有 customdata 等於一個包含單個項目的列表,即句點。

所以,我們到了按鈕,這是我與按鈕相關的代碼(我從這個 SO 問題中提取了一堆:

periods = [1,2]
bullet = reports.main_bullet(df,periods) # my func that builds out all the traces
buttons = []

for period in periods + ['All']:
    buttons.append(
        dict(
            method='restyle',
            label = period,
            visible=True,
            args=[{'visible':'customdata'[0]==period}], #help me...
        )
    )

updatemenu = []
your_menu = dict()
updatemenu.append(your_menu)

updatemenu[0]['buttons'] = buttons
updatemenu[0]['direction'] = 'down'
updatemenu[0]['showactive'] = True

bullet.update_layout(updatemenus=updatemenu)

bullet.show()

在按鈕args中,當跟蹤上的句點(在customdata[0]中是我在按鈕上選擇的任何內容時,我想要visible=True 。那不起作用...

我也對“你做錯了,這是處理整個事情的更好方法”持開放態度

找到了一些有效的東西,似乎是合理的。

因此args[{'visible':...}]可以獲取一個布爾值列表,並將按該順序將它們應用於跟蹤。 所以,我只需要每個按鈕都有它需要的True/False列表。

indices_for_period = {}
for period in periods + ['All']:
    indices_for_period[period] = []

for i,data in enumerate(bullet.data):
    indices_for_period[data.customdata[0]].append(i)

這將創建一個字典,其中每個周期都有一個鍵,每個鍵的值是它的索引。

然后我像這樣更新我的可見 arg(循環):

for period in periods + ['All']:
    buttons.append(
        dict(
            method='restyle',
            label = period,
            visible=True,
            args=[
                {'visible':[x in indices_for_period[period] for x in range(i+1)]},
                ],
        )
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM