如何根据年度数据创建月度直方图?

How to create monthly histogram from yearly data?

我创建了一个包含全年每小时数据的随机数据框。所以总共有 8760 个值。这是数据框的样子:

import pandas as pd
import numpy as np
df = pd.DataFrame({
        'Date': pd.date_range('01-01-2022 00:00', '31-12-2022 23:00', freq='1H'),
        'LMP': np.random.randint(10, 100, 8760)
    }).set_index('Date')

然后我使用索引列 Date 按月格式对 LMP 列进行分组。

df['month'] = pd.DatetimeIndex(df.index).month
df2 = df.groupby('month')['LMP'].apply(list)
print(df2)

看起来像:

month
1     [41, 98, 49, 78, 73, 49, 44, 33, 60, 29, 71, 1...
2     [93, 29, 70, 74, 15, 97, 10, 46, 59, 63, 52, 8...
3     [11, 36, 24, 54, 17, 65, 74, 63, 19, 36, 64, 8...
4     [86, 69, 27, 72, 48, 11, 61, 38, 80, 59, 74, 2...
5     [84, 48, 76, 51, 74, 60, 79, 46, 78, 50, 69, 3...
6     [24, 50, 14, 31, 35, 70, 92, 47, 66, 95, 67, 6...
7     [13, 69, 82, 15, 45, 98, 41, 72, 89, 40, 80, 5...
8     [42, 43, 86, 87, 24, 63, 52, 71, 62, 78, 35, 2...
9     [27, 75, 33, 74, 57, 59, 72, 68, 14, 80, 61, 8...
10    [17, 87, 83, 70, 90, 30, 32, 60, 99, 13, 88, 5...
11    [94, 11, 97, 69, 18, 75, 44, 97, 30, 21, 90, 2...
12    [54, 56, 85, 99, 37, 41, 76, 31, 49, 54, 35, 2...
Name: LMP, dtype: object

我想使用 plotly 库创建直方图,x 轴应该根据月份分类。

import plotly.graph_objs as go
fig = go.Figure()
fig.add_trace(go.Histogram(x=df2.index,y=df2.values, name="LMP"))
fig.show()

输出为:

预期输出应如下所示:

我试图通过使用 go.bar 使其尽可能类似于所需的输出,但首先你要稍微更改数据帧:

import plotly.express as px
import pandas as pd
import numpy as np

df = pd.DataFrame({
        'Date': pd.date_range('01-01-2022 00:00', '31-12-2022 23:00', freq='1H'),
        'LMP': np.random.randint(0, 100, 8760)
    }).set_index('Date')

df['month'] = pd.DatetimeIndex(df.index).month

start,end,step = (0,100,20)
labels = []
bins = []
for i in range(start,end+1,step):
    bins.append(i)
    if i != end:
        labels.append(str(i)+" - "+str(i+20))

df['range'] = pd.cut(df["LMP"], bins=bins, labels=labels)
df_new = df.groupby(["month","range"]).count().reset_index()

然后添加此代码:

import plotly.graph_objects as go

fig = go.Figure()
for i in df_new["month"].unique():
    fig.add_trace(go.Bar(
        x = [df_new.loc[df_new["month"]==i,"month"],df_new.loc[df_new["month"]==i,"range"]],
        y = df_new.loc[df_new["month"]==i,"LMP"],
        name=str(i),
    ))


fig.update_traces(texttemplate='%{y}', textposition='outside')
fig.update_layout(barmode='group', width=1200)
fig.show()

输出将是: