kaminsknator
kaminsknator

Reputation: 1183

changes axis of scatter plot with slider matplotlib python

I have a very large data set and want the user to be able to slide along the axis to view sections of the data. I'm trying to leverage off of the slider example but I'm unsure of how to update the graph. Hoping someone can explain some of the behinds the scenes with this.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider, Button, RadioButtons

fig = plt.figure()
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.25, bottom=0.25)
min0 = 0
max0 = 10

x = np.arange(10)
y = np.arange(10) 
im1 = plt.scatter(x,y, s=3, c=u'b', edgecolor='None',alpha=.75)
#most examples here return something iterable

plt.ylim([0,10])#initial limits

axcolor = 'lightgoldenrodyellow'
axmin = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axmax  = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

smin = Slider(axmin, 'Min', 0, 10, valinit=min0)
smax = Slider(axmax, 'Max', 0, 10, valinit=max0)

def update(val):
    plt.ylim([smin.val,smax.val])
    ax.canvas.draw()#unsure on this
smin.on_changed(update)
smax.on_changed(update)

plt.show()

Upvotes: 1

Views: 3084

Answers (1)

Sergey Vturin
Sergey Vturin

Reputation: 51

The graph updates itself when you set new limits. You just don't see this because you update wrong subplot. Just select right subplot to update:

def update(val):
    plt.subplot(111)
    plt.ylim([smin.val,smax.val])

(this work for me)

or maybe even:

def update(val):    
    plt.ylim([smin.val,smax.val])

plt.subplot(111)
smin.on_changed(update)
smax.on_changed(update)

if you don`t do anything with it elsewhere

UPD: also in matplotlib examples you can find fig.canvas.draw_idle()

Upvotes: 1

Related Questions