python - 与同一轴嵌套的热图 python dash

标签 python plotly

我想显示“通过、失败、未测量”计数矩阵。我可以在三个热图中做到这一点。一个地 block 可以结合三个地 block 吗?

我尝试过以下方法:

import plotly.offline as pyo
import plotly.graph_objs as go

p = [[1, 20, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 10, 20]]
f = [[1, 2, 3, 1, 1], [1, 1, 6, 8, 3], [3, 6, 1, 1, 2]]
nm = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
y = ['Morning', 'Afternoon', 'Evening']

data =[]
trace1  = go.Heatmap(x=x, y=y , z=p , name='pass')
trace2  = go.Heatmap(x=x, y=y , z=f,  name='fail')
trace3  = go.Heatmap(x=x, y=y , z=nm, name='not measured')

data.append(trace1)
data.append(trace2)
data.append(trace3)

fig = go.Figure(data=data)
fig.show()

我想要的是一个矩阵,其中矩阵中的每个单元格对于每个 x、y 轴都有 3 个单元格来表示通过、失败和未测量。热图是正确的方法吗?条形图是表示这些数据的好方法,但我想以非常紧凑的方式显示数据。

A sample output

问候

最佳答案

如果您要查找的内容确实与您提供的表格结构相匹配,则可以使用表格到达您想要的位置。即使用颜色来表示通过、失败和未测量组的大小。

plotly :

enter image description here

代码:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly.colors import n_colors
import numpy as np
np.random.seed(1)
import pandas as pd
import re

# color scales
colors_a = n_colors('rgb(200, 255, 200)', 'rgb(0, 200, 0)', 9, colortype='rgb')
colors_b =n_colors('rgb(255, 200, 200)', 'rgb(200, 0, 0)', 9, colortype='rgb')
colors_c = n_colors('rgb(160, 160, 160)', 'rgb(255, 255, 255)', 9, colortype='rgb')

# data
a = np.random.randint(low=0, high=9, size=3)
b = np.random.randint(low=0, high=9, size=3)
c = np.random.randint(low=0, high=9, size=3)

#subplot setup
fig = make_subplots(
    rows=1, cols=4,
    shared_yaxes=True,
    horizontal_spacing=0.005,
    specs=[[{"type": "table"}, {"type": "table"}, {"type": "table"}, {"type": "table"}]]
)

# table template
tbl = go.Table(
              header=dict(
                #values=['<b>Tbl1</b>', '<b>Tbl1</b>', '<b>Tbl1</b>'],
                line_color='white', fill_color='white',
                #align='center',font=dict(color='black', size=8)
              ),
              cells=dict(
                values=[a, b, c],
                line_color=[np.array(colors_a)[a],np.array(colors_b)[b], np.array(colors_c)[c]],
                fill_color=[np.array(colors_a)[a],np.array(colors_b)[b], np.array(colors_c)[c]],
                align='center', font=dict(color='black', size=11)
                ))

# a single extra table to show rownames for Tests
tbl2=go.Table(header=dict(
                #values=['<b>Tbl1</b>', '<b>Tbl1</b>', '<b>Tbl1</b>'],
                line_color='white', fill_color='white',
                #align='center',font=dict(color='black', size=8)
              ),
              cells=dict(
                values=[['Test1', 'Test2', 'Test3']],
                  line_color='white', fill_color='white',
                #line_color=[np.array(colors_a)[a],np.array(colors_b)[b], np.array(colors_c)[c]],
                #fill_color=[np.array(colors_a)[a],np.array(colors_b)[b], np.array(colors_c)[c]],
                align='right', font=dict(color='black', size=11)
                ))

# include tables:
fig.add_trace(
   go.Table(tbl2),
    row=1, col=1
)

fig.add_trace(
   go.Table(tbl),
    row=1, col=2
)

fig.add_trace(
   go.Table(tbl),
    row=1, col=3
)

fig.add_trace(
   go.Table(tbl),
    row=1, col=4
)

fig.update_layout(
    height=800,
    showlegend=False,
    title_text="Test results",
)


fig.update_layout(
    annotations=[
        go.layout.Annotation(text="Project A",
                                showarrow=False,
                                xref="paper",
                                yref="paper",
                                x=0.32,
                                y=1.005,
                                font=dict(family="sans serif", size=12, color="Blue")
                               ),

        go.layout.Annotation(text="Project B",
                                showarrow=False,
                                xref="paper",
                                yref="paper",
                                x=0.62,
                                y=1.005,
                                font=dict(family="sans serif", size=12, color="Blue")
                               ),

        go.layout.Annotation(text="Project C",
                                showarrow=False,
                                xref="paper",
                                yref="paper",
                                #x=0.91,
                                x=0.91,
                                y=1.005,
                               font=dict(family="sans serif", size=12, color="Blue")
                            )
    ]
)


fig.show()

关于python - 与同一轴嵌套的热图 python dash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57613749/

相关文章:

python - 使用 DBI 模块将 Perl 代码转换为带有 MySQLdb 的 Python

python - 在 numpy 中索引多个不相邻的范围

python - 在 For 循环中动态创建变量,Python

python - 如何在每次循环中将 y 的值加一

Julia - PlotlyJS 绘图仅适用于 REPL

python - plotly 基本示例显示 jupyter 实验室中没有 plotly

python - 为什么 pandas 创建 NaN 来重新采样 sql 数据?

Python Plotly 图,次要 x 轴链接到主要

python - 如何在悬停文本 Plotly 中打破长行?

r - 颜色数 = 类别数的 plotly 条形图