嵌套在同一轴上的热图python破折号

时间:2019-08-22 16:37:43

标签: python plotly

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

我尝试了以下方法:

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

致谢

1 个答案:

答案 0 :(得分:0)

如果您要查找的内容与您提供的表的结构真正匹配,则可以使用表到所需的位置。即使使用颜色来表示通过,失败和未衡量的组的大小。

情节:

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()