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

Python svgutils.compose()模块中是否存在使用tile()方法排列图形的错误?,python,matplotlib,svg,figure,svgutils,Python,Matplotlib,Svg,Figure,Svgutils,我有12个svg格式的图形,我想将它们以12个面板的形式排列成3列4行,以适合A4打印版本出版。所需要的是最大化面积覆盖率,以便最小化空白空间,以便查看12幅图像中的所有特征 我知道我可以在matplotlib中使用大数字下的子图。然而,在我的例子中,这些图形中的每一个都是独立于同一模拟的不同功能生成的。我不能在每次调用中重复嵌入大型模拟,因为我需要访问不同位置的一些图形,以用于不同的目的。换句话说,我操纵原始模拟的次数越少,就越不必从中提取不同的图。我希望尽量减少原始模拟中的任何更改,而是将我

我有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页面中,如下所示:

回溯(最近一次调用last):文件“script.py”,第70行,在 ).瓷砖(3,4)
文件“/usr/local/anaconda3/lib/python3.5/site packages/svgutils/compose.py”, 第287行,平铺 dx=(self.width/ncols).to('px')。值类型错误:不支持/:'Unit'和'int'的操作数类型


我正在远程桌面上运行代码,但我可以访问
compose.py
的内容。然而,我不知道在这个例程中是否有bug。修复方法是什么?

对我来说似乎是个bug。你应该在

有问题的行位于平铺函数中:

dx = (self.width/ncols).to('px').value
dy = (self.height/nrows).to('px').value
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
self.width
self.height
是类型
Unit
,Python不知道如何除以
int

同时,我很快用tile功能的猴子补丁修复了它:

dx = (self.width/ncols).to('px').value
dy = (self.height/nrows).to('px').value
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开始,这是正确的