Python matlplotlib add hyperlink to text

前端 未结 3 2072
南方客
南方客 2020-12-11 05:57

I\'ve created a plot in Python using matplotlib. After annotating each line, I\'d like to make the label a hyperlink (or alternatively, make the line itself a hyperlink). Th

3条回答
  •  隐瞒了意图╮
    2020-12-11 06:25

    Adding a hyperlink makes sense when e.g. using an SVG file.

    The url property works in newer matplotlib versions:

    text = plt.annotate("Link", xy=(2,5), xytext=(2.2,5.5),
                        url='http://matplotlib.org', 
                        bbox=dict(color='w', alpha=1e-6, url='http://matplotlib.org'))
    

    For example, in a Jupyter notebook, which runs in a browser anyways, one could display an SVG with hyperlinks like this:

    import matplotlib.pyplot as plt
    from IPython.display import set_matplotlib_formats
    set_matplotlib_formats("svg")
    
    fig, ax = plt.subplots()
    ax.scatter([1, 2, 3], [4, 5, 6])
    text = ax.annotate("Link", xy=(2,5), xytext=(2.2,5.5),
                        url='http://matplotlib.org', 
                        bbox=dict(color='w', alpha=1e-6, url='http://matplotlib.org'))
    

    In the figure produced this way you may click on the link and be directed to matplotlib.org.

提交回复
热议问题