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

如何将热力图配色方案应用到饼图中

Sync Pie Chart Segment Colors with Heatmap

Key Issues in Original Code

  • Reused the cmap variable, overwriting the heatmap reference with a static palette.
  • Applied a single color to all segments in a ring instead of mapping each segment to the corresponding heatmap cell's value-based color.

Corrected Implementation

To match each pie segment's color to the corresponding heatmap cell, we need to:

  1. Preserve the heatmap's color normalization (based on data min/max).
  2. Map each dataframe cell value to the appropriate color using the same colormap and normalization.
  3. Assign these colors to the corresponding pie segments.

Here's the revised code:

import matplotlib.pyplot as plt
import numpy as np
import math
import pandas as pd
import seaborn as sns
from matplotlib.colors import Normalize

# Excel文件路径
excel_file_path = "/content/Mappe1.xlsx"

# 读取Excel文件
data_frame = pd.read_excel(excel_file_path)

# 查看DataFrame中的数据
print(data_frame)

# 创建热力图(使用单独的变量名避免覆盖cmap)
ax_heatmap = sns.heatmap(data_frame, annot=True, cmap="inferno", fmt=".2f")
plt.show()

# 获取数据的最小和最大值,用于颜色归一化
vmin = data_frame.values.min()
vmax = data_frame.values.max()

# 获取热力图使用的色图和归一化对象
cmap = plt.get_cmap("inferno")
norm = Normalize(vmin=vmin, vmax=vmax)

# 获取Excel表格中的环数(行数)
num_rings = len(data_frame)
# 列数(每个环的分段数)
num_segments = len(data_frame.columns)

# 存储每个环半径的列表
ring_radii = []
for i in range(1, num_rings + 1):
    # 计算环的半径(简化原公式)
    radius = math.sqrt(i / num_rings)
    ring_radii.append(radius)

# 初始轴尺寸
size_axis = [25] * num_segments

fig, ax = plt.subplots()

# 第一个环的起始角度
circle_circumference = 2 * math.pi
start_angle = 25 - (360 * ring_radii[0] / circle_circumference)

# 循环创建所有环的饼图
for i in range(num_rings):
    radius = ring_radii[i]
    # 获取当前行的所有值
    row_values = data_frame.iloc[i].values
    # 为每个值生成对应的颜色
    segment_colors = [cmap(norm(value)) for value in row_values]
    
    ax.pie(size_axis, radius=radius, startangle=start_angle,
           colors=segment_colors,
           wedgeprops=dict(width=0.2, edgecolor='w'))
    # 减小下一个环的轴尺寸
    size_axis = [size * 0.8 for size in size_axis]

# 为轴添加标签
ax.text(0, 1.2, 'Axis 1', ha='center', va='center', fontsize=10)
ax.text(-1.3, 0, 'Axis 2', ha='center', va='center', fontsize=10)
ax.text(0, -1.2, 'Axis 3', ha='center', va='center', fontsize=10)
ax.text(1.3, 0, 'Axis 4', ha='center', va='center', fontsize=10)

print('半径列表:', ring_radii)
plt.show()

Explanation of Changes

  • Variable Naming: Renamed ring_widths to ring_radii for clarity, since they represent radii, not widths.
  • Color Mapping: Used Normalize to scale data values to the [0,1] range expected by the colormap, matching how the heatmap calculates colors.
  • Segment Colors: For each ring (row), generate a color for each segment (column) using the corresponding cell value from the dataframe.
  • Avoid Overwriting: Kept the heatmap reference separate from the colormap to preserve normalization context.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 16:01:14