Plotly graph : show amount of occurences in bar-chart

I try to plot a bar-chart from a givin dataframe.
x-axis = dates
y-axis = ammount of occurences for each month.
The result should be a barchart.
image

import datetime as dt
import pandas as pd
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
  
# initialize list of lists
data = [['a', '2022-01-05'], ['a', '2022-02-14'], ['a', '2022-02-15'],['a', '2022-05-14']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Date'])
  
# print dataframe.
df['Date']=pd.to_datetime(df['Date'])
# plot dataframe 
trace1=go.Bar(
#    
    x = df.Date.dt.month,
    y = df.Name.groupby(df.Date.dt.month).count()
)
data=[trace1]

fig=go.Figure(data=data)
pyo.plot(fig)

Blockquote

If you can have multiple years in the data, then it might make more sense to groupby month and year.

This should work:

df['Date']=pd.to_datetime(df['Date'])
df["month"] = df["Date"].dt.month
df["year"] = df["Date"].dt.year

grouped_df = df.groupby([df.month, df.year]).count().reset_index()
trace1=go.Bar(
    x = [grouped_df.month, grouped_df.year],
    y = grouped_df.Name
)

that’s doing the job …but.

Wen i add a new value to the dataframe with aonother name. And ik want the other name to be in a new trace. Ik expect that new trace to show something in march but it shows tha data in january

data = [[β€˜a’, β€˜2022-01-05’], [β€˜a’, β€˜2022-02-14’], [β€˜a’, β€˜2022-02-15’],[β€˜a’, β€˜2022-05-14’],[β€˜b’, β€˜2022-03-14’]]
dfa=df[df[β€˜Name’]==β€˜a’]
grouped_dfa=dfa.groupby([dfa.month, dfa.year]).count().reset_index()
dfb=df[df[β€˜Name’]==β€˜b’]
grouped_dfb=dfb.groupby([dfb.month, dfb.year]).count().reset_index()
trace1=go.Bar(
x = [grouped_df.month, grouped_df.year],
y = grouped_dfa.Name
)
trace2=go.Bar(
x = [grouped_df.month, grouped_df.year],
y = grouped_dfb.Name
)

You are using different dataframes for the x and y values. It should be:

trace1=go.Bar(
    x = [grouped_dfa.month, grouped_dfa.year],
    y = grouped_dfa.Name
)
trace2=go.Bar(
    x = [grouped_dfb.month, grouped_dfb.year],
    y = grouped_dfb.Name
)