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

如何制作饼块颜色由数据集变量值决定的散点饼图?

How to Create Scatter Pie Charts with Slice Colors Tied to Absolute Variable Values

Got it, let's build exactly the scatter pie chart you described! The core idea is to plot small, evenly-split pie charts at each data point, where each slice's color corresponds directly to the absolute value of your A, B, or C variable (1-3). I'll use Python's matplotlib since it's flexible for this kind of custom visualization.

Step 1: Prepare Your Data

First, let's set up a sample dataset matching your structure (you can swap this out for your actual data):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch

# Generate sample data (replace with your real dataset)
np.random.seed(42)
data = pd.DataFrame({
    'x': np.random.rand(10) * 10,  # X coordinates for scatter points
    'y': np.random.rand(10) * 10,  # Y coordinates for scatter points
    'A': np.random.randint(1, 4, 10),  # Values 1-3
    'B': np.random.randint(1, 4, 10),
    'C': np.random.randint(1, 4, 10),
    'Apiece': [1] * 10,  # Equal-sized slices (all 1s)
    'Bpiece': [1] * 10,
    'Cpiece': [1] * 10
})

Step 2: Map Variable Values to Colors

Define a clear color mapping for your 1-3 values. You can adjust these colors to fit your needs:

# Color mapping: absolute value → hex color
value_color_map = {
    1: '#1f77b4',  # Blue
    2: '#ff7f0e',  # Orange
    3: '#2ca02c'   # Green
}

# Create a column with the color list for each pie's slices
data['slice_colors'] = data.apply(
    lambda row: [value_color_map[row['A']], value_color_map[row['B']], value_color_map[row['C']]],
    axis=1
)

Step 3: Plot the Scatter Pie Chart

Loop through each data point and draw a small pie chart at its (x,y) position. We'll ensure each slice is 1/3 of the pie using your Apiece/Bpiece/Cpiece variables:

fig, ax = plt.subplots(figsize=(10, 8))

# Draw a small pie for every data point
for idx, row in data.iterrows():
    # Plot the pie with equal slices, custom colors, and subtle borders
    ax.pie(
        [row['Apiece'], row['Bpiece'], row['Cpiece']],
        center=(row['x'], row['y']),
        radius=0.3,  # Adjust this to make pies larger/smaller
        colors=row['slice_colors'],
        wedgeprops={'linewidth': 0.5, 'edgecolor': 'white'}  # White borders to separate slices
    )

# Configure plot labels and limits
ax.set_xlim(-1, 11)
ax.set_ylim(-1, 11)
ax.set_xlabel('X Position')
ax.set_ylabel('Y Position')

# Add a legend to explain color-value mapping
legend_elements = [
    Patch(facecolor=value_color_map[val], label=f'Value {val}')
    for val in sorted(value_color_map.keys())
]
ax.legend(
    handles=legend_elements,
    bbox_to_anchor=(1.05, 1),
    loc='upper left',
    title='A/B/C Variable Value'
)

plt.title('Scatter Pie Chart: Slice Color = Absolute Variable Value')
plt.tight_layout()
plt.show()

Key Details to Note

  • Equal Slice Sizes: Using your Apiece/Bpiece/Cpiece variables (all set to 1) guarantees each slice takes exactly 1/3 of the pie.
  • Color Accuracy: Each slice's color directly maps to the absolute value of A, B, or C—no extra dimensions or transformations involved.
  • Customization: Adjust the radius parameter to make pies larger/smaller, tweak the color hex codes, or add more styling to fit your visualization needs.

内容的提问来源于stack exchange,提问作者L. Wal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:20:40