matplotlib Plotly:如何绘制x=小时的直方图?

ttcibm8c  于 2023-11-22  发布在  其他
关注(0)|答案(4)|浏览(103)

我有一系列的数据只有下面的行

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")

wsxa1bj1

wsxa1bj11#

当你使用pandas时,你可以通过创建一个pivot table,同时使用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()


结果:

643ylb08

643ylb082#

在使用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')

字符串

Plot:


的数据

完整代码:

# 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()

fcy6dtqo

fcy6dtqo3#

感谢所有的建议,我从你们这里挑选了几行代码,类似于下面的代码,以实现我正在寻找的东西。下面是使用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")

字符串


的数据

goucqfw6

goucqfw64#

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()

字符串

相关问题