Search code examples
pythonmatplotlibscientific-computingspyder

Update/Refresh matplotlib plots on second monitor


At the moment I am working with Spyder and doing my plotting with matplotlib. I have two monitors, one for development and another for (data) browsing and other stuff. Since I am doing some calculations and my code often changes, I often (re)execute the code and have a look at the plots to check if the results are valid.

Is there any way to place my matplotlib plots on a second monitor and refresh them from the main monitor?

I have already searched for a solution but could not find anything. It would be really helpful for me!

Here's some additional information:

OS: Ubuntu 14.04 (64 Bit) Spyder-Version: 2.3.2 Matplotlib-Version: 1.3.1.-1.4.2.


Solution

  • This has to do with matplotlib, not Spyder. Placing the location of a figure explicitly appears to be one of those things for which there's really just workarounds ... see the answers to the question here. That's an old question, but I'm not sure there's been change since then (any matplotlib devs, feel free to correct me!).

    The second monitor shouldn't make any difference, it sounds like the issue is just that the figure is being replaced with a new one.

    Fortunately you can update figures you've moved to where you want them pretty easily, by using the object interface specifically, and updating the Axes object without creating a new figure. An example is below:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # Create the figure and axes, keeping the object references
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    p, = ax.plot(np.linspace(0,1))
    
    # First display
    plt.show()
    
     # Some time to let you look at the result and move/resize the figure
    plt.pause(3)
    
    # Replace the contents of the Axes without making a new window
    ax.cla()
    p, = ax.plot(2*np.linspace(0,1)**2)
    
    # Since the figure is shown already, use draw() to update the display
    plt.draw()
    plt.pause(3)
    
    # Or you can get really fancy and simply replace the data in the plot
    p.set_data(np.linspace(-1,1), 10*np.linspace(-1,1)**3)
    ax.set_xlim(-1,1)
    ax.set_ylim(-1,1)
    
    plt.draw()