如何通过matplotlib.finance为以下格式数据绘制“K”图?

时间:2016-10-25 13:25:29

标签: python matplotlib

我是matplotlib的新手,现在我真的很困惑。 代码如下:

import matplotlib.finance as mpf
import matplotlib.pyplot as plt
import tushare as ts
start = '2016-09-01'
end = '2016-09-30'
quotes = ts.get_hist_data('sh', start=start, end=end)
quote = quotes[['open', 'close', 'high', 'low']]
fig, ax = plt.subplots(figsize=(8, 5))
fig.subplots_adjust(bottom=0.2)
mpf.candlestick2_ochl(ax, opens, closes, highs, lows, width=0.6, colorup='k', colordown='r', alpha=0.75)
plt.grid(True)
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=30)
plt.show()

“引用”的数据是:

               open    close     high      low
date                                          
2016-09-30  2994.25  3004.70  3009.20  2993.06
2016-09-29  2992.17  2998.48  3009.20  2991.91
2016-09-28  3000.70  2987.86  3000.70  2984.32
2016-09-27  2974.59  2998.17  2998.23  2969.13
2016-09-26  3028.24  2980.43  3028.24  2980.12
2016-09-23  3044.79  3033.90  3046.80  3032.80
2016-09-22  3038.42  3042.31  3054.44  3035.07
2016-09-21  3021.58  3025.87  3032.45  3017.54
2016-09-20  3027.17  3023.00  3027.82  3015.88
2016-09-19  3005.32  3026.05  3026.65  3005.32
2016-09-14  3008.90  3002.85  3017.94  2995.42
2016-09-13  3025.03  3023.51  3029.72  3008.74
2016-09-12  3037.51  3021.98  3040.95  2999.93
2016-09-09  3095.43  3078.85  3101.79  3078.22
2016-09-08  3089.95  3095.95  3096.78  3083.90
2016-09-07  3091.33  3091.93  3105.68  3087.88
2016-09-06  3071.06  3090.71  3095.51  3053.19
2016-09-05  3070.71  3072.10  3085.49  3065.33
2016-09-02  3057.49  3067.35  3072.53  3050.49
2016-09-01  3083.96  3063.31  3088.70  3062.88

我收到了这个错误:

ValueError: ordinal must be >= 1

我该如何解决?

1 个答案:

答案 0 :(得分:0)

使用Python3.5查看工作代码:

import matplotlib.pyplot as plt 
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
import matplotlib.finance as mpf

def main():
    # plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    ticker = '600028'  
    ticker += '.ss'    

    date1 = (2015, 8, 1)  
    date2 = (2016, 1, 1)   


    mondays = WeekdayLocator(MONDAY)           
    alldays = DayLocator()                     
    #weekFormatter = DateFormatter('%b %d')     
    mondayFormatter = DateFormatter('%Y-%m-%d')  
    dayFormatter = DateFormatter('%d')         

    quotes = mpf.quotes_historical_yahoo_ochl('^GDAXI', date1, date2)

    if len(quotes) == 0:
        raise SystemExit

    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2)

    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(mondayFormatter)

    mpf.candlestick_ohlc(ax, quotes, width=0.6, colorup='r', colordown='g')

    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

    ax.grid(True)
    plt.title('600028')
    plt.show()
    return

if __name__ == '__main__':
    main()

#Slight modified base on http://blog.csdn.net/matrix_laboratory/article/details/50688275

或者使用Python3.5的这个工作代码:

import matplotlib.pyplot as plt
import matplotlib.finance as mpf  
from dateutil import parser
import matplotlib.dates as mpl_dt
import tushare as ts

start = '2016-09-01'
end = '2016-09-30'
df = ts.get_hist_data('sh', start=start, end=end)
DATA = df[['open', 'high', 'close', 'low', 'volume']]
DATA = DATA.reset_index()
DATA["date"]  = DATA["date"].apply(lambda x : mpl_dt.date2num(parser.parse(x)))
fig, ax = plt.subplots()
mpf.candlestick_ohlc(ax, DATA.values, width=0.6, colorup='r', colordown='g')
plt.grid(True)
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=30)
plt.show()


#cf: http://stackoverflow.com/questions/35677173/charting-with-candlestick-ohlc