我有一系列数据,只有下面的行
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 时,您可以通过创建一个 数据透视表 来实现这一点,同时使用 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()
在使用 pd.pivot_table 处理数据结构后,我将使用 px.bar。您提供的数据集对您的挑战没有多大意义,因为您需要更多的唯一时间戳来显示您想要的内容,因此我在您的源中添加了一些数据点。
# 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")
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()