所以我要做的是用plotly创建一个极坐标图。但是,它需要看起来类似于饼图,每个label
都有一个圆的切片。目前,如果我将圆分成相等的切片,极坐标图工作正常。但是,当我尝试给他们一个对应于weights
的切片时,结果不太好,因为它倾向于在每个切片之间重叠或留下空间。这主要是由于θ
有人能解释一下我哪里出错了吗
Ratings
-最大值为5,最小值为1。这用于确定极坐标图中切片的长度
Weights
-最大值为100,最小值为1。这用于确定极坐标图中切片的宽度
Labels
-标识每个切片
import plotly.graph_objects as go
import plotly.express as px
ratings = [3, 2, 5, 1, 2]
weights = [65, 79, 81, 98, 58]
labels = ["Strength", "Intelligence", "Dexterity", "Wisdom", "Stealth"]
def make_barpolar(ratings, weights, labels=None, colors=None, layout_options = None, **fig_kwargs):
# infer slice angles
num_slices = len(weights)
theta = [(i) * 360 / num_slices for i in range(0, num_slices)]
width = [360 / num_slices for _ in range(num_slices)]
# optionally infer colors
if colors is None:
color_seq = px.colors.qualitative.Safe
color_indices = range(0, len(color_seq), len(color_seq) // num_slices)
colors = [color_seq[i] for i in color_indices]
if layout_options is None:
layout_options = {}
if labels is None:
labels = ["" for _ in range(num_slices)]
layout_options["showlegend"] = False
# make figure
barpolar_plots = [go.Barpolar(r=[r], theta=[t], width=[w], name=n, marker_color=[c], **fig_kwargs)
for r, t, w, n, c in zip(ratings, theta, width, labels, colors)]
fig = go.Figure(barpolar_plots)
# additional layout parameters
fig.update_layout(**layout_options)
return fig
layout_options = {"title": "My Stats",
"title_font_size": 24,
"title_x": 0.5,
"legend_x": 0.85,
"legend_y": 0.5,
"polar_radialaxis_ticks": "",
"polar_radialaxis_showticklabels": False,
"polar_radialaxis_range": [0, max(ratings)],
"polar_angularaxis_ticks": "",
"polar_angularaxis_showticklabels": False}
fig = make_barpolar(ratings, weights, labels, layout_options=layout_options, opacity = 0.7)
fig.show()
weights
计算宽度和θ时import plotly.graph_objects as go
import plotly.express as px
ratings = [3, 2, 5, 1, 2]
weights = [65, 79, 81, 98, 38]
labels = ["Strength", "Intelligence", "Dexterity", "Wisdom", "Stealth"]
def make_barpolar(ratings, weights, labels=None, colors=None, layout_options = None, **fig_kwargs):
# infer slice angles
angles = [(weight / sum(weights) * 360) for weight in weights]
theta = []
num_slices = len(ratings)
theta = []
for index, angle in enumerate(angles):
if index < len(angles)-1:
if index == 0:
theta.append(0)
theta.append(theta[index] + angle)
width = angles
# optionally infer colors
if colors is None:
color_seq = px.colors.qualitative.Safe
color_indices = range(0, len(color_seq), len(color_seq) // num_slices)
colors = [color_seq[i] for i in color_indices]
if layout_options is None:
layout_options = {}
if labels is None:
labels = ["" for _ in range(num_slices)]
layout_options["showlegend"] = False
# make figure
barpolar_plots = [go.Barpolar(r=[r], theta=[t], width=[w], name=n, marker_color=[c], **fig_kwargs)
for r, t, w, n, c in zip(ratings, theta, width, labels, colors)]
fig = go.Figure(barpolar_plots)
# additional layout parameters
fig.update_layout(**layout_options)
return fig
layout_options = {"title": "My Stats",
"title_font_size": 24,
"title_x": 0.5,
"legend_x": 0.85,
"legend_y": 0.5,
"polar_radialaxis_ticks": "",
"polar_radialaxis_showticklabels": False,
"polar_radialaxis_range": [0, max(ratings)],
"polar_angularaxis_ticks": "",
"polar_angularaxis_showticklabels": False}
fig = make_barpolar(ratings, weights, labels, layout_options=layout_options, opacity = 0.7)
fig.show()
我想你是假设
theta
设置了一个径向扇区的一条边的位置,而实际上它是该径向扇区的中心。这是您的代码,但计算了theta
来解释此差异:给
如果要将所有内容向后移动,使浅蓝色扇区直接指向右侧,则始终可以从
theta
的每个元素中减去0.5 * angle[0]
,作为一个附加的小步_uuu
附言:非常高质量的第一次海报帖子。好极了
相关问题 更多 >
编程相关推荐