linux - 无法打开 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')
解决方案
推荐阅读
- python - Python数据爬虫维基百科保护策略
- php - Laravel voyager - 无法解决需求
- c# - 重新启动 Windows.Web.Http.HttpClient on progress 事件的内部取消计时器
- mysql - 在mysql中隐藏一部分值而不改变它
- python - 将行相乘并按单元格值附加到数据框
- javascript - 在 PHP 应用程序中需要 .vue 单文件组件
- uservoice - uservoice - 如何解决超出 API 限制的问题?
- google-chrome - 如果我使用 chrome.storage.sync,但用户未登录 Google Chrome,会发生什么情况?
- python - Jupyter 笔记本启动错误
- python - 在 python 中调用多重继承函数失败