I have the following pandas dataframe:
month stories comments comment_authors story_authors
0 2006-10 49 12 4 16
1 2007-02 564 985 192 163
2 2007-03 1784 4521 445 287
I am trying to construct a Plotly histogram where there are four categorical bins (x-axis) for each of the stories
, comments
, comment_authors
, and story_authors
columns, and the count (y-axis) is the given quantity for a specific month
(i.e. a specific row). I am then trying to animate the histogram based on month in Plotly Express using animation_frame
and animation_group
.
For example, the first histogram for month=2006-10
would look something like:
50 | ____
45 | | |
40 | | |
35 | | |
30 | | |
25 | | |
20 | | |
15 | | | ____
10 | | | ____ | |
5 | | | | | ______ | |
0 ----------------------------------------------------
stories comments comment_authors story_authors
In the histogram for the next animation frame, it would read the values from the stories
, comments
, comment_authors
, story_authors
columns for month=2007-02
.
Is this possible to construct in Plotly? Is there a better figure to use than px.Histogram
, like px.Bar
? I have tried putting the columns on the x-axis and using the month for the animation frame, but this stacks the columns into one bin on the histogram and uses the count of the entire column, not a specific row's value.
histogram = dcc.Graph(figure=px.histogram(
df, x=['stories', 'comments', 'comment_authors', 'story_authors'],
animation_frame='month', animation_group='month'
))
It is not possible to draw a histogram with the data you are presenting. The best you can do is a bar chart, and you can animate it with a time series. I have modified the sample in the official reference to fit your data.
import pandas as pd
import numpy as np
import io
data = '''
month stories comments comment_authors story_authors
0 2006-10 49 12 4 16
1 2007-02 564 985 192 163
2 2007-03 1784 4521 445 287
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
df.set_index('month', inplace=True)
dfs = df.unstack().reset_index()
dfs.columns = ['category', 'month', 'value']
import plotly.express as px
fig = px.bar(dfs, x='category', y='value', color='category', animation_frame='month', range_y=[0,dfs['value'].max()])
fig.show()