簡體   English   中英

Plotly:如何使用按鈕交互式設置熱圖色階?

[英]Plotly: How to interactively set heatmap colorscale using buttons?

我正在使用 Plotly 制作一些熱圖。 我想要一個交互式顏色條,以便能夠移動和/或更改比例,與 x 和 y 軸的方式相同,但顏色比例不同。 作為 MWE,請考慮此鏈接中的第一個代碼:

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                    z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]]))
fig.show()

我想在顏色欄中復制的 x,y 默認功能可以在此視頻中看到。 基本上我想部分復制SAOImageDS9的功能。 此功能可以在此視頻中看到,如果您右鍵單擊並移動鼠標,您可以更改顏色比例。

可能有更優雅的方式。 但是以下建議為所有 go.Heatmap go.Heatmap()可用的所有 colors 設置帶有 args 的按鈕,默認情況下,通過為每個色標構建一個圖形,“竊取”這些色標,然后將它們作為圖形更新提供:

# buttons, temporary figures and colorscales
for i, scale in enumerate(scales):
    colors.append(go.Figure(data=go.Heatmap(z=z,colorscale = scale)).data[0].colorscale)
    buttons.append(dict(method='restyle',
                        label=scale,
                        visible=True,
                        args=[{'colorscale':[colors[i]],
                                }, ],
                        )
                  )

帶有更新按鈕的圖:

在此處輸入圖像描述

關於解決方案的一些細節:

那么為什么colors.append(go.Figure(data=go.Heatmap(z=z,colorscale = scale)).data[0].colorscale)的麻煩? 正如我所說,可能有更優雅的方式。 但是這種方法至少可以確保色標在所有 colors 中保持相同的結構:

(Heatmap({
     'colorscale': [[0.0, 'rgb(237, 229, 207)'], [0.16666666666666666, 'rgb(224,
                    194, 162)'], [0.3333333333333333, 'rgb(211, 156, 131)'], [0.5,
                    'rgb(193, 118, 111)'], [0.6666666666666666, 'rgb(166, 84, 97)'],
                    [0.8333333333333334, 'rgb(129, 55, 83)'], [1.0, 'rgb(84, 31,
                    63)']],
     'z': [[1, 20, 30], [20, 1, 60], [30, 60, 1]]
 }),)

這是您通過按鈕和更新菜單編輯的結構。

完整代碼:

# imports 
import plotly.graph_objects as go

# data
z=[[1, 20, 30],
  [20, 1, 60],
  [30, 60, 1]]

# every colorscale available to go.Heatmap() by default
scales =     ['aggrnyl', 'agsunset', 'algae', 'amp', 'armyrose', 'balance',
             'blackbody', 'bluered', 'blues', 'blugrn', 'bluyl', 'brbg',
             'brwnyl', 'bugn', 'bupu', 'burg', 'burgyl', 'cividis', 'curl',
             'darkmint', 'deep', 'delta', 'dense', 'earth', 'edge', 'electric',
             'emrld', 'fall', 'geyser', 'gnbu', 'gray', 'greens', 'greys',
             'haline', 'hot', 'hsv', 'ice', 'icefire', 'inferno', 'jet',
             'magenta', 'magma', 'matter', 'mint', 'mrybm', 'mygbm', 'oranges',
             'orrd', 'oryel', 'peach', 'phase', 'picnic', 'pinkyl', 'piyg',
             'plasma', 'plotly3', 'portland', 'prgn', 'pubu', 'pubugn', 'puor',
             'purd', 'purp', 'purples', 'purpor', 'rainbow', 'rdbu', 'rdgy',
             'rdpu', 'rdylbu', 'rdylgn', 'redor', 'reds', 'solar', 'spectral',
             'speed', 'sunset', 'sunsetdark', 'teal', 'tealgrn', 'tealrose',
             'tempo', 'temps', 'thermal', 'tropic', 'turbid', 'twilight',
             'viridis', 'ylgn', 'ylgnbu', 'ylorbr', 'ylorrd']

# scales = scales[10:13]
colors =[]
buttons = []

# buttons, temporary figures and colorscales
for i, scale in enumerate(scales):
    colors.append(go.Figure(data=go.Heatmap(z=z,colorscale = scale)).data[0].colorscale)
    buttons.append(dict(method='restyle',
                        label=scale,
                        visible=True,
                        args=[{'colorscale':[colors[i]],
                                }, ],
                        )
                  )

# Initial figure:
fig = go.Figure(data=go.Heatmap(z=z, colorscale = scales[0]))

# some adjustments to the updatemenus
updatemenu = []
your_menu = dict()
updatemenu.append(your_menu)
updatemenu[0]['buttons'] = buttons
fig.update_layout(showlegend=False, updatemenus=updatemenu)

# f = fig.
fig.show()

暫無
暫無

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

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