你怎么能画出这样一幅情节清晰的图表呢?

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

colored颜色 和图例需要定制,但总体思路应该适合您的需求.

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, 359.9), 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相关问答推荐

计算每月过go x年的平均值

单击cookie按钮,但结果不一致

合并其中一个具有重叠范围的两个框架的最佳方法是什么?

在后台运行的Python函数

在Windows上启动新Python项目的正确步骤顺序

在编写要Excel的数据透视框架时修复标题行

如何在Python中使用时区夏令时获取任何给定本地时间的纪元值?

提取两行之间的标题的常规表达

Pythind 11无法弄清楚如何访问tuple元素

如何使用Jinja语法在HTML中重定向期间传递变量?

如何在BeautifulSoup中链接Find()方法并处理无?

Odoo 14 hr. emergency.public内的二进制字段

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

Django admin Csrf令牌未设置

旋转多边形而不改变内部空间关系

寻找Regex模式返回与我当前函数类似的结果

在代码执行后关闭ChromeDriver窗口

用SymPy在Python中求解指数函数

如何在Python请求中组合多个适配器?

Python pint将1/华氏度转换为1/摄氏度°°