你怎么能画出这样的绘图?
输入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]]
但是这个解决方案不允许你添加悬停文本。
结合使用链接方法和go.scatter 给出以下结果:
颜色和图例需要自定义,但整体思路要符合您的需求。
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()