额外解决的问题 1:由于某种原因,matplotlib 创建的 SVG 的大小相对于 Inkscape 缩放了 0.75
额外解决的问题 2:Matplotlib 使用...英寸,因此以下代码行中的 25.4 只是将英寸转换为毫米。
有时,在根目录拥有更多控制权比打补丁、打补丁和打补丁要好。因此,对于那些像我一样为能够拥有两个具有相同绝对轴大小的图而苦恼的人来说,这是我的解决方案:
from matplotlib import pyplot as plt
inch = False # Set to True if you want to use inch (blergh...).
width = 50 # The actual size in millimeters for the X axis to have.
height = 20 # The actual size in millimeters for the Y axis to have.
figsize = [(-0.212+width)/(1+24.4*(not inch)),(-0.212+height)/(1+24.4*(not inch))] # [W, H]
# Attention to the 0.212 mm which is thickness of the axis line; the cap at the end of the axis is half of thickness and is accounted for the size of the axis in Inkscape. So, when you use the size of a line from Inkscape as the desired size of the axis in a plot from matplotlib, ax.get_linewidth() by default should be 0.8 (whatever 0.8 is.. but it seems like 0.212/25.4 * 100).
height_scale = 3 # Scale to account for the axis title, labels and ticks.
width_scale = 2 # Scale to account for the axis title, labels and ticks.
figsize = [width_scale*figsize[0]/0.75, height_scale*figsize[1]/0.75]
fig = plt.figure(figsize = (figsize[0], figsize[1]))
wpos = (50/(1+24.4*(not inch)))/(figsize[0]/0.75) # Giving 50 mm mandatory position shift for the Y axis, to accommodate the title, labels and ticks.
hpos = (40/(1+24.4*(not inch)))/(figsize[1]/0.75) # Giving 40 mm mandatory position shift for the X axis to accommodate the title, labels and ticks.
# Now comes the problem. The AXIS size is defined relatively to the FIGURE size. The following values will simply use the rescaled FIGURE sizes:
wscale = 1/width_scale # = (width_scale*figsize[0]/0.75)/width_scale = figsize[0]/0.75 which is our target size for Inkscape.
hscale = 1/height_scale
ax = fig.add_axes([wpos, hpos, wscale, hscale])
然后你可以随意绘图,复制 SVG 输出(至少在 Spyder 的 IPython 控制台中)并将其粘贴到 Inkscape 中。
唯一的挫折是整个图形大小会异常,您必须在 Inkscape 中从中删除白色背景。但这可能是我们所有人都已经在做的事情了。
这是一个最小的工作代码。您可以将其粘贴到 IPython 控制台并复制 SVG 输出,将其粘贴到 Inkscape 中并检查轴线大小。它将具有 50 毫米的宽度和 20 毫米的高度。