Matplotlib烛台图看起来很奇怪 [英] Matplotlib Candlestick graph looks weird

查看:152
本文介绍了Matplotlib烛台图看起来很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用从quandl中提取的股票数据绘制烛台图。但是,该图看起来很怪异,只显示了几个小节。我不知道发生了什么。

I was trying to plot a candlestick graph using stock data extracted from quandl. However, the graph looks eerie, only a few bars showed up. I have no idea what is going on. Any help is greatly appreciated.

这里是代码:

start=dt.datetime(2000,1,1)
end=dt.datetime(2017,9,20)
df=quandl.get('WIKI/TWTR',start_date=start,end_date=end)
date_val=[x for x in range(len(df.index))]
open_val=np.array(df['Adj. Open'],dtype=np.float64)
high_val=np.array(df['Adj. High'],dtype=np.float64)
low_val=np.array(df['Adj. Low'],dtype=np.float64)
close_val=np.array(df['Adj. Close'],dtype=np.float64)
ohlc_data=[date_val,open_val,high_val,low_val,close_val]
ax1=plt.subplot(111)
candlestick_ohlc(ax1,ohlc_data,width=0.9,colorup='g',colordown='r',alpha=0.8)
plt.show()

这是图形:

This is the graph:

推荐答案

这里有两个主要问题。

There are two major issue here.


  • 日期应为与所讨论日期相对应的数值。

  • The dates should be numeric values corresponding to the dates in question.

date_val=matplotlib.dates.date2num(df.index.to_pydatetime()) 


  • candlestick_ohlc 的第二个参数需要是一个序列序列,即每个数据需要分别作为(时间,开盘价,最高价,最低价,收盘价...)

  • The second argument of candlestick_ohlc needs to be a sequence of sequences, i.e. each datum needs to be in it individually as (time, open, high, low, close, ...).
  • 可能的解决方案:

    import matplotlib.pyplot as plt
    import matplotlib.dates
    from matplotlib.finance import candlestick_ohlc
    import datetime as dt
    import quandl
    
    start=dt.datetime(2000,1,1)
    end=dt.datetime(2017,9,20)
    df=quandl.get('WIKI/TWTR',start_date=start,end_date=end)
    
    #convert dates to datetime, then to float
    date_val=matplotlib.dates.date2num(df.index.to_pydatetime()) 
    
    open_val=df['Adj. Open'].values
    high_val=df['Adj. High'].values
    low_val=df['Adj. Low'].values
    close_val=df['Adj. Close'].values
    # ohlc_data needs to be a sequence of sequences
    ohlc_data=zip(*[date_val,open_val,high_val,low_val,close_val])
    
    ax1=plt.subplot(111)
    candlestick_ohlc(ax1,ohlc_data,colorup='g',colordown='r',alpha=0.8)
    
    # Format x axis for dates
    ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y-%m-%d'))
    ax1.figure.autofmt_xdate()
    plt.show()
    

    或更简洁:

    import matplotlib.pyplot as plt
    import matplotlib.dates
    from matplotlib.finance import candlestick_ohlc
    import datetime as dt
    import quandl
    
    start=dt.datetime(2000,1,1)
    end=dt.datetime(2017,9,20)
    df=quandl.get('WIKI/TWTR',start_date=start,end_date=end)
    
    #convert dates to datetime, then to float
    df["Date"]=matplotlib.dates.date2num(df.index.to_pydatetime())
    ohlc_data= df[["Date",'Adj. Open','Adj. High','Adj. Low','Adj. Close']].values
    
    ax1=plt.subplot(111)
    candlestick_ohlc(ax1,ohlc_data,colorup='g',colordown='r',alpha=0.8)
    
    # Format x axis for dates
    ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y-%m-%d'))
    ax1.figure.autofmt_xdate()
    plt.show()
    

    这篇关于Matplotlib烛台图看起来很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆