繁体   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