Python Forum

Full Version: Change the scale on a Matplotlib Interactive Graph
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello !
I'm currently stuck trying to plot some interactive graphs on Matplotlib. The issue is, I need to be able to quickly and easily modify the parameters of my function, in order to see what consequences they have, and I need to also be able to change the time period on which I plot this function. I used Sliders to do that (by following quite blindly the Matplotlib doc), but the issue is that the graph does not autoscale to follow what I'm plotting. It can be seen very clearly in the 2nd picture, where the graph is completely off the charts.
To fix this, I have introduced a button called "Redraw" which redraws the entire figure when needed, but I wondered if there was perhaps a simpler and more automatic solution to that. Advice are very much appreciated !!!

[Image: 6wV5DEg.png]
A well scaled graph
[Image: yTqt60E.png]
A Graph that's not to scale. The 'sup' Slider is supposed to change the maximum x value

I'm using Python 3.7.1 on Ubuntu Linux, and this is the useful part of my script:
h = 10**-2
sup = 20
temps = np.arange(0,sup,h)


fig, ax = plt.subplots()

plt.subplots_adjust(left=0.075, bottom=0.5,top = 0.95,right = 0.95)


pop = evolutionNew(x0,y0,a,b,c,d,proie_max,pred_max,pourcent,temps)

popProies,popPred = pop

l, = plt.plot(temps, popProies, lw=2)

ax.margins(x=0)

#Cursors and axis

axcolor = 'lightgoldenrodyellow'
axProies = plt.axes([0.1, 0.1, 0.30, 0.03], facecolor=axcolor)
axPred = plt.axes([0.1, 0.15, 0.30, 0.03], facecolor=axcolor)
axPourcent = plt.axes([0.1,0.05,0.30,0.03], facecolor = axcolor)
axSup = plt.axes([0.1,0.2,0.3,0.03], facecolor = axcolor)
axA = plt.axes([0.5,0.05,0.3,0.03], facecolor = axcolor)
axB = plt.axes([0.5,0.1,0.3,0.03], facecolor = axcolor)
axC = plt.axes([0.5,0.15,0.3,0.03], facecolor = axcolor)
axD = plt.axes([0.5,0.2,0.3,0.03], facecolor = axcolor)

sProies = Slider(axProies, 'x0', 0.1, 30.0, valinit=x0)
sPred = Slider(axPred, 'y0', 0.1, 10.0, valinit=y0)
sPourcent = Slider(axPourcent, '%', 0.,100., valinit = 0)
sSup = Slider(axSup,'Sup',4,50,valinit = sup)
sA = Slider(axA, 'a', 0,10)
sB = Slider(axB,'b',0,10)
sC = Slider(axC,'c',0,10)
sD = Slider(axD,'d',0,10)


#Update function
def update(val):
    sup = sSup.val
    temps = np.arange(0,sup,h)
    x0 = sProies.val
    y0 = sPred.val
    pourcent = sPourcent.val
    a,b,c,d = sA.val,sB.val,sC.val,sD.val
    l.set_xdata(temps)
    l.set_ydata(evolutionNew(x0,y0,a,b,c,d,proie_max,pred_max,pourcent,temps)[0])
    fig.canvas.draw_idle()



#Sliders Update
sProies.on_changed(update)
sPred.on_changed(update)
sPourcent.on_changed(update)
sSup.on_changed(update)
sA.on_changed(update)
sB.on_changed(update)
sC.on_changed(update)
sD.on_changed(update)

#Making the buttons
resetax = plt.axes([0.875, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')

redrawAx = plt.axes([0.875,0.1,0.1,0.04])
Bredraw = Button(redrawAx, 'Redraw', color = axcolor, hovercolor = '0.975')


def reset(event):
    sProies.reset()
    sPred.reset()
    sPourcent.reset()
button.on_clicked(reset)


def redraw(event):
    global l
    ax.cla()
    sup = sSup.val
    temps = np.arange(0,sup,h)
    x0 = sProies.val
    y0 = sPred.val
    pourcent = sPourcent.val
    a,b,c,d = sA.val,sB.val,sC.val,sD.val
    l, = ax.plot(temps, evolutionNew(x0,y0,a,b,c,d,proie_max,pred_max,pourcent,temps)[0], lw=2)
    ax.margins(x=0)


Bredraw.on_clicked(redraw)


plt.show()

Bredraw.on_clicked(redraw)


plt.show()