简体   繁体   English

如何使用 Python 中的 Plotly Express 向每个条形图添加可点击链接?

[英]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.

相关问题 Plotly:如何使用 Python 对 plotly 图形对象条形图进行颜色编码? - Plotly: How to colorcode plotly graph objects bar chart using Python? 如何删除 plotly express 图形对象中的网格线(python) - How do I remove gridlines in plotly express graph objects (python) 如何在 plotly 快递条形图中添加一条线 - How to add a line to a plotly express bar chart 如何使用 plotly express 更改条形图中每个特定条的颜色? - How to change colour for each specific bar in bar plot using plotly express? 如何使用 plotly express 和 python 将多个图形添加到单个选项卡 - How to add several graphs to a single tab using plotly express with python 如何隐藏 plotly express 条形图中的颜色条和图例? - How to hide the colorbar and legend in plotly express bar graph? 如何使用 Python 在 Plotly 中制作水平直方图? - How do I make a horizontal histogram in Plotly express using Python? 如何在python中将标签添加到绘图箱图中? - How do you add labels to a plotly boxplot in python? Plotly:如何使用 plotly express 为具有多个组的条形图设置动画? - Plotly: How to animate a bar chart with multiple groups using plotly express? 如何使用 python 中的破折号 plotly 更改图表的 colors - How do I change the colors of the graph using dash plotly in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM