在第二台显示器上更新/刷新matplotlib图

时间:2015-01-09 13:42:31

标签: python matplotlib scientific-computing spyder

目前我正在与Spyder合作并使用matplotlib进行绘图。我有两个显示器,一个用于开发,另一个用于(数据)浏览和其他东西。由于我正在进行一些计算并且我的代码经常更改,我经常(重新)执行代码并查看图表以检查结果是否有效。

有没有办法将我的matplotlib图放在第二台显示器上并从主显示器上刷新它们?

我已经搜索过一个解决方案但找不到任何东西。这对我真的很有帮助!

以下是一些其他信息:

操作系统:Ubuntu 14.04(64位) Spyder版本:2.3.2 Matplotlib版本:1.3.1.-1.4.2。

2 个答案:

答案 0 :(得分:2)

这与matplotlib有关,而不是Spyder。明确地放置一个图形的位置似乎是那些真正只是解决方法的事情之一...查看问题here的答案。这是一个古老的问题,但我不确定自那时以来是否有变化(任何matplotlib开发者,请随时纠正我!)。

第二台显示器不应该有任何区别,听起来问题就是这个数字正在被一个新的替换。

幸运的是,您可以通过专门使用对象界面更新已移动到您想要的图形,并更新Axes对象而无需创建新图形。一个例子如下:

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()

答案 1 :(得分:2)

我知道这是一个老问题,但我遇到了类似的问题并发现了这个问题。我设法使用QT4Agg后端将我的绘图移动到第二个显示器。

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]来自@divenex的答案:How do you set the absolute position of figure windows with matplotlib?