我有一个矩阵,我用它画出下面的图.

enter image description here

正如人们所看到的那样,绘图的总行和列部分主导着 colored颜色 比例,这反过来又削弱了其他值的视觉效果.我的目标是删除最后一行和最后一列的色阶,但我不确定该怎么做.

这是我当前的绘图代码

def annotate_matrix_values(df: pd.DataFrame, fig: go.Figure) -> go.Figure:
    df_matrix = df.to_records(index=False)
    for i, r in enumerate(df_matrix):
        for k, c in enumerate(r):
            fig.add_annotation(
                x=k,
                y=i,
                text=f"<b>{str(int(c))}</b>" if not math.isnan(c) else "",
                showarrow=False,
            )
    fig.update_xaxes(side="top")
    fig.update_coloraxes(showscale=False)
    return fig
  

df_final = pd.crosstab(pt[y_axis_name], pt[x_axis_name], margins=True)

fig = px.imshow(
            df_final,
            labels=dict(x=x_title, y=y_title, color="Value"),
            x=x_label,
            y=y_label,
            color_continuous_scale="Purples",
            aspect="auto",
        )
fig = annotate_matrix_values(df_final, fig)

当然,我可以在这里设置边距为false,得到下面的图.但是我不能显示总数.

df_final = pd.crosstab(pt[y_axis_name], pt[x_axis_name], margins=False)

enter image description here

所以我的目标是显示总数,但不显示任何色阶.谢谢你的帮助

推荐答案

理解到这个问题的目的是从色标中排除总数,我try 了各种方法,包括子图和叠加空白图和热图,但没有得到好的结果.最简单的方法是在快速热图中添加一个四边形和注释.数据以x,y的形式创建,带有分类变量.热图是用不包括总计的数据创建的,注释是用总计作为字符串创建的.

import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import pandas as pd

data = np.random.randint(0,10,60).reshape(12,5)

cols = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
rows = ['Jan.','Feb.','Mar.','Apr.','May','June','July','Aug.','Sept.','Oct.','Nov.','Dec.']

df = pd.DataFrame(data, columns=cols, index=rows)
df['month_sum'] = df.sum(axis=1)
df.loc['week_sum'] = df.sum(axis=0)

fig = px.imshow(
    df.iloc[:-1,:-1].values,
    labels=dict(x="Day of Week", y="Year of Month", color="Value"),
    x=cols,
    y=rows,
    color_continuous_scale="Purples",
    aspect="auto",
    text_auto='auto'
        )

for i,r in enumerate(df['month_sum']):
    fig.add_shape(type='rect',
                  x0=4.5, y0=-0.5+i, x1=5.5, y1=0.5+i,
                  line=dict(
                      color='rgb(188,189,220)',
                      width=1,
                  ),
                  fillcolor='white',
                 )
    fig.add_annotation(
        x=5,
        y=i,
        text=str(r),
        showarrow=False
    )

for i,c in enumerate(df.loc['week_sum'].tolist()):
    if i == 5:
        break
    fig.add_shape(type='rect',
                  x0=-0.5+i, y0=12.5, x1=0.5+i, y1=11.5,
                  line=dict(
                      color='rgb(188,189,220)',
                      width=1,
                  ),
                  fillcolor='white',
                 )
    fig.add_annotation(
        x=i,
        y=12.0,
        text=str(c),
        showarrow=False
    )

fig.add_annotation(
    x=1.0,
    y=1.04,
    xref='paper',
    yref='paper',
    text='Month Total',
    showarrow=False,
)

fig.add_annotation(
    x=-0.16,
    y=0.02,
    xref='paper',
    yref='paper',
    text='Week Total',
    showarrow=False,
)

fig.update_xaxes(side="top")
fig.update_layout(
    autosize=False,
    width=600,
    height=600,
    coloraxis=dict(showscale=False)
                 )

fig.show()

enter image description here

Python相关问答推荐

Pandas使用过滤器映射多列

Ibis中是否有一个ANY或ANY_UTE表达,可以让我比较子查询返回的一组值中的值?

Polars -转换为PL后无法计算熵.列表

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

如何计算列表列行之间的公共元素

Pandas 在最近的日期合并,考虑到破产

将整组数组拆分为最小值与最大值之和的子数组

Pandas 滚动最接近的价值

如何将双框框列中的成对变成两个新列

如何使用html从excel中提取条件格式规则列表?

输出中带有南的亚麻神经网络

将两只Pandas rame乘以指数

更改键盘按钮进入'

无法使用requests或Selenium抓取一个href链接

对象的`__call__`方法的setattr在Python中不起作用'

连接一个rabrame和另一个1d rabrame不是问题,但当使用[...]'运算符会产生不同的结果

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

替换现有列名中的字符,而不创建新列