简体   繁体   English

matplotlib:savefig 省略了 eps 中的文本和轴标签

[英]matplotlib: savefig omits text and axes labels in eps

If I execute the following code, the resulting eps-file does not contain tick labels or axes labels.如果我执行以下代码,生成的 eps 文件不包含刻度标签或轴标签。

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,2*np.pi,100)
y = np.sin(x)

plt.figure()
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.savefig('image.eps', format='eps')

As some additional information: I am using version 4.3.0 of the anaconda distribution and execute my code in Spyder 3.1..2.作为一些附加信息:我正在使用 anaconda 发行版的 4.3.0 版并在 Spyder 3.1..2 中执行我的代码。 I tested the two backends TkAgg and Qt5Agg, the result is the same for both.我测试了两个后端 TkAgg 和 Qt5Agg,结果都是一样的。

Any ideas how I can get a correct eps-file?任何想法如何获得正确的 eps 文件?

Alternatively: which other vector graphics format could I use if I want to import into MS Word later?或者:如果我想稍后导入到 MS Word 中,我可以使用哪种其他矢量图形格式?

So I think that you will find your answer here: https://stackoverflow.com/a/20817722/2402281所以我认为你会在这里找到答案: https : //stackoverflow.com/a/20817722/2402281


It might be the case that your matplotlib backend does not support that operation.可能是您的 matplotlib 后端不支持该操作。 You can change the matplotlib backend as follows:您可以按如下方式更改 matplotlib 后端:

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
import matplotlib
matplotlib.use('PS')

or in the matplotlibrc (see the documentation ).或在 matplotlibrc 中(请参阅文档)。

I encountered same question.我遇到了同样的问题。 The eps file is fine when I plot time series or scatter plot.当我绘制时间序列或散点图时,eps 文件很好。 But when I am plotting geospatial plots using dataframe data, the saved eps file contain no texts and tick labels.但是当我使用数据框数据绘制地理空间图时,保存的 eps 文件不包含文本和刻度标签。 So I simply tried:所以我只是尝试:

import matplotlib
matplotlib.use('PS') 
matplotlib.rcParams['text.usetex'] = True

It seems that if you turn the text.usetex to True, it may help.似乎如果您将text.usetex为 True,它可能会有所帮助。 Btw, my Matplotlib is 3.4.2, and operation system is MacOS, and I am using Spyder with python 3.7.顺便说一句,我的 Matplotlib 是 3.4.2,操作系统是 MacOS,我使用的是 Python 3.7 的 Spyder。 Actually I don't know why it sometimes works sometimes does not work, and why it works after I adding matplotlib.rcParams['text.usetex'] = True ...It may be because I used alpha when I plot the geospatial plots (like set alpha=5 in ax.scatter ).实际上我不知道为什么它有时会起作用有时不起作用,以及为什么在我添加matplotlib.rcParams['text.usetex'] = True之后它起作用......这可能是因为我在绘制地理空间图时使用了 alpha (例如在ax.scatter设置alpha=5 )。 I will put some tiny examples later.稍后我会举一些小例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM