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
(ie 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()
@r-beginners' answer is the best way to do this, but I wanted to share how to do it manually in case anyone else finds this question in the future (and so my toil doesn't go to waste). Here is how I was accomplishing this manually before @r-beginners' answer.
# build list of frames for animation
cols = ['stories', 'comments', 'comment_authors', 'story_authors']
frames = list(map(
lambda index: go.Frame(
data=[go.Bar(
x=cols,
y=list(map(lambda col: df.iloc[index][col], cols))
)]
), range(len(df.index))
))
# compute chart height
m = max(list(filter(
lambda m: not isinstance(m, str), list(df.max())
))) * 1.1
# compute animation steps for slider
steps = list(map(
lambda index: {
'args': [
[str(df.iloc[index]['month'])],
{
'frame': {'duration': 300, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 300}
}
],
'label': str(df.iloc[index]['month']),
'method': 'animate'
}, range(len(df.index))
))
# metadata for slider element
sliders_dict = {
"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "Month: ",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": 300, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": steps
}
# histogram figure - actually a bar chart
histogram = dcc.Graph(id='counts-histogram', figure=go.Figure(
data=frames[0].data[0],
layout=go.Layout(
yaxis=dict(range=[0, m], autorange=False),
updatemenus=[
{
"buttons": [
{
"args": [None, {"frame": {"duration": 500, "redraw": False},
"fromcurrent": True, "transition": {"duration": 300,
"easing": "quadratic-in-out"}}],
"label": "Play",
"method": "animate"
},
{
"args": [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
"label": "Pause",
"method": "animate"
}
],
"direction": "left",
"pad": {"r": 10, "t": 87},
"showactive": False,
"type": "buttons",
"x": 0.1,
"xanchor": "right",
"y": 0,
"yanchor": "top"
}
],
sliders = [sliders_dict]
),
frames=frames
))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.