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 8955555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:35:16+00:00 2026-06-15T14:35:16+00:00

I have multiple lines plots that are plotted on the same axes in Matplotlib.

  • 0

I have multiple lines plots that are plotted on the same axes in Matplotlib. I’m trying to use the Slider widget to adjust the lines, but for some reason only the first line plot is showing, and nothing is updated when I move the slider:

import matplotlib.pyplot as p
from matplotlib.widgets import Slider, Button, RadioButtons

Kd=0.0 
Ks=0.0
mass=0.02 

width=900
yPosition = []
yVelocity = []
yTarget = []
yForce = []
lpos= []
lvel = []
ltarget = []
lforce = []

def runSimulation(positionGain=1.5, velocityGain=60.0):
    global Kd, Ks, mass, yPosition, yVelocity, yTarget, yForce, width

    velocity = 0.0
    acceleration = 0.0
    reference = 100.0
    target = 0.0
    position = 0.0
    force = 0.0
    T=0.0005

    yPosition = []
    yVelocity = []
    yTarget = []
    yForce = []

    for i in range(0,width*10):
        acceleration = (force - Kd*velocity - Ks*position)/mass

        # Equations of motion for constant acceleration
        position = position + (velocity*T) + (0.5*acceleration*T*T)
        velocity = velocity + acceleration*T 

        e1 = target - position # Output of 1st control system
        e2 = positionGain * e1 - velocity # Output of 2nd control system
        force = velocityGain * e2

        if i % 10 == 0: #Plot 1 point for every 10 iterations of simulation
            if i>=30:
                target = reference
            else:
                target = 0
            yPosition.append(position)
            yVelocity.append(velocity*0.1)
            yTarget.append(target)
            yForce.append(force*0.001)

def plotGraph():
    global yPosition, yVelocity, yTarget, yForce, lpos, lvel, ltarget, lforce
    x = range(0,width)
    ax = p.subplot(111)
    lpos, = ax.plot(x,yPosition,'r')
    lvel, = ax.plot(x,yVelocity,'g')
    ltarget, = ax.plot(x,yTarget,'k')
    lforce, = ax.plot(x,yForce,'b')

ax = p.subplot(111)
p.subplots_adjust(left=0.25, bottom=0.25)
runSimulation()
plotGraph()

p.axis([0, 1, -10, 10])

axcolor = 'lightgoldenrodyellow'
axpos = p.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axvel  = p.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

spos = Slider(axpos, 'Position Gain', 1.0, 20.0, valinit=1.5)
svel = Slider(axvel, 'Velocity Gain', 5.0, 500.0, valinit=60.0)

def update(val):
    global yPosition,yVelocity,yTarget,yForce
    runSimulation(round(spos.val,2),round(svel.val,2))
    lpos.set_ydata(yPosition)
    lvel.set_ydata(yVelocity)
    ltarget.set_ydata(yTarget)
    lforce.set_ydata(yForce)
    p.draw()

spos.on_changed(update)
svel.on_changed(update)
p.show()

If you remove the lines between plotGraph() and p.show() you can see the original plots.

  • 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-15T14:35:17+00:00Added an answer on June 15, 2026 at 2:35 pm

    To be honest, you made a little mess with the axis positioning and on the update function. I took the liberty to write again the plotting part and put the comment in there:

    # run your simulation like usual
    runSimulation()
    
    #create a ordered grid of axes, not one in top of the others
    axcolor = 'lightgoldenrodyellow'
    fig = p.figure()
    axdata = p.subplot2grid((7,4),(0,0),colspan=4,rowspan=4)
    axpos = p.subplot2grid((7,4),(-2,0),colspan=4, axisbg=axcolor)
    axvel = p.subplot2grid((7,4),(-1,0),colspan=4, axisbg=axcolor)
    
    # create your plots in the global space.
    # you are going to reference these lines, so you need to make them visible
    # to the update functione, instead of creating them inside a function 
    # (and thus losing them at the end of the function)
    x = range(width)
    lpos, = axdata.plot(x,yPosition,'r')
    lvel, = axdata.plot(x,yVelocity,'g')
    ltarget, = axdata.plot(x,yTarget,'k')
    lforce, = axdata.plot(x,yForce,'b')
    
    # same as usual
    spos = Slider(axpos, 'Position Gain', 1.0, 20.0, valinit=1.5)
    svel = Slider(axvel, 'Velocity Gain', 5.0, 500.0, valinit=60.0)
    
    
    def update(val):
        # you don't need to declare the variables global, as if you don't
        # assign a value to them python will recognize them as global
        # without problem
        runSimulation(round(spos.val,2),round(svel.val,2))
        lpos.set_ydata(yPosition)
        lvel.set_ydata(yVelocity)
        ltarget.set_ydata(yTarget)
        lforce.set_ydata(yForce)
        # you need to update only the canvas of the figure
        fig.canvas.draw()
    
    spos.on_changed(update)
    svel.on_changed(update)
    p.show()
    

    By the way, if you want to simulate an damped oscillation, I strongly suggest you to give a look to the integrate module of scipy, wich contain the odeint function to integrate differential equation in a better way than what are you doing (that is called Euler integration, and is really error-prone)

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

Sidebar

Related Questions

I am trying to make a matplotlib figure that will have multiple horizontal boxplots
I have multiple lines to be drawn on the same axes, and each of
I have multiple lines of text entered into a single MySQL longtext cell that
I have a long label that spans multiple lines and I can not get
In a large HTML document, I have multiple lines that look like this. The
I need to have multiple lines in a cell, so I use DataGridViewTextBoxColumn.DefaultCellStyle.WrapMode =DataGridViewTriState.True.
I have multiple lines of texts in a text file that look similar to
I have multiple lines of data all sharing the same Company id. Is there
I've been trying relentlessly to have multiple lines of text on my link buttons
I want to have multiple lines on the same plot. Multiple data points. In

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.