Python 有没有一种方法可以在plotly中使用时间线动画对条形图进行分组?

Python 有没有一种方法可以在plotly中使用时间线动画对条形图进行分组?,python,pyspark,plotly,Python,Pyspark,Plotly,我目前在plotly中有一个条形图,x轴上有类别,y轴上有总数。它是动画的,因此当用户按play时,它会在表中的每个条目中循环使用1个月,然后是2个月,以此类推。以下是我当前的版本: 我不希望2018年和2019年的门票一张一张地叠在一起,而是希望它们像这样分组: 到目前为止,我已经尝试在figure creation语句中添加“barmode='group'”。这根本没有改变图形,也不会产生任何错误消息。我将在下面的代码中强调此更改 > #Scatterplot x -axis time

我目前在plotly中有一个条形图,x轴上有类别,y轴上有总数。它是动画的,因此当用户按play时,它会在表中的每个条目中循环使用1个月,然后是2个月,以此类推。以下是我当前的版本:

我不希望2018年和2019年的门票一张一张地叠在一起,而是希望它们像这样分组:

到目前为止,我已经尝试在figure creation语句中添加“barmode='group'”。这根本没有改变图形,也不会产生任何错误消息。我将在下面的代码中强调此更改

> #Scatterplot x -axis time y axis = number of tickets 
> 
> import chart_studio import chart_studio.plotly as py  import
> plotly.graph_objs as go import pandas as pd import plotly.express as
> px
> 
> from math import log
> 
> #Selecting the corresponding columns from pyspark dataframe
> 
> y0 = tbl_SP.limit(2000).select('TotalTickets').collect()
> 
> y = [] for sublist in y0:
>     for item in sublist:
>         y.append(item)    x = []
> 
> x0 =  tbl_SP.limit(2000).select('Month').collect() for sublist in x0:
>     for item in sublist:
>         x.append(item) z = []
>          z0 =  tbl_SP.limit(2000).select('Year').collect() for sublist in z0:
>     for item in sublist:
>         z.append(item)
> 
> w = []
>          w0 =  tbl_SP.limit(2000).select('cmdb_ci').collect() for sublist in w0:
>     for item in sublist:
>         w.append(item)
> 
> #Converting data to pandas dataframe
> 
> data = {'TotalTickets':y , 'Month': x, 'Year': z, 'Category': w}
> dataPD = pd.DataFrame(data)
> 
> #Creating barchart figure, last parameter is my attempted solution
> 
> fig = px.bar(dataPD, x='Category', y="TotalTickets", animation_frame =
> 'Month', color = 'Year', barmode = 'group')
> 
> #Changing axis label fig.update_xaxes(title_text=' ')
> 
> 
> 
> 
> 
> #Publish to chart studio
> py.plot(fig, output_type = "div")

希望我能够将条形图组合在一起,这样2018年某个月的条形图将正好位于2019年条形图的旁边,而不是堆叠在一起。

编辑:这里的问题是,您的
列是数字列,因此在基础图中只有一个轨迹。这就是为什么您会在侧面看到连续的颜色比例。要强制
px
进入分类颜色模式,您需要将
Year
列设置为字符串列:
dataPD[“Year”]=dataPD[“Year”]。astype(str)

barmode='group'
就是这种工作方式,至少在我这方面是可行的:

import plotly.express as px
tips = px.data.tips()
px.bar(tips, x='sex', y="total_bill", animation_frame ='size', color = 'smoker', barmode = 'group')
这将生成一个动画的分组条形图。。。这对你也一样吗


编辑了我的答案,以指出潜在的问题可能是什么。我实现了这一点,它对我起到了作用,非常感谢!请大家注意:如果不是每个实例都有匹配的一对,就像我的例子一样,我有2018年,但只有2019年的前6个月,那么请根据最后一个可用的分组对其进行详细分组。在我的例子中,它匹配2019年第6个月到2018年第7、8、9个月等。