【问题标题】:Matplotlib, Inkscape, Spyder, plots and SVG compatibility (true axis size)Matplotlib、Inkscape、Spyder、绘图和 SVG 兼容性(真轴大小)
【发布时间】:2022-01-06 18:21:40
【问题描述】:

在攻读博士学位期间,我多年来一直在绘制数据,并且总是不得不与不幸困扰科学界的问题作斗争:疏忽的数据操作。

我的问题是,当我使用 matplotlib 在 Y 轴上绘制两个具有不同数字长度的图形时,结果是两个具有两个不同 X 轴大小的图形。 当我直接从 Spyder IPython 控制台(复制 SVG)复制生成的 SVG 图像并粘贴到 Inkscape 中进行编辑时,匹配轴是一项痛苦的任务,需要以绝对精度正确缩放它们。我知道有插件可以在 Inkscape 等中重新缩放绘图。

【问题讨论】:

  • 期刊和网站不在乎你的轴有多大,而是你的数字有多宽。
  • 这与期刊和网站关心与否无关。它是关于矢量图形的图像处理;能够在inkscape中将两个或多个图形拼接在一起,用matplotlib制作。图形大小可以稍后在 inkscape 中轻松配置。

标签: matplotlib svg plot axis inkscape


【解决方案1】:

额外解决的问题 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 毫米的高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2017-02-22
    • 2017-08-31
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多