'How to create monthly histogram from yearly data?

I have created a random dataframe with hourly data for the whole year. So there are 8760 values total. This is how the data frame look like:

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

I then group the LMP column by monthly format using the index column Date.

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

which will look like:

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

I want to create the histogram using plotly library and the x-axis should be catograized based on months.

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

output is: enter image description here

The expected output should look like this: enter image description here



Solution 1:[1]

I tried to make it as similar as possible to the desired output by using go.bar but first you change the dataframe a little:

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

enter image description here

Then add this code:

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

The output will be: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1