首页 > 解决方案 > 无法打开 matplotlib 生成的 EPS 文件

问题描述

plt.savefig(name, format='eps')在python中使用过。我直接使用了 GIMP、LibreOffice、Okular、Evince 和 Ghostscript,它们都无法打开结果。当它保存为 PNG 时,它可以工作,但不能保存为 EPS。我正在使用 Linux。

这是 gs 给出的错误:

GPL Ghostscript 9.54.0 (2021-03-30) 版权所有 (C) 2021 Artifex Software, Inc. 保留所有权利。该软件是在 GNU AGPLv3 下提供的,并且没有任何保证:请参阅文件复制了解详细信息。错误:/undefined in --setcachedevice-- 操作数堆栈:CharStrings --dict:17/30(L)-- e --nostringval-- 1300 0 113 -29 1114 1556 执行堆栈:%interp_exit .runexec2 --nostringval- - --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1990 1 3 %oparray_pop 1989 1 3 %oparray_pop 1988 1 3 %oparray_pop -- nostringval-- 1977 1 3 %oparray_pop 1833 1 3 %oparray_pop --nostringval-- %errorexec_pop。

埃文斯给出了这个:

(libspectre) ghostscript 报告:未定义 -21 (libspectre) ghostscript 报告:未定义 -21

当我运行命令文件时,结果似乎表明它是一个有效的 eps 文件:

符合 DSC 级别 3.0 的 PostScript 文档文本,类型 EPS

由于我对此进行了更多测试,因此当我不提供图例时,EPS 文件可以正常打开。但是,当我添加标签和图例时,ghostscript 无法打开文件:

这是代码:

for graph in graphs:
    # Split the file extension from name
    name = graph.filename.split('.', 1)[0]
    # Get the values for X. This will act as x-axis
    X = graph.data_dict['X']
    # Go through each column (except for X) and plot
    for column in (i for i in graph.data_dict if i != 'X'):
        # Get the data for the column
        y = graph.data_dict[column]
        # Plot it
        plt.plot(X,y,label=column)
        plt.yticks(fontsize="small")
    # Set the title to the file name
    plt.title(name)
    # X is the x label
    plt.xlabel('X')
    plt.xticks(fontsize="small")
    # Provide a legend
    plt.legend()
    plt.margins(0, tight=True)

    plt.savefig(name + ".eps", format='eps')

这将正确显示:

for graph in graphs:
    # Split the file extension from name
    name = graph.filename.split('.', 1)[0]
    # Get the values for X. This will act as x-axis
    X = graph.data_dict['X']
    # Go through each column (except for X) and plot
    for column in (i for i in graph.data_dict if i != 'X'):
        # Get the data for the column
        y = graph.data_dict[column]
        # Plot it
        plt.plot(X,y)
        plt.yticks(fontsize="small")
    # Set the title to the file name
    # X is the x label
    plt.xticks(fontsize="small")
    # Provide a legend
    plt.margins(0, tight=True)

    plt.savefig(name + ".eps", format='eps')

标签: linuxmatplotlibghostscript

解决方案


推荐阅读