1
votes

I currently have a bar graph in plotly with category on the x-axis and a total on the y-axis. 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/

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.

So far I have tried adding "barmode = 'group'" within the figure creation statement. 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.

1

1 Answers

1
votes

Edit: the problem here is that your Year column is numerical, so in the underlying figure there is only one trace. 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)

barmode='group' is the way this is intended to work, and it does work on my end at least:

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?

animated grouped bar chart