Matplotlib event_handling行选择器 [英] Matplotlib event_handling line picker
问题描述
此示例可以点击图例,从而更改图。 / p>
我想做类似的事情,但是不要点击图例,只需点击绘图中的行。我试图这样做:
self.ax = self.fig.add_subplot(1,2,1)
data = NP.array(2,10)#incilled with numbers
self.x = NP.arange(2)
for line in range(len(data [0 ,:])))
self.ax.plot(self.x,data [:,line],picker = 5)
在每个循环中,绘制一条额外的线。一行由2点组成,因此绘制一条直线。但是现在每个循环,选择器都是一样的,所以无论我点击哪一行,我写的操作点击行的命令总是影响第一行。
有没有办法改善这个?
你想要这样的东西吗?当点击一行时,它将被隐藏,当再次单击空位置时,将显示该行。
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()
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?
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()
这篇关于Matplotlib event_handling行选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!