2024-04-18 23:42:54 发布
网友
如果要将额外属性绑定到艺术家对象,例如打印多部电影的IMDB分级,并且要通过单击电影对应的点来查看,则可以通过向打印的点添加自定义对象来执行此操作,如下所示:
import matplotlib.pyplot as plt 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): print(event.artist.obj.name) 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()
现在,单击绘图上的某个点时,将打印相应对象的“名称”属性。
要展开@tcaswell所说的内容,请参阅此处的文档:http://matplotlib.org/users/event_handling.html
但是,您可能会发现pick事件的快速演示非常有用:
import matplotlib.pyplot as plt def on_pick(event): artist = event.artist xmouse, ymouse = event.mouseevent.xdata, event.mouseevent.ydata x, y = artist.get_xdata(), artist.get_ydata() ind = event.ind print 'Artist picked:', event.artist print '{} vertices picked'.format(len(ind)) print 'Pick between vertices {} and {}'.format(min(ind), max(ind)+1) print 'x, y of mouse: {:.2f},{:.2f}'.format(xmouse, ymouse) print 'Data point:', x[ind[0]], y[ind[0]] print fig, ax = plt.subplots() tolerance = 10 # points ax.plot(range(10), 'ro-', picker=tolerance) fig.canvas.callbacks.connect('pick_event', on_pick) plt.show()
具体如何实现这一点将取决于您使用的艺术家(换句话说,您是否使用了ax.plot与ax.scatter与ax.imshow?)。
ax.plot
ax.scatter
ax.imshow
根据所选的艺术家,选择事件将具有不同的属性。总会有event.artist和event.mouseevent。大多数具有单独元素(例如Line2Ds、Collections等)的艺术家都将具有被选为event.ind的项的索引列表。
event.artist
event.mouseevent
Line2D
Collections
event.ind
如果要绘制多边形并选择内部点,请参见:http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py
如果要将额外属性绑定到艺术家对象,例如打印多部电影的IMDB分级,并且要通过单击电影对应的点来查看,则可以通过向打印的点添加自定义对象来执行此操作,如下所示:
现在,单击绘图上的某个点时,将打印相应对象的“名称”属性。
要展开@tcaswell所说的内容,请参阅此处的文档:http://matplotlib.org/users/event_handling.html
但是,您可能会发现pick事件的快速演示非常有用:
具体如何实现这一点将取决于您使用的艺术家(换句话说,您是否使用了
ax.plot
与ax.scatter
与ax.imshow
?)。根据所选的艺术家,选择事件将具有不同的属性。总会有
event.artist
和event.mouseevent
。大多数具有单独元素(例如Line2D
s、Collections
等)的艺术家都将具有被选为event.ind
的项的索引列表。如果要绘制多边形并选择内部点,请参见:http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py
相关问题 更多 >
编程相关推荐