Reputation: 43
I have a series of data with only rows below
Time,Component
9:32,System
9:32,Class
9:32,System
9:32,System
9:32,System
9:32,Class
9:32,System
9:32,Class
9:32,System
9:32,System
9:32,Class
9:32,Class
9:32,System
9:32,System
9:32,System
9:32,Class
9:32,Class
9:32,System
9:32,Class
How do I plot a histogram with X-axis is the time series by hourly and Y-axis will be the count of the Components happen in that hour.
I tried below but it does not show any data.
import plotly.express as px
series['datetime']=pd.to_datetime(series['Time'])
df = series
fig2 = px.histogram(df, x=df.datetime, y=df.Component, histfunc='sum', title='Histogram Chart')
fig2.show(renderer="iframe_connected")
Upvotes: 2
Views: 3038
Reputation: 61254
I would use px.bar after taking care of the data structure using pd.pivot_table. Your provided dataset doesn't make much sense for you challenge as you would need a few more uniqe timestamps to show what you want, so I've added a few data points to your source.
# data munging using pandas
dfp = pd.pivot_table(df,index=pd.Grouper(key='Time', freq='H'),
columns='Component',
aggfunc=len,
fill_value=0)
# plotly express figure
fig = px.bar(dfp, x=dfp.index, y=['Class', 'System'])
fig.update_layout(barmode='group')
# imports
import plotly.express as px
import pandas as pd
# data
df = pd.DataFrame({'Time': {0: '9:32',
1: '9:32',
2: '9:32',
3: '9:32',
4: '9:32',
5: '9:32',
6: '9:32',
7: '9:32',
8: '13:32',
9: '13:32',
10: '13:32',
11: '17:22',
12: '17:22',
13: '17:22',
14: '17:32',
15: '19:32',
16: '19:32',
17: '19:32',
18: '19:32'},
'Component': {0: 'System',
1: 'Class',
2: 'System',
3: 'System',
4: 'System',
5: 'Class',
6: 'System',
7: 'Class',
8: 'System',
9: 'System',
10: 'Class',
11: 'Class',
12: 'System',
13: 'System',
14: 'System',
15: 'Class',
16: 'Class',
17: 'System',
18: 'Class'}})
# data munging us pd.pivot_table
df['Time'] = pd.to_datetime(df['Time'])
dfp = pd.pivot_table(df, index=pd.Grouper(key='Time', freq='H'), columns='Component', aggfunc=len, fill_value=0)
# plotly
fig = px.bar(dfp, x=dfp.index, y=['Class', 'System'])
fig.update_layout(barmode='group')
fig.show()
Upvotes: 2
Reputation: 43
Thanks for all the suggestions, I pick a few lines from you guys here and resemble below code to achieve what I am looking for. Below are is using Plotly.
import plotly.express as px
df=series
#df.set_index('Time', inplace=True)
Component_count = df['Component'].resample('s').count()
Time_Component_count = pd.DataFrame({'Time': Component_count.index, 'Component Count': Component_count.values})
fig1 = px.histogram(Time_Component_count, x='Time', y='Component Count', histfunc='sum', title='Histogram Chart')
fig1.show(renderer="iframe_connected")
Upvotes: -1
Reputation: 9649
As you're using pandas, you can do this by creating a pivot table while using grouper to aggregate the values per hour:
import pandas as pd
data = [['9:32', 'System'], ['9:32', 'Class'], ['9:32', 'System'], ['9:32', 'System'], ['9:32', 'System'], ['9:32', 'Class'], ['9:32', 'System'], ['9:32', 'Class'], ['10:32', 'System'], ['10:32', 'System'], ['10:32', 'Class'], ['11:22', 'Class'], ['11:22', 'System'], ['11:22', 'System'], ['11:32', 'System'], ['11:32', 'Class'], ['11:32', 'Class'], ['12:32', 'System'], ['12:32', 'Class']]
df = pd.DataFrame(data, columns=['Time','Component'])
df['Time'] = pd.to_datetime(df['Time']) # convert Time to datetime object
df.pivot_table(index=pd.Grouper(key = 'Time', freq = 'H'), columns='Component', aggfunc=len, fill_value=0).plot(kind='bar')
result:
If you wish to plot the chart in plotly:
import plotly.graph_objects as go
df2 = df.pivot_table(index=pd.Grouper(key = 'Time', freq = 'H'), columns='Component', aggfunc=len, fill_value=0).plot(kind='bar')
fig = go.Figure(data=[
go.Bar(name='Class', x=df2.index, y = df2.Class),
go.Bar(name='System', x=df2.index, y = df2.System)
])
fig.update_layout(barmode='group')
fig.show()
Upvotes: 2
Reputation: 197
import matplotlib.pyplot as plt
df.set_index('Time', inplace=True)
Component_count = df['Component'].resample('H').count()
Time_Component_count = pd.DataFrame({'Time': Component_count.index, 'Component Count': Complonent_count.values})
plt.hist(x = Time_Component_count['Time'], y = Time_Component_count['Component Count'])
plt.show()
Upvotes: -2