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

如何使用Zoomcharts根据传入值设置饼图各扇区高度?

嘿,我来帮你实现这个可以自定义每个扇区高度的饼图!普通的平面饼图没法设置扇区高度,咱们可以用3D绘图的方式来模拟出带高度的立体饼图,下面我用两种常用的Python库来给你演示:

方法一:用Matplotlib绘制自定义高度的3D饼图

这种方法可以精确控制每个扇区的高度、颜色和占比,适合需要高度定制化的场景:

import matplotlib.pyplot as plt
import numpy as np

# 1. 准备数据
values = [15, 30, 25, 10, 20]  # 饼图每个扇区的占比数值
heights = [2, 5, 3, 1, 4]       # 每个扇区对应的高度值
labels = ["A", "B", "C", "D", "E"]
colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FFEAA7"]

# 2. 计算每个扇区的角度
total = sum(values)
angles = [v / total * 2 * np.pi for v in values]
start_angle = 0

# 3. 创建3D绘图对象
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')

# 4. 循环绘制每个带高度的扇区
for angle, height, label, color in zip(angles, heights, labels, colors):
    # 生成扇区的角度点
    theta = np.linspace(start_angle, start_angle + angle, 50)
    # 计算扇区的内外半径(这里外半径设为1,内半径0,就是实心饼图,可调整)
    r_inner = 0
    r_outer = 1
    # 生成x,y坐标
    x_inner = r_inner * np.cos(theta)
    y_inner = r_inner * np.sin(theta)
    x_outer = r_outer * np.cos(theta)
    y_outer = r_outer * np.sin(theta)
    
    # 绘制扇区的侧面
    ax.plot_surface(np.array([x_inner, x_outer]), 
                    np.array([y_inner, y_outer]), 
                    np.zeros((2, len(theta))),
                    color=color, alpha=0.8)
    # 绘制扇区的顶面
    ax.plot_surface(np.array([x_inner, x_outer]), 
                    np.array([y_inner, y_outer]), 
                    np.full((2, len(theta)), height),
                    color=color, alpha=0.8)
    
    # 更新起始角度
    start_angle += angle

# 5. 设置样式
ax.set_box_aspect([1, 1, 0.5])  # 调整3D图的比例
ax.set_axis_off()  # 隐藏坐标轴
plt.title("自定义扇区高度的3D饼图", fontsize=14)
# 添加图例
plt.legend([plt.Rectangle((0,0),1,1, color=color) for color in colors], labels, loc="upper right")

plt.show()

代码说明:

  • 我们通过计算每个扇区的角度占比,然后用plot_surface绘制每个扇区的侧面和顶面,从而实现不同高度的效果。
  • 如果想要环形饼图,只需要把r_inner设置成大于0的数值(比如0.5)即可。
  • 你可以根据需求调整heights数组来改变每个扇区的高度,values控制扇区的大小占比。

方法二:用Plotly快速实现(更简洁)

如果你想要更交互式的图表,Plotly的极坐标柱状图可以轻松模拟带高度的饼图:

import plotly.express as px
import pandas as pd

# 准备数据
df = pd.DataFrame({
    "category": ["A", "B", "C", "D", "E"],
    "value": [15, 30, 25, 10, 20],
    "height": [2, 5, 3, 1, 4]
})

# 绘制极坐标柱状图(模拟带高度的饼图)
fig = px.bar_polar(df, r="value", theta=np.linspace(0, 360, len(df), endpoint=False),
                   color="category", height=600, width=600,
                   hover_data=["height"],
                   color_discrete_sequence=px.colors.qualitative.Pastel)

# 设置样式,模拟饼图效果
fig.update_layout(
    title="自定义扇区高度的交互式饼图",
    polar=dict(
        radialaxis=dict(visible=False),  # 隐藏径向轴
        angularaxis=dict(visible=False)  # 隐藏角度轴
    ),
    showlegend=True
)

# 设置每个柱体的高度
fig.update_traces(width=360/len(df), marker=dict(height=df["height"]))

fig.show()

这个方法生成的是交互式图表,你可以鼠标悬停查看每个扇区的高度和数值,非常方便!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:15:07