2017-09-26 67 views
0

此代碼構造matplotlib圖形,使用開放,高,低,關閉格式的網頁中的get_data函數獲取數據,並每五分鐘將這些值附加到列表中使用FuncAnimation然後更新燭臺圖表。但是,y軸值不正確,因爲它們只顯示1-7,應該是當前的S & P的價格在2500左右。如何解決?y軸在matplotlib.animation中未顯示正確的值

import matplotlib.pyplot as plt 
from matplotlib.finance import candlestick2_ohlc 
import matplotlib.animation as animation 
from matplotlib import style 
from es_cme_quote import get_data 
import numpy as np 

style.use('ggplot') 
fig=plt.figure() 
ax= fig.add_subplot(111) 
o_array=[] 
h_array=[] 
l_array=[] 
c_array=[] 
def update(dummy): 

    [O,H,L,C]=get_data() 

    def append_var(x,y): 
     y.append(x) 

    append_var(O[0],o_array) 
    append_var(H[0],h_array) 
    append_var(L[0],l_array) 
    append_var(C[0],c_array) 
    ax.clear() 
    candlestick2_ohlc(ax,o_array,h_array,l_array,c_array, 
    width=.8,colorup='g',colordown='r',alpha=1.0) 

anime=animation.FuncAnimation(fig,update,interval=300000) 
plt.show() 

回答

1

對於圖中的單個圖。

axes = plt.gca() 
axes.set_ylim([ymin,ymax]) 

來源:setting y-axis limit in matplotlib

+0

試圖ax.set_ylim([np.min(l_array),np.max(h_array)])和更新函數內AX = plt.gca()和添加它的所有方式仍然不起作用。 –

+0

解決方案就像你說的,但沒有set_ylim只是斧頭= plt.cla()放在ax.clear() –

+0

上面我很高興它解決了。 –

1
import matplotlib.pyplot as plt 
from matplotlib.finance import candlestick2_ohlc 
import matplotlib.animation as animation 
from matplotlib import style 
from es_cme_quote import get_data 
import numpy as np 

style.use('ggplot') 
fig=plt.figure() 
ax= fig.add_subplot(111) 
o_array=[] 
h_array=[] 
l_array=[] 
c_array=[] 
def update(dummy): 

    [O,H,L,C]= get_data() 

    def append_var(x,y): 
     y.append(x) 
    append_var(O[0],o_array) 
    append_var(H[0],h_array) 
    append_var(L[0],l_array) 
    append_var(C[0],c_array) 

    ax=plt.gca() 
    ax.clear() 
    candlestick2_ohlc(ax,o_array,h_array,l_array,c_array, 
    width=.8,colorup='g',colordown='r',alpha=1.0) 

anime=animation.FuncAnimation(fig,update,interval=300000) 
plt.show()