Plotly:如何用 x=hour 绘制直方图?
Plotly: How to plot histogram with x=hour?
我有一系列数据,只有下面的行
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
如何绘制直方图,X 轴是每小时的时间序列,Y 轴是该小时发生的组件计数。
我在下面试过,但没有显示任何数据。
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")
当您使用 pandas 时,您可以创建一个 pivot table while using grouper 来聚合每小时的值:
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')
结果:
如果您想绘制图表:
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()
结果:
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()
我会使用 px.bar after taking care of the data structure using pd.pivot_table。您提供的数据集对您的挑战没有多大意义,因为您需要更多的唯一时间戳来显示您想要的内容,因此我在您的来源中添加了一些数据点。
一些核心步骤(完整代码在最后):
# 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()
感谢所有建议,我在这里从你们那里挑选了几行,类似于下面的代码来实现我正在寻找的东西。下面是使用 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")
我有一系列数据,只有下面的行
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
如何绘制直方图,X 轴是每小时的时间序列,Y 轴是该小时发生的组件计数。
我在下面试过,但没有显示任何数据。
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")
当您使用 pandas 时,您可以创建一个 pivot table while using grouper 来聚合每小时的值:
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')
结果:
如果您想绘制图表:
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()
结果:
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()
我会使用 px.bar after taking care of the data structure using pd.pivot_table。您提供的数据集对您的挑战没有多大意义,因为您需要更多的唯一时间戳来显示您想要的内容,因此我在您的来源中添加了一些数据点。
一些核心步骤(完整代码在最后):
# 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()
感谢所有建议,我在这里从你们那里挑选了几行,类似于下面的代码来实现我正在寻找的东西。下面是使用 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")