svgutils.compose() 模块中是否存在关于使用 tile() 方法排列图形的错误?



我有 12 个 svg 格式的数字,我想将它们以 12 个面板的形式排列成 3 列和 4 行,适合 A4 印刷版本以供出版。需要的是最大化覆盖面积,使空白空间最小化,以便看到 12 张图像中的所有特征。

我知道我可以在 matplotlib 中使用大数字下的子图。但是,就我而言,这些数字中的每一个都是从同一模拟的不同功能独立生成的。我无法在每个调用中重复嵌入大型模拟,因为我需要出于不同目的访问不同位置的一些图形。换句话说,作原始模拟的次数越少,从中提取不同情节的麻烦就越少。我想尽量减少原始模拟中的任何更改,而是将我的附加代码添加到我的唯一目的,即将一些绘图收集到同一个地方。

以下是我在python代码中提出的实现此目的的方法:

import matplotlib.pyplot as plt
import svgutils.transform as sg
from svgutils.compose import *
import os
plt.figure(1, tight_layout=True)
...
plt.savefig('/some_path/first_xy.svg')
plt.figure(2, tight_layout=True)
...
plt.savefig('/some_path/first_xz.svg')
plt.figure(3, tight_layout=True)
...
plt.savefig('/some_path/first_yz.svg')
plt.figure(4, tight_layout=True)
...
plt.savefig('/some_path/second_xy.svg')
plt.figure(5, tight_layout=True)
...
plt.savefig('/some_path/second_xz.svg')
plt.figure(6, tight_layout=True)
...
plt.savefig('/some_path/second_yz.svg')
plt.figure(7, tight_layout=True)
...
plt.savefig('/some_path/third_xy.svg')
plt.figure(8, tight_layout=True)
...
plt.savefig('/some_path/third_xz.svg')
plt.figure(9, tight_layout=True)
...
plt.savefig('/some_path/third_yz.svg')
plt.figure(10, tight_layout=True)
...
plt.savefig('/some_path/fourth_xy.svg')
plt.figure(11, tight_layout=True)
...
plt.savefig('/some_path/fourth_xz.svg')
plt.figure(12, tight_layout=True)
...
plt.savefig('/some_path/fourth_yz.svg')


myfigure = Figure("21cm", "29.7cm", 
SVG("/some_path/first_xy.svg"), 
SVG("/some_path/first_xz.svg"), 
SVG("/some_path/first_yz.svg"), 
SVG("/some_path/second_xy.svg"), 
SVG("/some_path/second_xz.svg"), 
SVG("/some_path/second_yz.svg"), 
SVG("/some_path/third_xy.svg"), 
SVG("/some_path/third_xz.svg"), 
SVG("/some_path/third_yz.svg"), 
SVG("/some_path/fourth_xy.svg"), 
SVG("/some_path/fourth_xz.svg"), 
SVG("/some_path/fourth_yz.svg")
).tile(3, 4)
myfigure.save('/some_path/complete_figure.svg')
os.system('inkscape --export-png=/some_path/complete_figure.png /some_path/complete_figure.svg --export-background=white --export-area-drawing')

但是,运行此代码会产生与正在使用的函数相关的奇怪错误消息,tile()将 12 个数字分组到单个 A4 页面中,如下所示:

回溯(最近一次调用(:文件 "script.py",第 70 行,在 (.tile(3, 4(
File "/usr/local/anaconda3/lib/python3.5/site-packages/svgutils/compose.py", 第 287 行,在磁贴中 dx = (self.width/ncols(.to('px'(.value TypeError: 不支持/的操作数类型: 'Unit' 和 'int'

我在远程桌面上运行我的代码,但我可以访问compose.py的内容。但是我不知道该例程中是否存在错误。修复是什么?

对我来说似乎是一个错误。您应该在 https://github.com/btel/svg_utils/issues

违规行位于磁贴函数中:

dx = (self.width/ncols).to('px').value
dy = (self.height/nrows).to('px').value

self.widthself.heightUnit型,Python不知道如何除以int

与此同时,我用平铺功能的猴子补丁迅速修复了它:

def new_tile(self, ncols, nrows):
"""Automatically tile the panels of the figure.
This will re-arranged all elements of the figure (first in the
hierarchy) so that they will uniformly cover the figure area.
Parameters
----------
ncols, nrows : type
The number of columns and rows to arange the elements into.
Notes
-----
ncols * nrows must be larger or equal to number of
elements, otherwise some elements will go outside the figure borders.
"""
dx = self.width.to('px').value/ncols
dy = self.height.to('px').value/nrows
ix, iy = 0, 0
for el in self:
el.move(dx*ix, dy*iy)
ix += 1
if ix >= ncols:
ix = 0
iy += 1
if iy > nrows:
break
return self

通过执行以下内容申请:

svgutils.compose.Figure.tile = new_tile

编辑:从 pip 新安装的 svgutils-0.2.0 确实如此

最新更新