John Boe
John Boe

Reputation: 3621

Reactions of slider on_change event is very slow, how to optimize it? (python, matlab.widgets)

Can you help me to optimize this code, I think that it is very slow. When I try to set the slider, it reacts too slow. I have dual core CPU Celeron made in 2009. Also is it possible to have values of integer in the slider?

# https://matplotlib.org/stable/gallery/widgets/range_slider.html

import matplotlib.pyplot as plt
from matplotlib.widgets import RangeSlider

img = plt.imread('crayion-1024x1024.png')

fig, axs = plt.subplots(1, 2, figsize=(10, 5))
fig.subplots_adjust(bottom=0.25)

im = axs[0].imshow(img)
axs[1].hist(img.flatten(), bins='auto')
axs[1].set_title('Histogram of pixel intensities')

slider_ax = fig.add_axes([0.20, 0.1, 0.60, 0.03])
slider = RangeSlider(slider_ax, "Threshold", img.min(), img.max())

lower_limit_line = axs[1].axvline(slider.val[0], color='k')
upper_limit_line = axs[1].axvline(slider.val[1], color='k')

def update(val):
    print("vals:", val)
    im.norm.vmin = val[0]
    im.norm.vmax = val[1]
    lower_limit_line.set_xdata([val[0], val[0]])
    upper_limit_line.set_xdata([val[1], val[1]])
    fig.canvas.draw_idle()

slider.on_changed(update)
plt.show()

There is the print of values. If I drag one movement on the slider, so it prints two pairs of values so it means that the event could be at least run twice. Maybe too much?

Upvotes: 0

Views: 21

Answers (0)

Related Questions