情节:更改森伯斯特图中元素的顺序 [英] Plotly: Change order of elements in Sunburst Chart

查看:60
本文介绍了情节:更改森伯斯特图中元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 plotly express 创建森伯斯特图表.但是,我意识到儿童是按字母顺序排列的名义值.尤其是绘制月份是非常不走运的......你知道如何处理这个问题吗?也许是财产或一些解决方法?下面有一个例子,你可以自己尝试一下.提前致谢!

I am currently using plotly express to create a Sunburst Chart. However, i realized that children are ordered alphabetical for nominal values. Especially for plotting months that is pretty unlucky... Do you know how to handle that issue? Maybe a property or some workaround? Below there is an example so you can try it yourself. Thanks in advance!

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.因此相应的月份与相应的数字匹配.

Please Check this out. I have just added values to each months instead of hardcoding 2. So the corresponding month matches with corresponding number.

1 月至 1 日、2 月至 2 日、... 12 月至 12 日

January-1, February-2, ... December-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()

这篇关于情节:更改森伯斯特图中元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆