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

看板Python作者 (Yogi)時間9年前 (2016/02/14 00:16), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《cosmoSJ (cosmoSJ)》之銘言: : 大家好,新年恭喜 : 有個問題困擾我很久想請教一下大家 : 目前在做投資績效回測的時候,因為要將結果輸出成圖比較容易確認 : 結果發現如果圖用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") : 一直都試不出來問題在哪,只能請大家幫忙看看 : 感謝各位前輩 原始碼不完整 不過感覺有可能問題是出在你設定Axis的位置 舉個例子: import matplotlib.pylab as plt x = [1,2,3] y = map(lambda(x): x * 1.5, x) fig = plt.figure() ax = fig.add_axes([0,0,1,1]) ax.set_title("TEST") ax.set_xlabel("x") ax.plot(x, y, 'r--') fig.savefig("testfig.png") 這個輸出就會跟你的問題一樣 把上面那行改一下: import matplotlib.pylab as plt x = [1,2,3] y = map(lambda(x): x * 1.5, x) fig = plt.figure() ax = fig.add_axes([0.1,0.1,0.8,0.8]) ax.set_title("TEST") ax.set_xlabel("x") ax.plot(x, y, 'r--') fig.savefig("testfig.png") 這樣axis就跑出來了 因為原本axis的設定會剛好壓在整張圖片的最邊界處 當然就會變成不可見了 所以把axis設定往裡面挪一點點 就可以正常顯示了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.166.210 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1455380181.A.086.html

02/14 16:26, , 1F
感謝您 有解了 正是您提供的解法
02/14 16:26, 1F
文章代碼(AID): #1MlrRL26 (Python)
文章代碼(AID): #1MlrRL26 (Python)