svgutils.compose() 模块中是否存在有关使用 tile() 方法排列图形的错误?
Is there a bug in svgutils.compose() module regarding the arrangement of figures using tile() method?
我有 12 个 svg 格式的图形,我想以 12 个面板的形式将它们排列成 3 列和 4 行,以适合 A4 打印版本以供发布。所需要的是最大化区域覆盖率,使空的 space 最小化,以便查看 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 页面中,如下所示:
Traceback (most recent call last): File "script.py", line 70, in
).tile(3, 4)
File "/usr/local/anaconda3/lib/python3.5/site-packages/svgutils/compose.py",
line 287, in tile
dx = (self.width/ncols).to('px').value TypeError: unsupported operand type(s) for /: 'Unit' and 'int'
我是 运行 我在远程桌面上的代码,但我可以访问 compose.py
的内容。但是我不知道该例程中是否存在错误。解决方法是什么?
对我来说似乎是个错误。您应该在 https://github.com/btel/svg_utils/issues
举报
违规行在 tile 函数中:
dx = (self.width/ncols).to('px').value
dy = (self.height/nrows).to('px').value
self.width
和 self.height
是类型 Unit
,Python 不知道如何除以 int
。
同时,我用 tile 函数的 monkey-patch 快速修复了它:
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 开始是这样
我有 12 个 svg 格式的图形,我想以 12 个面板的形式将它们排列成 3 列和 4 行,以适合 A4 打印版本以供发布。所需要的是最大化区域覆盖率,使空的 space 最小化,以便查看 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 页面中,如下所示:
Traceback (most recent call last): File "script.py", line 70, in ).tile(3, 4)
File "/usr/local/anaconda3/lib/python3.5/site-packages/svgutils/compose.py", line 287, in tile dx = (self.width/ncols).to('px').value TypeError: unsupported operand type(s) for /: 'Unit' and 'int'
我是 运行 我在远程桌面上的代码,但我可以访问 compose.py
的内容。但是我不知道该例程中是否存在错误。解决方法是什么?
对我来说似乎是个错误。您应该在 https://github.com/btel/svg_utils/issues
举报违规行在 tile 函数中:
dx = (self.width/ncols).to('px').value
dy = (self.height/nrows).to('px').value
self.width
和 self.height
是类型 Unit
,Python 不知道如何除以 int
。
同时,我用 tile 函数的 monkey-patch 快速修复了它:
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 开始是这样