[英]How do you add a clickable link to each bar graph using Plotly Express in Python?
So basically, I want a user to be directed to a specific website when they click a bar in a bar graph but I cant figure out how to do it.所以基本上,我希望用户在单击条形图中的条时被定向到特定网站,但我不知道该怎么做。 Couldn't find any resources for this either..
也找不到这方面的资源..
You can use HTML in annotations您可以在注释中使用 HTML
import pandas as pd
import plotly.express as px
df = pd.DataFrame({"tag": ["pandas", "plotly", "matplotlib"]}).assign(
val=1,
url=lambda d: d["tag"].apply(
lambda t: f"https://stackoverflow.com/questions/tagged/{t}?sort=Newest&filters=NoAnswers,NoAcceptedAnswer&edited=true"
),
)
fig = px.bar(df, y="val", x="tag")
for r in df.iterrows():
fig.add_annotation(
{
"x": r[1]["tag"],
"y": r[1]["val"],
"text": f"""<a href="{r[1]["url"]}" target="_blank">{r[1]["tag"]}</a>""",
}
)
fig
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.