使用两种主要技术
- 将 mark 视为数据框中的分类。使用有序索引构建极坐标图
- 将步骤 1 中分配的代码中的 radialaxis 更新回文本
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'category': ['Apple', 'Pear', 'Banana', 'Orange', 'Cherry'],
'value': [1, 1, 3, 2, 0],
'mark': ['Average', 'Average', 'Terrible', 'Bad', 'Good']})
# need order of categoricals...
cat = ['Good', 'Average', 'Bad', 'Terrible']
# output radial as an ordered number
df2 = df.assign(mark=pd.Categorical(df["mark"], ordered=True, categories=cat).codes).sort_values(by= 'category')
fig = px.line_polar(df2, theta= 'category', r= 'mark').update_traces(fill='toself')
# change axis back to text
fig.update_layout(polar={"radialaxis":{"tickmode":"array","tickvals":[i for i in range(len(cat))],"ticktext":cat}})
fig
不使用分类
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'category': ['Apple', 'Pear', 'Banana', 'Orange', 'Cherry'],
'value': [1, 1, 3, 2, 0],
'mark': ['Average', 'Average', 'Terrible', 'Bad', 'Good']})
# output radial as an ordered number
fig = px.line_polar(df.sort_values("category"), theta= 'category', r= 'value').update_traces(fill='toself')
# get mapping of value to mark
df2 = df.loc[:,["value","mark"]].drop_duplicates().sort_values("value")
# change axis to text
fig.update_layout(polar={"radialaxis":{"tickmode":"array","tickvals":df2["value"],"ticktext":df2["mark"]}})
fig