Hi, everyone.
I´m new using plotly and I would like to know if someone can help.
I have a dataframe with dates. I want to create a bar plot in which I can show a count per each day, month and year. I shoe the count per day, and then I update the plot using two buttons.
import pandas as pd
import plotly.express as px
import datetime
import plotly
# Data creation
fechas = pd.date_range(start='2023-07-01', periods=10, freq='M')
ids = list(range(1, len(fechas) + 1))
data = {'Date': fechas, 'ID': ids}
df = pd.DataFrame(data)
#Creating fields to count
df["Year"] = pd.DatetimeIndex(df["Date"]).year
df["Month"] = df["Date"].dt.strftime('%Y/%m')
df["Day"] = df["Date"].dt.strftime('%Y/%m/%d')
#counting and converting to datetime
df_d = df.groupby(['Day'], as_index=False).count()
df_d['Day']= pd.to_datetime(df_d['Day'])
df_m = df.groupby(['Month'], as_index=False).count()
df_m['Month']= pd.to_datetime(df_m['Month'])
df_a = df.groupby(['Year'], as_index=False).count()
#df_a['Year']= pd.to_datetime(df_a['Year'])
dbar = px.bar(df_d,x='Day',y='Date')
dbar.update_layout(
updatemenus=[
dict(
type="buttons",
direction="down",
#bgcolor='Dark Blue',
buttons=list(
[
dict(
label="Day",
visible=True,
method="update",
args=[{"y": [df_d["Date"]]},
{"x": [df_d["Day"]]}]
),
dict(
label="Month",
visible=True,
method="update",
args=[{"y": [df_m["Date"]]},
{"x": [df_m["Month"]]}]
),
dict(
label="Year",
visible=True,
method="update",
args=[{"y": [df_a["Date"]]},
{"x": [df_a["Year"]]}]
)
]),
), dict(
type="dropdown",
direction="right"
)
])
um = [{'showactive': True,
'active': 0,
'direction': 'right',
'x': 1,'y': 1.1,}
]
dbar.update_layout(updatemenus=um)
dbar.show()
This is my code. Maybe it´s not the best approach but it´s working.
However, when I click the buttons the ticks in the xaxis doesn’t change.
Count each day works fine
But as you can see, the count each month and each year is wrong.
Thanks for the help!