Update/Refresh matplotlib plots on second monitor
Asked Answered
G

2

6

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.

Goldeneye answered 9/1, 2015 at 13:42 Comment(7)
(Spyder dev here) Let me see if I understand your problem correctly: you re-run your scripts, which generate matplotlib plots, and the plots are not updated in your second monitor? Another question: What is your opertating system and versions of Spyder, matplotlib and Python? Thanks :-)Fadil
Yes, if I re-run the script (having the plot of the first run maximized on my second monitor) a new plot is opened and placed on my first monitor. I am working under Ubuntu 14.04 (64 Bit) with Spyder 2.3.2 and matplotlib 1.3.1.. Thanks for your help!Goldeneye
This seems a problem with matplotlib and not with Spyder. Maybe updating to matplotlib 1.4.2 will help you. Sorry for not being of more help.Fadil
I have now updated to matplotlib 1.4.2. and still have the same problem.. Thanks anyway!Goldeneye
Try to post an issue in matplotlib issue tracker. Maybe the mpl guys can help you there :-)Fadil
Are you using the pyplot interface or the actual object methods? Pyplot tends to want to replace figures...Gothurd
What do you mean by "refresh them from the main monitor"?Melmon
G
2

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()
Gothurd answered 9/1, 2015 at 16:52 Comment(2)
Thanks for your answer! Your code works perfectly but is actually not was I was searching for. Maybe it was because my formulation was a bit unclear.. For me it is important that the plot window remains on the second monitor and keeps it position if I re-run my code with the plotting commands.Goldeneye
After updating to matplotlib 1.4.2 all new plots somehow "remember" the monitor of the last plot. So for my purpose I only had to make sure that the position is the same which I solved by maximizing the window with code from this question (#12440088) Anyway, thanks for your answer! The next time I'll try to express myself more clearly ;)Goldeneye
D
3

I know it's an old question but I came across a similar problem and found this question. I managed to move my plots to a second display using the QT4Agg backend.

import matplotlib.pyplot as plt
plt.switch_backend('QT4Agg')

# a little hack to get screen size; from here [1]
mgr = plt.get_current_fig_manager()
mgr.full_screen_toggle()
py = mgr.canvas.height()
px = mgr.canvas.width()
mgr.window.close()
# hack end

x = [i for i in range(0,10)]
plt.figure()
plt.plot(x)

figManager = plt.get_current_fig_manager()
# if px=0, plot will display on 1st screen
figManager.window.move(px, 0)
figManager.window.showMaximized()
figManager.window.setFocus()

plt.show()

[1] answer from @divenex: How do you set the absolute position of figure windows with matplotlib?

Draghound answered 4/3, 2016 at 13:31 Comment(0)
G
2

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()
Gothurd answered 9/1, 2015 at 16:52 Comment(2)
Thanks for your answer! Your code works perfectly but is actually not was I was searching for. Maybe it was because my formulation was a bit unclear.. For me it is important that the plot window remains on the second monitor and keeps it position if I re-run my code with the plotting commands.Goldeneye
After updating to matplotlib 1.4.2 all new plots somehow "remember" the monitor of the last plot. So for my purpose I only had to make sure that the position is the same which I solved by maximizing the window with code from this question (#12440088) Anyway, thanks for your answer! The next time I'll try to express myself more clearly ;)Goldeneye

© 2022 - 2024 — McMap. All rights reserved.