作者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://www.ptt.cc/bbs/Python/M.1455257919.A.C46.html
→ kerwinhui: 為什麼是 fig.savefig 而非 plt.savefig ? 02/13 02:13
→ cosmoSJ: 其實兩個都試過,但是都沒有辦法顯示座標軸 02/13 11:21
→ s860134: 如果不排斥檔案格式 你可以嘗試看看.savefig("test.pdf") 02/14 00:18