1

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.

NYezhov
  • 45
  • 5

1 Answers1

1

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:

enter image description here

jylls
  • 4,395
  • 2
  • 10
  • 21