我已经想出了如何使matplotlib
图表可点击,以便我可以单击一个点并根据该点的元数据创建一个新图,按照此处的答案。
现在是时候将可点击的图放在Panel
仪表板中了。显而易见的举动是将该数字添加到panel.Row
. 虽然这显示了数字,但我没有得到交互性。
import matplotlib.pyplot as plt
import panel as pn
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):
my_fig, my_ax = plt.subplots() # New plot with unique name
my_ax.scatter([1, 2, 3, 4], [5, 6, 7, 8]) # Make the scatterplot
my_fig.show() # Show the plot
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)
my_row = pn.Row()
my_row.append(fig)
my_row.show()
我如何让交互式(可点击)图看起来像我从命令行运行时的样子?