我正在尝试使用matplotlib制作交互式绘图,在该交互式绘图中,我将单击条形图的索引。这样,我可以使用索引访问存储在数组中的一些详细信息并进行打印。我有下面的代码示例,可用于折线图。
if isinstance(event.artist, Line2D):
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
print('onpick1 line:', np.column_stack([xdata[ind], ydata[ind]]))
但是,我无法获得条形图的任何此类索引/索引。有没有解决的办法?
这些条是单独的补丁。您可以从返回的列表中获取补丁的索引。
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
x = np.arange(10)
y = np.random.rand(len(x))
info = list("ABDCEFGHIJ")
bars = plt.bar(x,y, picker=True)
def on_pick(evt):
ind = bars.index(evt.artist)
print(ind, info[ind])
plt.gcf().canvas.mpl_connect("pick_event", on_pick)
plt.show()