[問題] matplotlib圖片存檔如何保留座標軸

作者: cosmoSJ (cosmoSJ)   2016-02-12 14:18:37
大家好,新年恭喜
有個問題困擾我很久想請教一下大家
目前在做投資績效回測的時候,因為要將結果輸出成圖比較容易確認
結果發現如果圖用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")
一直都試不出來問題在哪,只能請大家幫忙看看
感謝各位前輩
作者: kerwinhui (kezza)   2016-02-13 02:13:00
為什麼是 fig.savefig 而非 plt.savefig ?
作者: cosmoSJ (cosmoSJ)   2016-02-13 11:21:00
其實兩個都試過,但是都沒有辦法顯示座標軸
作者: s860134 (s860134)   2016-02-14 00:18:00
如果不排斥檔案格式 你可以嘗試看看.savefig("test.pdf")

Links booklink

Contact Us: admin [ a t ] ucptt.com