You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为矩阵表的不同列设置不同颜色

实现不同列颜色的矩阵表方法

HTML表格

直接通过CSS给指定列设置背景色即可,示例代码如下:

<table border="1">
  <tr>
    <th style="background-color: #ffcccc;">列1</th>
    <th style="background-color: #ccffcc;">列2</th>
    <th style="background-color: #ccccff;">列3</th>
  </tr>
  <tr>
    <td>1-1</td>
    <td>2-1</td>
    <td>3-1</td>
  </tr>
  <tr>
    <td>1-2</td>
    <td>2-2</td>
    <td>3-2</td>
  </tr>
</table>

如果列数较多,也可以用CSS类统一管理样式:

<style>
.col1 { background-color: #ffcccc; }
.col2 { background-color: #ccffcc; }
.col3 { background-color: #ccccff; }
</style>
<table border="1">
  <tr>
    <th class="col1">列1</th>
    <th class="col2">列2</th>
    <th class="col3">列3</th>
  </tr>
  <tr>
    <td class="col1">1-1</td>
    <td class="col2">2-1</td>
    <td class="col3">3-1</td>
  </tr>
</table>

Excel/Google Sheets

  1. 点击列标题选中整列
  2. 在工具栏找到「填充颜色」按钮,选择目标颜色
  3. 重复操作给其他列设置不同颜色即可
    如果需要根据单元格内容动态变色,可使用「条件格式」功能自定义规则。

Python Pandas表格

用Styler对象可以给Pandas表格的指定列设置背景色,示例代码:

import pandas as pd

# 创建示例矩阵数据
data = {
    '列1': [1, 2, 3],
    '列2': [4, 5, 6],
    '列3': [7, 8, 9]
}
df = pd.DataFrame(data)

# 定义列颜色规则
def set_col_color(val):
    color_map = {
        '列1': '#ffcccc',
        '列2': '#ccffcc',
        '列3': '#ccccff'
    }
    return f'background-color: {color_map.get(val.name, "")}'

# 应用样式并导出为Excel
styled_df = df.style.apply(set_col_color, axis=0)
styled_df.to_excel('带颜色的矩阵表.xlsx', engine='openpyxl', index=False)
# 在Jupyter Notebook中直接打印styled_df即可查看效果

内容的提问来源于stack exchange,提问作者chunshan feng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:57:20