Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Embedding a matplotlib graph in a Tkinter window

  1. #11
    Join Date
    Nov 2009
    Beans
    3,336

    Re: Embedding a matplotlib graph in a Tkinter window

    Looking good now .... also checked it in debian too ........... and its working fine ............

    Here is the code with that latest addition you made and some labels in the loop as they disappear otherwise ...........
    not sure if it is any use to you - but its here to look at anyway.
    u9c.py
    https://sites.google.com/site/python...edirects=0&d=1

    rdata.txt
    https://sites.google.com/site/python...edirects=0&d=1

    Its interesting following up on the error messages .......... as I say I am only learning too so your additional
    information is giving me ideas for other things and also helping along with my other project.

    So we can carry on if you want adding and discussing this ............ its interesting and also making me find new
    things that python and all the routines / modules can do to make things better for animating things.

    I am still not sure about positioning the windows ......... but as you have found out that is a good way to use the full window.

    Will go back through this thread now and see what other things are required ........
    Last edited by 23dornot23d; February 1st, 2014 at 01:04 AM. Reason: spelling mistake ...

  2. #12
    Join Date
    Mar 2005
    Location
    Isle of Bute, Scotland
    Beans
    369
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Re: Embedding a matplotlib graph in a Tkinter window

    I think it could be useful to post anything we find out about configuration of different parts.
    Being a simple guy I usually find it easier to understand this sort of stuff if there is an example but I am finding that this doesn't seem to be the case with matplotlib and pyplot.
    Here are a few other things I have found:

    Change slider background colour
    Code:
    #axcolor = 'lightblue'
    axcolor = 'lightgrey'    #  comment out all but the one you want. Not tried all possible colours - no idea what they are anyway
    #axcolor = 'lightgoldenrodyellow'
    #axcolor = 'white'
    Change slider foreground colour ( facecolor )
    Code:
    axvals = plt.axes([0.25, 0.05, 0.42, 0.025], axisbg=axcolor)
    axvals = Slider(axvals, 'Points to\n display', 10, 100, valfmt='%1.0f', facecolor='lightgreen')
    axvals1 = plt.axes([0.25, 0.10, 0.42, 0.025], axisbg=axcolor)
    axvals1 = Slider(axvals1, 'Pause', 0, 1.0, valfmt='%1.2f', facecolor='lightgreen')
    Running 14.04 on my HP Pavilion g6 4 Gig Ram 320 Gig Disc

  3. #13
    Join Date
    Nov 2009
    Beans
    3,336

    Re: Embedding a matplotlib graph in a Tkinter window

    This was where I started with Tkinter ..... got the colour selection dialog from here for choosing colours to use
    then had to learn how to strip out the parts that I needed from the output for individual colours to re-use them.
    ( if I am writing any new snippets of code that I find useful - I will post as you have done - maybe we will end up with a small library here of useful
    snippets as we progress )
    http://www.python-course.eu/tkinter_dialogs.php

    I use this examples page for matplotlib quite a lot to refer to too ... http://matplotlib.org/1.3.1/examples/index.html

    Not all examples seem to work without having to do more hunting around for other python programs - but often the
    error messages given are pretty good for seeing where to go to next. ( will also post other example pages as I come across them)

    Cheers for the code above too .... the slider and the input is a good way to quickly add data - or set defaults up - ready for modifications.

    Some of the searches too come up with diagrams that look interesting - then I go dig out the code to examine .

    ( searching the IMAGES ..... seems a quick way to see if anything may be useful ....... may seem obvious ..... but when I first started I just
    kept going from one web page to the next trawling through loads of text ........ usually if the blogger has been good enough to supply a decent
    picture of what it was they were doing - its a good indication they set the page out easy to follow too ....... )

    https://www.google.co.uk/search?q=py...client=ubuntu&
    Last edited by 23dornot23d; February 1st, 2014 at 05:23 PM. Reason: Ways of searching to speed up finding relative things

  4. #14
    Join Date
    Mar 2005
    Location
    Isle of Bute, Scotland
    Beans
    369
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Re: Embedding a matplotlib graph in a Tkinter window

    I have found this:
    http://www.verious.com/tutorial/16-c...eginners-find/
    gives some good error pointers.

    Also good for labelling
    Code:
    ax.set_ylabel('Voltage')
    ax.set_title('Micropython Scope')
    Running 14.04 on my HP Pavilion g6 4 Gig Ram 320 Gig Disc

  5. #15
    Join Date
    Nov 2009
    Beans
    3,336

    Re: Embedding a matplotlib graph in a Tkinter window

    I will post this 18 minute video ..... because its interesting and covers a lot of ground for speeding up operations.

    http://youtu.be/-p4CVtPZoPo

    Explains how to get speed increases out of python code by using Numpy ( Numerical Python )

    It also explains why it is faster to use Numpy in different situations - I am looking for speed
    in operations as the one thing that makes programs seem better is when they execute quickly
    and smoothly. Hope its of use to people interested in the python programming and the additional
    reasoning behind why its used a lot.

    Also if you want a quick look at this @ 4 : 4o explains what other speed increases can be gained
    This may be interesting for real time use of data flowing through a real time display .....
    http://youtu.be/HFxn3mSp9ww
    Last edited by 23dornot23d; February 4th, 2014 at 04:10 AM. Reason: one more - but just part of it caught my eye

  6. #16
    Join Date
    Mar 2005
    Location
    Isle of Bute, Scotland
    Beans
    369
    Distro
    Ubuntu Gnome 14.04 Trusty Tahr

    Re: Embedding a matplotlib graph in a Tkinter window

    Interesting video, shows what can be done.
    I thought I would post my code so far as someone might be interested.
    Just a reminder to all reading this thread.
    I wanted to use python on my laptop with a micropython board ( hereafter MPB ) - a kickstarter project I subscribed to.
    I wanted to use the combination to make a simple form of oscilloscope. I have not got the MPB yet as it is about to be manufactured.
    What i aim to do is write a small file to the SD card on the MPB which will tell the board how many readings to take from one of it's onboard ADC's
    The board will carry out the task and then write the results to another file on the SD card. When it's finished writing it will write another small file to the SD card to confirm it has finished. Python on my laptop will copy the data file to it's own hard disc. From there the code below will process it.
    Code:
    #!/usr/bin/env python
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button, Slider, RadioButtons
    import sys
    import time
    with open('./rdata.txt') as f:
        for i, l in enumerate(f):
            pass
    i+=1
    fig, ax = plt.subplots(figsize=(17, 10))
    plt.hold(False) #  <<<< Clears the plotting area when a new plot is initiated
    plt.subplots_adjust(bottom=0.25, top= 0.90)
    
    
    #axcolor = 'lightblue'
    axcolor = 'lightgrey'
    #axcolor = 'lightgoldenrodyellow'
    #axcolor = 'white'
    ax.set_ylabel('Voltage')
    ax.set_title('Micropython Scope')
    def endit(mock):
        sys.exit() 
    
    
    def update(val):
        howmany = int(axvals.val)
    
    
    def writefile(dummy):
        total = int(round(axvals2.val))
        print("hello" + str(total))# this is just a test to make sure function is called by my code
        # this will need to be programmed
        # to write the number of seconds to sample
        # to the SD card on the micropython (MP) board.
        # then some part of the program needs to wait for the
        #samples to be taken and written to another file on the SD card.
        # MP will read the command file and delete it then 
        # it will write the file and then write a small dummy file to signify
        # that all the data has been written.
        # The program, after it has waited say 1 second longer than the sample time
        # will read the data and do the plot.
    
    
    def static_plot(dummy):
        ydata =  []
        ax1 = plt.axes ()  
        myfile = open("rdata.txt", "r")
        for myline in myfile:
            q = myline
            ydata.append(q)  
        file.close(myfile)
        fst = (int(ax1st.val))
        lst = int(axvals.val) + fst
        line, =  plt.plot (ydata[fst:lst])
        plt.draw()
    
    
    
    
    def plotit(dummy):
        howmany = int(axvals.val)
        wait_time = (int(axvals1.val))
        top = i
        plt.ion() # set plot to animated
        xdata = []
        c = 0
        for a in range(0,top):
            xdata.append(a)
            c+=1
    
    
        ydata =  []
        ax1 = plt.axes ()  
    
    
        myfile = open("rdata.txt", "r")
        for myline in myfile:
            q = myline
            ydata.append(q)  
        file.close(myfile)
        c = 0
    # Make plot
        line, =  plt.plot (ydata[0:howmany])
        for p in range(0, top ):
            del  ydata [ 0 ]
            line.set_xdata (np.arange ( len (ydata)))
            line.set_ydata (ydata)   # update the data
            plt.draw () # update the plot
            op='Plotting ' + str((int(p/10))*10)
            ax.set_title(op)    
            time.sleep(wait_time)
    
    
    
    
    axvals = plt.axes([0.36, 0.05, 0.30, 0.025], axisbg=axcolor)
    axvals = Slider(axvals, 'Points to\n display', 10, 100, valfmt='%1.0f', facecolor='lightgreen')
    axvals1 = plt.axes([0.36, 0.10, 0.30, 0.025], axisbg=axcolor)
    axvals1 = Slider(axvals1, 'Pause', 0, 1.0, valfmt='%1.2f', facecolor='lightgreen')
    axvals2 = plt.axes([0.08, 0.15, 0.20, 0.025], axisbg=axcolor)
    axvals2 = Slider(axvals2, 'Seconds\n to Record', 0.0, 60, valfmt='%1.0f', facecolor='lightgreen')
    ax1st = plt.axes([0.36, 0.15, 0.30, 0.025], axisbg=axcolor)
    ax1st = Slider(ax1st, '1st Point\nto display', 1, 100, valfmt='%1.0f', facecolor='lightgreen')
    
    
    axplot = plt.axes([0.74, 0.05, 0.07, 0.075])
    aplot = Button(axplot, 'Dynamic\nPlot')
    aplot.on_clicked(plotit)
    
    
    axstat = plt.axes([0.82, 0.05, 0.07, 0.075])
    astat = Button(axstat, 'Static\nPlot')
    astat.on_clicked(static_plot)
    
    
    axout = plt.axes([0.90, 0.05, 0.07, 0.075])
    aout = Button(axout, 'Exit')
    aout.on_clicked(endit)
    
    
    axnum = plt.axes([0.13, 0.05, 0.07, 0.075])
    anum = Button(axnum, 'Start\nmeasuring')
    anum.on_clicked(writefile)
    
    
    
    
    plt.show()
    Forget the comment below I found the needed information see it in the code at line 12
    As it stands the dynamic plot works fine and, in itself so does the static plot except I cannot, as yet find out how to clear a previous plot other than stopping and restarting the program. Say I choose to plot points 25 to 102 it works, I then choose points 41 to 95 it works but the new display overlays the first one but in a different colour and so on. Even a dynamic plot after displaying a static one displays as if it is on another layer over the top of the previous static one.
    Anyone any idea what I need to do please?
    Last edited by linuxonbute; February 4th, 2014 at 11:17 PM. Reason: added information
    Running 14.04 on my HP Pavilion g6 4 Gig Ram 320 Gig Disc

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •