自matplotlib-1.0.1更新以来的动画问题

时间:2014-07-13 20:46:46

标签: python animation matplotlib

这个问题主要与Matplotlib和动画有关。问题是,当动画更新时,我需要每次清除轴,或者由于颜色的变化我得到重叠的图像。当我使用matplotlib-1.0.1时,下面的代码工作正常,但现在我正在使用matplotlib-1-3.1如果我继续在下面的代码中使用ax.clear(),那么ax的图像就不会出现了图表。这是代码:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from finance_pylab import quotes_historical_yahoo, candlestick,plot_day_summary, candlestick2,volume_overlay
from pylab import *
import f_charting
import f_datamanip # to use idx_of_last_unique
import f_tcnvrt # for dukascopy date
from numpy import*
import f_file_handling
import first_add2file
import f_dukascopy_fetcher
import datetime  ## so we can print a time stamp we recognize
import f_candle_stick_maker3_fluent

anim_data_file='candle_test6'
showlast_idxs=40
tframe=5 ##how many minutes candles will be #minutes
dukas_file_name="EURUSD_1m_jan2012"

first_candle=datetime.datetime(2012,1,1,22,10,0) ## start chart here
last_candle=datetime.datetime(2012,1,2,1,55,0) ## start trading after here

candle_name_LDP_diff=datetime.timedelta(0,(tframe-1)*60) ##LDP = last_data_point
dukas_last_data_point=last_candle+candle_name_LDP_diff

Dcsv,D,O,H,L,C,V=f_dukascopy_fetcher.dukas2(dukas_file_name,first_candle,dukas_last_data_point)## will get data up to data_end, could just put in high date to take all in file#,s_date)#,data_start,s_date)

f_candle_stick_maker3_fluent.candle_2_txtfile(anim_data_file,tframe,first_candle,last_candle,Dcsv,D,O,H,L,C,V,0)

filename='candle_test6';file_name=filename+'.txt'
candle_width=.8;colorup='#33CC33'; colordown='#E80000'  ;up_col='#B8B8B8';down_col='w'

rect1=[.05,.14,.94,.82]#left, bottom, width, height #rect1=[.1,.1,.8,.7] #seems full:rect1=[.02,.04,.95,.93] , the more you move left , you also have to adjust width. bottom and hight push on each other as well

fig =plt.figure(figsize=(15,7),facecolor='white');axescolor ='#f6f6f6'  #'#200000' #'#180000' ##100000'#f6f6f6'  # the axies background color # border of chart
ax = fig.add_axes(rect1, axisbg=axescolor)  #start with volume axis
ax1v = ax.twinx()

def candle_animate(i):

  pullData = open(anim_data_file+'.txt','r').read()
  dataArray= pullData.split('\n')
  contig_time=[];_open=[];_close=[];_high=[];_low=[];_vol=[]; _timevec=[]
  for eachLine in dataArray:
     if len(eachLine)>1:

        _t,_o,_c,_h,_l,_v,u=eachLine.split(',')##x,y=eachLine.split(',')
        _timevec.append(f_tcnvrt.str2time_dukascopy(_t))
        _open.append(float(_o))
        _close.append(float(_c))
        _high.append(float(_h))
        _low.append(float(_l))
        _vol.append(float(_v))
        units_print=u

  _open=array(_open);_close=array(_close);_high=array(_high);_low=array(_low);_vol=array(_vol)
  ax.clear();ax1v.clear() # this line worked with matplotlib-1.0.1 but matplotlib-1.3.1 keeps ax blank

  D=_timevec;O=_open;H=_high;L=_low;C=_close;V=_vol

  time_delta=datetime.timedelta(0,tframe*60)
  last_data_point=D[-1]
  _time,_open,_high,_low,_close,_vol=f_candle_stick_maker3_fluent.cs_maker(tframe,first_candle,last_data_point,D,O,H,L,C,V)
  contig_time=range(0,len(_time))
  chartstart=len(contig_time)-showlast_idxs
  numXlbls=12

  myidx,x_label=f_charting.x_labels_last_tick_showall_contig(_time,numXlbls)

  data4candleshow=transpose([contig_time,_open,_close,_high,_low,_vol])

  data4candle=transpose([contig_time,_open,_close,_high,_low,_vol])[-showlast_idxs:]

  candlestick(ax, data4candle, width=candle_width,colorup='#33CC33',colordown='#E80000') #'#00FF00' '#C11B17'

  f_charting.bar_vol(contig_time[-showlast_idxs:],_open[-showlast_idxs:],_close[-showlast_idxs:],_vol[-showlast_idxs:],ax1v,candle_width,up_col,down_col)

ani=animation.FuncAnimation(fig,candle_animate, interval=1000)
plt.show()

我从代码中拿了很多东西以使其更简单,但我知道还有很多东西要看。希望有些专家更了解2个matplotlib版本之间的区别,或者熟悉我的问题。必须有一个干净的方法来清除更新之间的图表,所以我不会得到重叠的图像。

注意:行:ax.clear(); ax1v.clear()可以在上面的代码中找到并且旁边有一个注释,表示这是在旧的matplotlib中用于其目的的行,但是现在不幸的是,在使用matplotlib-1.3.1

时会清除图表

谢谢

0 个答案:

没有答案