Python 图工厂热图(带按钮)
我有两个数据框,我想放在一个带注释的热图中,我希望用户能够用按钮在两个热图之间切换 问题是我无法让Python 图工厂热图(带按钮),python,python-3.x,plotly,Python,Python 3.x,Plotly,我有两个数据框,我想放在一个带注释的热图中,我希望用户能够用按钮在两个热图之间切换 问题是我无法让更新\u布局按我预期的方式工作。这是我的剧本: a = np.random.rand(5, 5) b = np.random.rand(5, 5) fig = ff.create_annotated_heatmap() a = [dict(a, annotation_text=a, )] b = [dict(b, annotation_t
更新\u布局
按我预期的方式工作。这是我的剧本:
a = np.random.rand(5, 5)
b = np.random.rand(5, 5)
fig = ff.create_annotated_heatmap()
a = [dict(a,
annotation_text=a,
)]
b = [dict(b,
annotation_text=b,
)]
fig.update_layout(
updatemenus=[
dict(
type="buttons",
buttons=list([
dict(label="choose a",
method="update",
args=[{"visible": [True, False]},
{"title": "a is best",
}]),
dict(label="choose b",
method="update",
args=[{"visible": [False, True]},
{"title": "b is better",
}]),
]),
)
])
fig.show()
这将返回:
TypeError: create_annotated_heatmap() missing 1 required positional argument: 'z'
我这里缺少什么?由于缺少
z
参数,因此出现此错误。使用此选项制作地物工厂注释的热图:
a = np.random.randint(10, size=(2, 4))
fig = ff.create_annotated_heatmap(z=a, colorscale='Viridis')
要使用按钮添加其他值,请查看下面为选项z=a
和z=b
生成此图的代码段:
绘图1:z=a
绘图2:z=b
完整代码:
import plotly.figure_factory as ff
import numpy as np
np.random.seed(123)
a = np.random.randint(10, size=(2, 4))
b = np.random.randint(10, size=(2, 4))
fig = ff.create_annotated_heatmap(z=a, colorscale='Viridis')
# Add dropdown
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=[{'z':[a]}],
label="Select a",
method="update"
),
dict(
args=[{'z':[b]}],
label="Select b",
method="update"
)
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=-0.2,
xanchor="left",
y=0.8,
yanchor="top"
),
]
)
fig.show()