我有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页面中,如下所示:
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函数中:
self.width
和self.height
是类型Unit
,Python不知道如何除以int
。在同时,我很快就用一个monkey patch的tile函数修复了它:
^{pr2}$通过执行以下操作应用:
编辑:这是从pip新安装的svgutils-0.2.0
相关问题 更多 >
编程相关推荐