作者cosmoSJ (cosmoSJ)
看板Python
标题[问题] matplotlib图片存档如何保留座标轴
时间Fri Feb 12 14:18:37 2016
大家好,新年恭喜
有个问题困扰我很久想请教一下大家
目前在做投资绩效回测的时候,因为要将结果输出成图比较容易确认
结果发现如果图用show的方法,可以正确显示坐标轴
但是如果savefig後,他的座标轴就会不见
范例如下
正常的
http://imgur.com/CSkxdA3
如果用saveig,会变成这个
http://imgur.com/yQEHyw2
附上原始码如下
fig = plt.figure(facecolor='#07000d')
ax1 = plt.subplot2grid((3,2), (1,0), rowspan=4, colspan=4,
axisbg='#07000d')
candlestick_ohlc(ax1,newAr[:],width=.6,colorup='#ff1717',colordown='#53c156')
par1 = ax1.twinx()
par1.plot(datenumArr[:],AcctAmtArr[:],color = "red",linewidth=1)
ax1.grid(True, color='w')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))
ax1.tick_params(axis='x', colors='w')
plt.ylabel('Stock price and Volume')
plt.sca(par1)
fig.savefig(u"test.png", format="png")
一直都试不出来问题在哪,只能请大家帮忙看看
感谢各位前辈
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.228.46.8
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1455257919.A.C46.html
1F:→ kerwinhui: 为什麽是 fig.savefig 而非 plt.savefig ? 02/13 02:13
2F:→ cosmoSJ: 其实两个都试过,但是都没有办法显示座标轴 02/13 11:21
3F:→ s860134: 如果不排斥档案格式 你可以尝试看看.savefig("test.pdf") 02/14 00:18