matplotlib是否提供了在屏幕上均匀分布多个数字的功能?或者有谁知道有什么工具箱能够实现这个功能?我已经厌倦了用手来做这件事。
import matplotlib.pyplot as plt
for i in range(5):
plt.figure()
plt.show()
这样就会产生5个数字,它们互相叠加在一起。为了检查图1上的内容,我必须把其他4个数字移到一边。
在MacOS上,我可以使用 Ctrl+↓ 快捷键,只为一睹所有数字的风采。另外,我也可以把图写到文件中,然后在图库中查看图像。但我想知道是否有一个matplotlib的自定义窗口管理器,可能提供一些更多的灵活性。
在Matlab中,我习惯于使用一些工具,如 蔓延图 或 autoArrangeFigures.
你可以像这样用图管理器控制绘图窗口的位置。
import matplotlib.pyplot as plt
start_x, start_y, dx, dy = (0, 0, 640, 550)
for i in range(5):
if i%3 == 0:
x = start_x
y = start_y + (dy * (i//3) )
plt.figure()
mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(x, y, dx, dy)
x += dx
plt.show()
希望这就是你要找的东西!
看来matplotlib并没有提供这样一个开箱即用的功能。此外,没有 "后台诊断 "的方式来控制图形的几何形状,正如在下面讨论的那样 此处.
因此,我写道 tile_figures()
来实现这个迷你功能,通过一些平铺逻辑和一个简单的后端抽象来扩展Anwarvic的建议。目前它只支持Qt-或Tk-backends,但当然也可以扩展到其他后端。
祝您使用愉快
tile_figures(cols=3, rows=2, screen_rect=None, tile_offsets=None)
# You may have to adjust the available screen area and a tile offset
# for nice results. This works well for my MacOS.
tile_figure(screen_rect=(0,22,1440,740), tile_offsets=(0,22))
# Run a test with 10 figures. Note that you cannot switch the backend dynamically.
# It's best to set mpl.use(<backend>) at the very beginning of your script.
# https://matplotlib.org/faq/usage_faq.html#what-is-a-backend
test(n_figs=10, backend="Qt5Agg", screen_rect=(0,22,1440,750), tile_offsets=(0,22))
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
def screen_geometry(monitor=0):
try:
from screeninfo import get_monitors
sizes = [(s.x, s.y, s.width, s.height) for s in get_monitors()]
return sizes[monitor]
except ModuleNotFoundError:
default = (0, 0, 900, 600)
print("screen_geometry: module screeninfo is no available.")
print("Returning default: %s" % default)
return default
def set_figure_geometry(fig, backend, x, y, w, h):
if backend in ("Qt5Agg", "Qt4Agg"):
fig.canvas.manager.window.setGeometry(x, y, w, h)
#fig.canvas.manager.window.statusBar().setVisible(False)
#fig.canvas.toolbar.setVisible(True)
elif backend in ("TkAgg",):
fig.canvas.manager.window.wm_geometry("%dx%d+%d+%d" % (w,h,x,y))
else:
print("This backend is not supported yet.")
print("Set the backend with matplotlib.use(<name>).")
return
def tile_figures(cols=3, rows=2, screen_rect=None, tile_offsets=None):
"""
Tile figures. If more than cols*rows figures are present, cols and
rows are adjusted. For now, a Qt- or Tk-backend is required.
import matplotlib
matplotlib.use('Qt5Agg')
matplotlib.use('TkAgg')
Arguments:
cols, rows: Number of cols, rows shown. Will be adjusted if the
number of figures is larger than cols*rows.
screen_rect: A 4-tuple specifying the geometry (x,y,w,h) of the
screen area used for tiling (in pixels). If None, the
system's screen is queried using the screeninfo module.
tile_offsets: A 2-tuple specifying the offsets in x- and y- direction.
Can be used to compensate the title bar height.
"""
assert(isinstance(cols, int) and cols>0)
assert(isinstance(rows, int) and rows>0)
assert(screen_rect is None or len(screen_rect)==4)
backend = mpl.get_backend()
if screen_rect is None:
screen_rect = screen_size()
if tile_offsets is None:
tile_offsets = (0,0)
sx, sy, sw, sh = screen_rect
sx += tile_offsets[0]
sy += tile_offsets[1]
fig_ids = plt.get_fignums()
# Adjust tiles if necessary.
tile_aspect = cols/rows
while len(fig_ids) > cols*rows:
cols += 1
rows = max(np.round(cols/tile_aspect), rows)
# Apply geometry per figure.
w = int(sw/cols)
h = int(sh/rows)
for i, num in enumerate(fig_ids):
fig = plt.figure(num)
x = (i%cols) *(w+tile_offsets[0])+sx
y = (i//cols)*(h+tile_offsets[1])+sy
set_figure_geometry(fig, backend, x, y, w, h)
def test(n_figs=10, backend="Qt5Agg", **kwargs):
mpl.use(backend)
plt.close("all")
for i in range(n_figs):
plt.figure()
tile_figures(**kwargs)
plt.show()
y方向的瓷砖偏移量最好选择为标题栏的高度。在我的MacOS上,它是22。这个值可以通过编程查询,例如使用Qt.NET来查询。
from PyQt5 import QtWidgets as qtw
enum = qtw.QStyle.PM_TitleBarHeight
style = qtw.QApplication.style()
tile_offset_y = style.pixelMetric(enum)