Matplotlib Python Stealing Screen Focus
Asked Answered
B

2

9

my code is taking serial data from an arduino, processing it, and then plotting it. I am using matplotlib as the graphics interface. Every time it 'draws' though it forces attention to it, and a user won't be able to look at anything besides that. What is the best way to get this to stop? (The code works fine except for the stealing focus). I tried to use the matplotlib.use('Agg') method after reading that on another post, but it did not work. (Using a MAC OS X).

The Code shown below is a super simple graph of updating data, with which I have the same problem. I'm not showing my code because it is not copy-pastable without the right inputs Here is my code:

import matplotlib
from matplotlib import *
from pylab import *
# import math


x=[]
y=[]
def function(iteration):
    xValue=iteration#Assigns current x value
    yValue=(1./iteration)*34#Assigns current y value

    x.extend([xValue]) #adds the current x value to the x list
    y.extend([yValue]) #adds the current y value to the y list


    clf() #clears the plot

    plot(x,y,color='green') #tells the plot what to do 
    draw() #forces a draw

def main():

    for i in range(1,25): #run my function 25 times (24 I think actually)
        function(i)
        pause(.1)

main()
Barina answered 24/6, 2013 at 23:3 Comment(9)
Could you reduce this to the minimal needed to replicate the problem? Not many people are going to read that much code.Micelle
Yeah no problem. So I've taken my code and am showing the very basics of the plotting function that is running, as well as the function called 'main' which is the function I actually call to run. It is in the following comment. @tcaswellBarina
So I just edited the code in the question rather than reposting down here.Barina
good choice:) It is still probably too long (do you really think that having multiple sub-plots is affecting the focus grabbing?). Is this code copy-and-paste-and-runable?Micelle
I do believe so as long as whoever runs it has all the packages. (The whole code that is, not the part pasted above.) Tell me what you think I should do to get it in good format and I'll paste that.Barina
Actually. The whole code cannot run unless there is input data from a serial port in the form 'H,int,int,int,\n'. I think I might be able to write a simple code though that would be able to replicate the plotting functionality.Barina
Start simple. Does from pylab import *;figure();draw() steal focus? For me with the MacOSX backend, it does not.Pygmy
I don't have 'figure()' anywhere in my code. How do you use that? Also I have replaced my code @tcaswell with something much more simple that I have the same problem with.Barina
After much tinkering, I have fixed the problem. I was using Canopy as the front end for python, and it was opening its own base graphics. After tinkering a bit, I now run it from the command line and it works perfectly.Barina
S
1

Have you tried using the interactive mode of matplotlib?

You can switch it on using ion() (see Documentation)

If you use interactive mode you do not need to call draw() but you might need to clear your figures using clf() depending on your desired output

Sweeny answered 28/10, 2013 at 10:10 Comment(0)
P
0

I find that using the Tkagg backend works

import matplotlib

matplotlib.use('Tkagg')

credit to 457290092

Petulancy answered 6/5, 2020 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.