1

我有一个由 8 个图组成的 matplotlib 图imshow

fontP = FontProperties()
fontP.set_size('small')

%matplotlib inline
fig, ax = plt.subplots(nrows=4, ncols=2,figsize=[6,6], sharex=True, sharey=True) 

vmax=1500
marker_size=70
plot_norm=SqueezedNorm(vmin=0, vmax=vmax, mid=marker_size+5, s1=3, s2=2)

im = ax[0][0].imshow(df0_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[0][0].set_title('(a) left eye, single marker, 2d',y=-0.25)
ax[0][0].set_xticks([0, int(round(w/2)) ,w])
ax[0][0].set_yticks([h, 15 ,0])
ax[0][0].set_xlim(xmin=0, xmax=w)
ax[0][0].set_ylim([h, 0])

ax[0][1].imshow(df1_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[0][1].set_title('(b) right eye, single marker, 2d',y=-0.25)

ax[1][0].imshow(df2_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[1][0].set_title('(c) left eye, manual marker, 2d',y=-0.25)

ax[1][1].imshow(df3_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[1][1].set_title('(d) right eye, manual marker, 2d',y=-0.25)

ax[2][0].imshow(df4_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[2][0].set_title('(e) both eyes, single marker, 3d',y=-0.25)

ax[2][1].imshow(df5_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[2][1].set_title('(f) both eyes, manual marker, 3d',y=-0.25)

ax[3][0].imshow(df6_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[3][0].set_title('(g) both eyes, single marker, 2d',y=-0.45)

ax[3][1].imshow(df7_c_grid, cmap=plt.cm.BuPu_r, norm=plot_norm, aspect="auto", interpolation=None)
ax[3][1].set_title('(h) both eyes, manual marker, 2d',y=-0.45)

plt.tight_layout()

cax = plt.axes([0.1, 1.015, 0.8, 0.035])
fig.colorbar(im, cax=cax, orientation='horizontal',ticks=[0,marker_size,vmax])

# pos : [left, bottom] 
fig.text(0.55, 0.98, 'gaze to marker center distance (in px)', ha='center')

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    "font.family": "serif",
    "font.serif": [],                   # blank entries should cause plots to inherit fonts from the document
    "font.sans-serif": [],
    "font.monospace": [],
    "axes.labelsize": 12,               # LaTeX default is 10pt font.
    "font.size": 12,
    "legend.fontsize": 12,               # Make the legend/label fonts a little larger
    "xtick.labelsize": 12,
    "ytick.labelsize": 12,
    #"figure.figsize": figsize(0.4),     # default fig size of 0.9 textwidth
    "pgf.preamble": [
        r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts becasue your computer can handle it :)
        r"\usepackage[T1]{fontenc}",        # plots will be generated using this preamble
        ]
    }
mpl.rcParams.update(pgf_with_latex)

我将图形保存为 *.pgf 文件以将其导入我的 Miktex 文件。

plt.savefig('mypath/myfile.pgf', bbox_inches='tight')

该代码确实有效。*.pgf 文件将 8 个绘图和颜色条加载为 *.png 文件。这是可以忍受的。但是,*.png 图像的质量会受到影响:它们看起来很模糊,并且在我的数据点周围绘制了灰色边框。

最终乳胶文档中的模糊 imshow 图

我的问题:我可以将我savefig的图形作为 *.pgf 但带有矢量图形,如 *.svg,用于绘图和颜色条吗?

我尝试了以下代码。但导入我的 Miktex 文件失败:

plt.savefig('mypath/myfile.pgf', bbox_inches='tight', format='svg')
4

0 回答 0