Closed
Description
I'm using matplotlib to plot some images in ipython notebook. But after saving fig as PNG, I get a different image than the one shown in the notebook. I've come across this issue multiple times. Mostly savefig()
lose the font info, but there maybe others. Can anyone take a look?
The code is as following:
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
mpl.rcParams['font.sans-serif'] = 'SimHei' #to render some Chinese character properly
d = [19, 14, 8, 21, 17, 10, 16, 11, 8]
cmap=plt.get_cmap('Set1', 6)
fig = plt.figure(dpi=400)
for i, x in enumerate(d):
plt.bar(i, x, color=cmap(i%3))
ax = gca()
xlim(-0.2, 9)
width = ax.patches[0].get_width()
ax.xaxis.set_minor_locator(FixedLocator(np.arange(9)+width/2))
ax.xaxis.set_minor_formatter(FixedFormatter(['合计', '鱼卵', '仔稚鱼']*3))
ax.xaxis.set_major_locator(FixedLocator(np.array([1, 4, 7])+width/2))
ax.xaxis.set_major_formatter(FixedFormatter(['2012年6月', '2013年6月', '2014年6月']))
ax.xaxis.set_tick_params(which='major', pad=17)
ax.xaxis.set_tick_params(which='both', tick1On=False, tick2On=False)
ax.yaxis.set_tick_params(which='both', tick1On=False, tick2On=False, label1On=False)
for r in ax.patches:
h = r.get_height()
ax.text(r.get_x()+w/2, h+0.4, int(h), ha='center', va='center')
legend(ax.patches[:3], ['合计', '鱼卵', '仔稚鱼'], loc='upper right')
plt.tight_layout()
fig.savefig('a.png', dpi=fig.dpi)
I got the fig as following in notebook:
And the 'a.png' from 'savefig()' is as following:
I'm doing some quick plot and formatting, using both 'pyplot' and 'pylab' sometimes, sorry for the dirty code.
Some backgroud:
Windows 10;
matplotlib '1.5.1rc1'
python 3.4
ipython 3.2.1
matplotlibrc file is set as default
Some test:
1. Result of `fig.savefig('a.svg')` is the same as shown in notebook.
2. If manually edit the `matplotlibrc` as following, `savefig` get the same result as shown in notebook.
font.family : sans-serif
font.sans-serif: SimHei