简体   繁体   English

使用`savefig'时,缺少`matplotlib.imshow`在PDF中插入的图像

[英]Missing images inserted by `matplotlib.imshow` in PDF when using `savefig`

I am trying to insert images to annotate a graph like this: 我正在尝试插入图像来注释这样的图形:

from matplotlib import offsetbox
from matplotlib import pyplot as plt
from matplotlib import rcParams

rcParams["figure.dpi"] = 200
rcParams["figure.figsize"] = 4,3
alphaEndo = pd.read_csv('../data/UNIT_VOL_FRACTION.ENDO_DODECAHEDRON.csv', comment="#")

fig, ax = plt.subplots()
ax.grid(False)

ax.set_ylabel("$\\alpha_c(s)$")
ax.set_xlabel("$s$")

colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

def plot_polyhedron(N, xScale, yScale, ax):
    img = plt.imread("../data/UNIT_VOL_ENDO_DODECAHEDRON_IMAGES/UNIT_VOL_ENDO_DODECAHEDRON.%04d.png" % N) 
    img = offsetbox.OffsetImage(img, zoom=0.07)
    img.image.axes = ax
    ab = offsetbox.AnnotationBbox(img, xy=(alphaEndo['S'].loc[N], alphaEndo['ALPHA_STAR'].loc[N]),
                                  xybox=(xScale*alphaEndo['S'].loc[N], yScale*alphaEndo['ALPHA_STAR'].loc[N]),
                                  frameon=False, arrowprops=dict(arrowstyle="Simple",facecolor=colors[0]),pad=False)
    ax.add_artist(ab)      

plot_polyhedron(5, 2, 0.6, ax)
plot_polyhedron(50,0.65, 0.35, ax)
plot_polyhedron(95, 0.7, 3, ax)
ax.plot(alphaEndo['S'], alphaEndo['ALPHA_STAR'], lw=2)


figBaseName = "ENDO_DODECAHEDRON_ALPHA_S"
fig.savefig(figBaseName + ".png")
fig.savefig(figBaseName + ".pdf")

if "GEOP" in os.environ:
    pathName = os.path.join(os.environ["GEOP"], "figures", figBaseName)
    fig.savefig(pathName + ".png")
    fig.savefig(pathName + ".pdf")

For the PNG file, I get this: 对于PNG文件,我得到以下信息:

在此处输入图片说明

but in the PDF, the small images of the polyhedron are missing: 但在PDF中,缺少多面体的小图像:

在此处输入图片说明

I found this question about missing images, but the answer doesn't really help in my case, because I am not explicitly calling "plt.show()" before "plt.savefig()", I'm using axes. 我发现了有关丢失图像的问题 ,但是对于我的情况,答案并没有真正帮助,因为我没有在“ plt.savefig()”之前显式调用“ plt.show()”,而是使用轴。 The code is from a Jupyter notebook cell. 该代码来自Jupyter笔记本单元。

Please, try this modification: 请尝试以下修改:

def plot_polyhedron(N, xScale, yScale, ax):
    img = plt.imread("../data/UNIT_VOL_ENDO_DODECAHEDRON_IMAGES/UNIT_VOL_ENDO_DODECAHEDRON.%04d.png" % N) 
    imagebox = offsetbox.OffsetImage(img, zoom=0.07)
    imagebox.image.axes = ax
    ab = offsetbox.AnnotationBbox(imagebox, xy=(alphaEndo['S'].loc[N], alphaEndo['ALPHA_STAR'].loc[N]),
                                  xybox=(xScale*alphaEndo['S'].loc[N], yScale*alphaEndo['ALPHA_STAR'].loc[N]),
                                  frameon=False, arrowprops=dict(arrowstyle="Simple",facecolor=colors[0]),pad=False)
    ax.add_artist(ab)   

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

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