I would like to replicate the below matplotlib graph with plotly, but how can I color the histogram bars based on the x values (dates)?
import numpy as np
import pandas as pd
import plotly.express as px
# fmt: off
df = pd.DataFrame({"date": pd.date_range("23-jan-2022", "1-apr-2027", freq="M")})
df["count"] = pd.value_counts(pd.cut(np.sort(np.random.normal(0, 0.1, 2000)), bins=len(df)), sort=False).values
df["color"] = np.select(
[df["date"].lt("1-jan-24"), df["date"].lt("1-jan-25"), df["date"].lt("1-oct-25")],
["blue", "yellow", "orange"],
"red",
)
# fmt: on
px.bar(
df,
x="date",
y="count",
color="color",
color_discrete_map={
"blue": "blue",
"yellow": "yellow",
"orange": "orange",
"red": "red",
},
).update_layout(xaxis={"dtick": "M1"}, showlegend=False)