python - 如何使用Plotly绘制多个饼图圆圈?

标签 python plotly pie-chart

如何绘制这样的图表?

enter image description here

输入 x=np.array([])、y=np.array([])、a、b 参数,绘制饼图。例如图中:

x = [1,2,3,4] 
y = [1,2,3,4]
ab = [[1,1], [1,2], [3,7], [15,2]]

有一个类似的答案here .

但此解决方案不允许您添加悬停文本。

最佳答案

结合使用链接方法 go.scatter给出以下结果:

enter image description here

颜色和图例需要自定义,但总体思路应该适合您的需求。

import numpy as np
from numpy import pi, sin, cos
import plotly.graph_objects as go


def degree2rad(degrees):
    return degrees * pi / 180


def disk_part(center, radius, start_angle, end_angle, n_points=50):
    t = np.linspace(degree2rad(start_angle), degree2rad(end_angle), n_points)
    x = center[0] + radius * cos(t)
    y = center[1] + radius * sin(t)
    return np.append(x, (center[0], x[0])), np.append(y, (center[1], y[0]))


x_points = [1, 2, 3, 4]
y_points = [1, 2, 3, 4]
ab = [[1, 1], [1, 2], [3, 7], [15, 2]]
radius = 0.4
colors = ["yellow", "red"]

fig = go.Figure()

for x, y, (a, b) in zip(x_points, y_points, ab):
    ratio = a / (a + b)
    for start_angle, end_angle, color in zip(
        (0, 360 * ratio), (360 * ratio, 360), colors
    ):
        x_disk, y_disk = disk_part([x, y], radius, start_angle, end_angle)
        fig.add_trace(
            go.Scatter(
                x=x_disk,
                y=y_disk,
                fill="toself",
                fillcolor=color,
                line={"color": color},
                name=f"{(end_angle-start_angle)/360 * 100:.1f}%",
            )
        )

fig.update_yaxes(scaleanchor="x", scaleratio=1)
fig.show()

关于python - 如何使用Plotly绘制多个饼图圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75760477/

相关文章:

Python: turtle 大小(以像素为单位)

python - 如何使用重定义一个方法,在方法中仍然使用原来未修改的继承方法?

python - 使用 wxPython 更改工具栏中的标签

r - 在多行对齐上绘制轴标签

python - 获取生成器的子集

html - 如何编写一个 R 函数来创建一系列散布有 markdown 的绘图

r - 使用 R 绘图下拉菜单选择变量并继续使用颜色变量作为跟踪

python - 无法绘制饼图

android - 饼图 : how to display text just on some pie slices using AChartEngine Android library?

iOS SDK-如何检测饼图切片上的触摸?