我目前正在使用 plotly express 创建森伯斯特图表。然而,我意识到 children 是按字母顺序排列的名义值。尤其是绘制月份是非常不走运的......你知道如何处理这个问题吗?也许是属性(property)或一些解决方法?下面有一个例子,你可以自己尝试一下。提前致谢!
import plotly.express as px
import pandas as pd
import calendar
months = [x for x in calendar.month_name if x]
#Create Dataframe
data = []
for m in months:
data.append(['2018', m, 2])
df = pd.DataFrame(data, columns=['Year', 'Month', 'Value'])
#Compute Sunburst
fig = px.sunburst(df, path=['Year', 'Month'], values='Value')
fig.show()
最佳答案
请检查一下。我刚刚为每个月添加了值,而不是硬编码 2。因此相应的月份与相应的数字匹配。
1 月 1 日、2 月 2 日、... 12 月 12 日
import plotly.express as px
import pandas as pd
import calendar
months = [x for x in calendar.month_name if x]
#Create Dataframe
data = []
for i,m in enumerate(months):
data.append(['2018', m,i+1])
print(data)
df = pd.DataFrame(data, columns=['Year', 'Month', 'Value'])
#Compute Sunburst
fig = px.sunburst(df, path=['Year', 'Month'], values='Value')
fig.show()
关于python - plotly :更改森伯斯特图中元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63701758/