Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:42:24+00:00 2026-06-18T04:42:24+00:00

I have a line generalisation algorithm and want to add a scroll bar to

  • 0

I have a line generalisation algorithm and want to add a scroll bar to the plot that will increase the tolerance (i,e make the line more and more generalised). Using matplotlib how would this be possible?

So to sum up, I want to be able to click and drag a slider that will display the increase in the tolerances effect on the line.


Still really struggling with this. I only want one slider on a simple scale from 1-10.


yeah the demo helps, i’m just struggerling to get one slider to work, this is what I have so far,

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

chain1 = ChainLoader('Wiggle1.txt')
chain = chain1[0]

chain2 =  chain.generalise(tolerance)

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)

tolerance = Slider(axmin, 'Min', 1, 10, valinit=min0)
#smax = Slider(axmax, 'Max', 0, 30000, valinit=max0)

def update(val):
    tolerance = tolerance.val
    #pp.show()

tolerance.on_changed(update)
#smax.on_changed(update)
chain2 =  chain.generalise(tolerance)
pp.plotPolylines(chain2)
pp.show()   

My problems are how to write the def update section. Any help?

from PointPlotter import PointPlotter 
from ChainHandler import ChainLoader
pp=PointPlotter()
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider 

ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)

tolerance = 0 
f0 = 0
chain2 = ChainLoader('Wiggle1.txt')
for chain in chain2:

    chain2 =  chain.generalise(tolerance)
    pp.plotPolylines(chain2)

axcolor = 'lightgoldenrodyellow'

axtol = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)

tolerance = Slider(axtol, 'tol', 0.1, 30.0, valinit=f0)

def update(val):
    tolerance = tolerance.val 
    for chain in chain2:

        chain2 =  chain.generalise(tolerance)
        pp.plotPolylines(chain2)

        pp.plotPolylines(chain2)

tolerance.on_changed(update) 

plt.show()

So close! Its now plotting, but returns “UnboundLocalError: local variable ‘tolerance’ referenced before assignment” when the scroll bar is used. @tcaswell any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:42:25+00:00Added an answer on June 18, 2026 at 4:42 am

    You want the slider widget (doc).

    Here is the demo from the examples:

    http://matplotlib.org/examples/widgets/slider_demo.html

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider, Button, RadioButtons
    
    ax = plt.subplot(111)
    plt.subplots_adjust(left=0.25, bottom=0.25)
    t = np.arange(0.0, 1.0, 0.001)
    a0 = 5
    f0 = 3
    s = a0*np.sin(2*np.pi*f0*t)
    l, = plt.plot(t,s, lw=2, color='red')
    plt.axis([0, 1, -10, 10])
    
    axcolor = 'lightgoldenrodyellow'
    axfreq = 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)
    
    sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
    samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
    
    def update(val):
        amp = samp.val
        freq = sfreq.val
        l.set_ydata(amp*np.sin(2*np.pi*freq*t))
        plt.draw()
    sfreq.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):
        sfreq.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)
        plt.draw()
    radio.on_clicked(colorfunc)
    
    plt.show()
    

    To adapt this to your case:

    #smax.on_changed(update)
    chain2 =  chain.generalise(tol)
    pp.plotPolylines(chain2)
    
    def update(val):
        tol = tolerance.val # get the value from the slider
        chain2 =  chain.generalise(tol) # shove that value into your code
        ax.cla() # clear the axes
        pp.plotPolylines(chain2) # re-plot your new results
    
    # register the call back
    tolerance.on_changed(update)
    

    Be careful about re-using variable names (you use tolerance twice, once for a float and once for the Slider and python will happily clobber your old variables with new ones of an entirely different type).

    In update I went with the most brute-force approach, clearing the axes and then re-drawing it, in general you want to grab the artists that are returned by plotPolylines and update those with your new data. (If you need help with that step, open a new question with details about your data structure).

    The way to understand .on_changed is that when the slider notices it has been changed, it will call the function you passed in (update) with a single argument (val) which is the current value of the slider. Inside that function you can do what ever you want, and it will be executed in full every time the slider is changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have line that is defined as two points. start = (xs,ys) end =
I have line: imagejpeg($this->image, $file, $quality); How can add on this image watermark.png?
I have a line of php code that looks like this: echo <script>$('#edit_errors').html('<h3><em>Please Correct
I have this line: $(#clients-edit-wrapper).height($(window).height()-150); I would like to apply that height function to
For example I have *line[30]; I want to allocate a string 81 characters for
How to show axis line for each bar in chart? I have line only
I have this line that works OK: c.execute('select cleanseq from cleanseqs WHERE newID=%s'%name) But
I have a line of code written in C# that I need to convert
I have a line of code that is cause a EXC_BAD_ACCESS error. The line
I have this line of code that returns index of particular object in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.