Python Forum
Change the scale on a Matplotlib Interactive Graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change the scale on a Matplotlib Interactive Graph
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Embed Matplotlib Graph to Tkinter? dimidgen 0 182 Mar-04-2024, 07:40 PM
Last Post: dimidgen
  Interactive plots that can be embed in web page mouse9095 1 570 Jun-12-2023, 04:51 PM
Last Post: deanhystad
  How to properly scale text in postscript conversion to pdf? philipbergwerf 3 1,084 Nov-07-2022, 01:30 PM
Last Post: philipbergwerf
  KeyError: 0 when trying to dedupe and match records at scale Catalytic 1 2,109 Apr-07-2022, 06:34 PM
Last Post: deanhystad
  How to conditionally specify markers in matplotlib graph Mark17 3 3,120 Mar-03-2022, 07:42 PM
Last Post: Mark17
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,911 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Matplotlib scale julienhofmann 0 1,782 Apr-04-2021, 08:50 AM
Last Post: julienhofmann
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,161 Mar-11-2021, 10:52 AM
Last Post: buran
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,550 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  interactive map and function hurc60248 1 1,722 Jan-08-2021, 09:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020