简体   繁体   English

有没有办法在情节中将条形图与时间线动画分组?

[英]Is there a way to have grouped bar charts with a timeline animation in plotly?

I currently have a bar graph in plotly with category on the x-axis and a total on the y-axis.我目前有一个条形图,其中 x 轴为类别,y 轴为总数。 It is animated so that when the user presses play, it cycles through each each entry in the table with a month of 1, then 2, etc. Here is the version I have currently: https://plot.ly/~samuelbuxton/187/它是动画的,因此当用户按下播放键时,它会以一个月的 1、2 等循环遍历表中的每个条目。这是我目前拥有的版本: https : //plot.ly/~samuelbuxton/ 187/

Rather than have the 2018 tickets and 2019 tickets stacked one on the other, I want them to be grouped like this: https://plot.ly/python/bar-charts/#grouped-bar-chart .与其将 2018 年的门票和 2019 年的门票叠在一起,我希望它们像这样分组: https : //plot.ly/python/bar-charts/#grouped-bar-chart

So far I have tried adding "barmode = 'group'" within the figure creation statement.到目前为止,我已经尝试在图形创建语句中添加“barmode = 'group'”。 This has not altered the graph at all and does not produce any error messages.这根本没有改变图形,也不会产生任何错误消息。 I will highlight this change in the following code.我将在以下代码中突出显示此更改。

> #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")

Hopefully I will be able to group the bar charts together so that the 2018 bar chart for a particular month will be right next to the 2019 bar chart rather than being stacked.希望我能够将条形图组合在一起,以便特定月份的 2018 年条形图紧挨着 2019 年条形图,而不是堆叠在一起。

Edit: the problem here is that your Year column is numerical, so in the underlying figure there is only one trace.编辑:这里的问题是你的Year列是数字,所以在底层数字中只有一个痕迹。 This is why you see a continuous color scale on the side.这就是为什么您会在侧面看到连续的色标。 To force px into categorical-color mode, you will need to make your Year column into a string column: dataPD["Year"] = dataPD["Year"].astype(str)要强制px进入分类颜色模式,您需要将Year列设为字符串列: dataPD["Year"] = dataPD["Year"].astype(str)

barmode='group' is the way this is intended to work, and it does work on my end at least: 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')

This yields an animated grouped bar chart... does it do the same for you?这会产生一个动画分组条形图......它对你有同样的作用吗?

动画分组条形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM