简体   繁体   中英

Hyperlink points in a scatter plot - matplotlib

I see that matplotlib's artist has the ability to embed hyperlinks into graph objects as seen in the documentation and briefly mentioned here .

I would like to attempt to embed hyperlinks into the points of a scatter plot but have some general confusion about how to integrate or access the artist in my plot.

The Stackoverflow examples linked above lead me to believe that this is easier if I plot blank text elements over the scatter plot, and the text elements contain the hyperlinks.

There is very little information beyond the two resources I shared, any additional insight would be greatly appreciated.

Here is a way to automatically open a web browser and go to a specific URL when clicking on the points of a scatter plot. Parts of the code below come from here .

Here is the code:

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()

And the output looks like that:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM