mardi 12 mai 2020

How to turn on/off all ax.text plotted through a checkbox?

it's my first question here!. I'm new at Python and I am focusing my learning programming simplest tasks I had already scripted in MatLab some time ago so I can compare and be sure my python scripting is working. I've not been able to do one simple thing: I can do that plotted points turn off/on in plot through a checkbox, but I've not been able to do the same thing with text plotted as labels related to those points. I can see the problem is that ax.text is called/assigned to line2 on a For loop, and then it's only taking the last assigned ax.text. This has an easy solution in Matlab that it should be to store every ax.text on a cell and then call it like a cell to turn it off/on. I've been searching the same thing on python and no luck till now. Any suggestions or readings will be much appreciated ... thanks !

x = np.random.randint(1,10,10)
y = np.random.randint(1,10,10)
z = np.random.randint(1,10,10)

tag = np.ones(len(x)).astype(str)

fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(1,1,1, projection='3d')

line1 = ax.scatter(x,y,z,'o',visible = True)

for sid, x, y, z in zip(tag, x, y, z):

    line2 = ax.text(x+0.01*x, y, z, sid, None, visible = True,)

lines =  [line1, line2]
rax = plt.axes([0.8, 0.05, 0.1, 0.15])
labels = ['DOTS','TEXT']
visibility = [True,True]
check = CheckButtons(rax, labels, visibility)

def func(label):
    index = labels.index(label)
    lines[index].set_visible(not lines[index].get_visible())
    plt.draw()

check.on_clicked(func)

plt.show()


[enter image description here][1] [Only last tag turn off][2]

![1]: https://i.stack.imgur.com/Cixpv.png ![2]: https://i.stack.imgur.com/GlfYS.png




Aucun commentaire:

Enregistrer un commentaire