【问题标题】:Hyperlink points in a scatter plot - matplotlib散点图中的超链接点 - matplotlib
【发布时间】:2021-12-09 08:38:50
【问题描述】:

我看到 matplotlib 的美工能够将超链接嵌入到图形对象中,如 documentation 中所见并简要提及 here

我想尝试将超链接嵌入到散点图的点中,但对于如何在我的图中集成或访问艺术家有一些普遍的困惑。

上面链接的 Stackoverflow 示例让我相信,如果我在散点图上绘制空白文本元素并且文本元素包含超链接,这会更容易。

除了我分享的两个资源之外,几乎没有什么信息,任何额外的见解将不胜感激。

【问题讨论】:

    标签: python matplotlib svg


    【解决方案1】:

    这是一种在单击散点图的点时自动打开 Web 浏览器并转到特定 URL 的方法。以下部分代码来自here

    代码如下:

    import matplotlib.pyplot as plt
    import webbrowser
    
    
    #set_matplotlib_formats("svg")
    
    class custom_objects_to_plot:
        def __init__(self, x, y, name):
            self.x = x
            self.y = y
            self.name = name
    
    a = custom_objects_to_plot(10, 20, "a")
    b = custom_objects_to_plot(30, 5, "b")
    c = custom_objects_to_plot(40, 30, "c")
    d = custom_objects_to_plot(120, 10, "d")
    
    def on_pick(event):
        webbrowser.open('https://stackoverflow.com')
        
    
    fig, ax = plt.subplots()
    for obj in [a, b, c, d]:
        artist = ax.plot(obj.x, obj.y, 'ro', picker=5)[0]
        artist.obj = obj
    
    fig.canvas.callbacks.connect('pick_event', on_pick)
    plt.show()
    

    输出看起来像这样:

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 2017-01-31
      • 2020-07-22
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多