我正在尝试插入图像来注释这样的图形:
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")
对于PNG文件,我得到了:
但是在PDF中,缺少多面体的小图像:
我发现this question关于丢失图像,但是对于我的情况,答案并没有真正帮助,因为我没有在“ plt.savefig()”之前明确调用“ plt.show()”, m使用轴。该代码来自Jupyter笔记本单元。
答案 0 :(得分:0)
请尝试以下修改:
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)