【问题标题】:How can I automatically combine matplotlib graphs with Adobe Illustrator vector illustrations?如何自动将 matplotlib 图形与 Adob​​e Illustrator 矢量插图结合起来?
【发布时间】:2014-11-05 17:57:51
【问题描述】:

我目前正在撰写一篇科学论文,并且正在使用 matplotlib 生成大部分数据。我有一个使用生成文件设置的管道,每当我更新数据时,它都会重新生成我的所有图。我的问题是这些数字是由多个面板组成的,其中一些面板应该包含我使用 Adob​​e Illustrator 创建的矢量插图。更新原始数据时,如何自动将图表与插图结合起来?我可以将矢量插图保存为光栅格式,然后使用 matplotlib 的 imshow 函数显示它们,但我希望输出为矢量以确保最佳打印质量。

【问题讨论】:

  • 您是否尝试过将后端设置为 SVG,以便您的所有图像都是矢量格式?
  • Matplotlib 不具备导入 SVG 图像的能力。
  • @MattDMo 是的,我可以将使用 matplotlib 制作的部件导出为 SVG,但随后我需要另一个程序将它们与插图画家图像结合起来 - 所以问题是我可以使用什么程序来达到这个目的?我可以使用 matplotlib 本身将图像组合在一起,但正如 tom10 指出的那样,matplotlib 无法导入我使用 illustrator 制作的任何 SVG 图像。
  • @R_Beagrie Illustrator 应该能够导入 SVG(你可能需要一个插件,我不记得了)。根据您的平台,还有许多免费的在线 SVG 编辑程序可用。 Windows 版的 Inkscape 相当不错。

标签: python matplotlib vector-graphics adobe-illustrator reproducible-research


【解决方案1】:

经过更广泛的谷歌搜索后,我发现了这个旧的message on the matplotlib mailing list

帖子建议使用python库PyX,这对我来说效果很好。

我可以将 illustrator 图和 matplotlib 图都保存为 .eps 文件,然后像这样将它们组合在一起:

import pyx
c = pyx.canvas.canvas()
c.insert(pyx.epsfile.epsfile(0, 0, "1.eps", align="tl"))
c.insert(pyx.epsfile.epsfile(0,0,"2.eps", align="tr"))
c.writeEPSfile("combined.eps")

【讨论】:

    【解决方案2】:

    我在 svgutils 文档中找到了这个 example,它概述了如何将 matplotlib 生成的 SVG 组合成一个图。

    这是该页面中的示例:

    import svgutils.transform as sg
    import sys 
    
    #create new SVG figure
    fig = sg.SVGFigure("16cm", "6.5cm")
    
    # load matpotlib-generated figures
    fig1 = sg.fromfile('sigmoid_fit.svg')
    fig2 = sg.fromfile('anscombe.svg')
    
    # get the plot objects
    plot1 = fig1.getroot()
    plot2 = fig2.getroot()
    plot2.moveto(280, 0, scale=0.5)
    
    # add text labels
    txt1 = sg.TextElement(25,20, "A", size=12, weight="bold")
    txt2 = sg.TextElement(305,20, "B", size=12, weight="bold")
    
    # append plots and labels to figure
    fig.append([plot1, plot2])
    fig.append([txt1, txt2])
    
    # save generated SVG files
    fig.save("fig_final.svg")
    

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多