Issue
I am trying to make a matplotlib widget show data from a 1D-numpy array that is extracted from a 2D-numpy array using an index that can be varied using a slider.
The widget never updates and no error message is displayed. I have confirmed that the update-method is called by updating the index to a predifined value (which is not related to the slider).
%matplotlib notebook
# This should run on its own in a Jupyter notebook. Interestingly, in this sample-code, the plot does update when the slider is at its maximum, but not otherwise
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
sampleArray = np.random.rand(5, 100)
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
ax.set_ylim([0.2, 3])
t = np.arange(0.0, 1.0, 0.01)
s = sampleArray[0]
l, = plt.plot(t, s, lw=2)
ax.margins(x=0)
axcolor = 'lightgoldenrodyellow'
axline = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
#axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
sline = Slider(axline, 'Line Number', 0, len(sampleArray)-1, valinit=0, valstep=1)
#samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
x =100
def update(val):
line_number = sline.val
l.set_ydata(sampleArray[line_number])
fig.canvas.draw_idle()
sline.on_changed(update)
#samp.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
sline.reset()
#samp.reset()
button.on_clicked(reset)
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
l.set_color(label)
fig.canvas.draw_idle()
radio.on_clicked(colorfunc)
plt.show()
The plot should update for every step of the slider. But it only updates at the maximum and the minimum value of the slider.
Solution
I figured some of it out. (Most of the time,) the type of sline.val
seems to be numpy.float64
. After converting this using round(sline.val.item())
the code now works as expected.
Answered By - NoBullsh1t
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.