Matplotlib event_handling line picker

喜你入骨 提交于 2019-12-22 07:57:23

问题


This example makes it possible to click a legend and thereby change a plot.

I want to do something similar, but then not by clicking the legend, just by clicking the line in the plot. I tried to do it like this:

self.ax =  self.fig.add_subplot(1,2,1)
data = NP.array(2,10)  #filled with numbers
self.x = NP.arange(2)
for line in range(len(data[0,:])):
    self.ax.plot(self.x, data[:,line], picker=5)

In every loop, an extra line is plotted. One line consists of 2 points, so it draws a straight line. But now every loop, the picker is the same, so no matter which line I click, the commands I wrote to manipulate the clicked line always affect the first line. Is there a way to improve this?


回答1:


Are you wanting something like this? When a line is clicked, it will be hidden, and when the "empty" location is clicked again, it will be shown.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 10):
    ax.plot(x, i * x + x, picker=5)

def on_pick(event):
    event.artist.set_visible(not event.artist.get_visible())
    fig.canvas.draw()

fig.canvas.callbacks.connect('pick_event', on_pick)
plt.show()


来源:https://stackoverflow.com/questions/22201869/matplotlib-event-handling-line-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!