Update/Refresh matplotlib plots on second monitor

2019-06-26 08:19发布

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.

2条回答
淡お忘
2楼-- · 2019-06-26 09:02

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?

查看更多
Bombasti
3楼-- · 2019-06-26 09:11

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()
查看更多
登录 后发表回答