如何消除matplotlib创建的烛台图表中的空白?

时间:2016-03-14 15:07:56

标签: python matplotlib quantitative-finance candlestick-chart

股票的数据是这样的:

    date    open    high    low     close   date_ori
53  735999  340.5   340.5   332.5   336.0   2016-02-05
54  736009  330.5   342.0   330.0   339.5   2016-02-15
55  736010  340.5   341.5   337.5   339.0   2016-02-16

从2016-02-05到2016-02-15,有一个日期差距。

然后,我用matplotlib

创建了一个烛台图表
fig, ax = plt.subplots(figsize=(25, 15), dpi=300)
fig.subplots_adjust(bottom=0.2)
mpf.candlestick_ohlc(ax, quotes, width=0.5, colorup='r', colordown='g')
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=30)

图表如下: enter image description here

如何消除图表上的间隙以使烛台线连续?

1 个答案:

答案 0 :(得分:1)

在这里。

    #!/usr/bin/env python
    import matplotlib.pyplot as plt
    from matplotlib.dates import DateFormatter, WeekdayLocator,\
        DayLocator, MONDAY
    from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
    import matplotlib.dates as mdates
    import datetime as dt

    # (Year, month, day) tuples suffice as args for quotes_historical_yahoo
    date1 = (2016, 2, 1)
    date2 = dt.datetime.today()


    mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
    alldays = DayLocator()              # minor ticks on the days
    weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
    dayFormatter = DateFormatter('%d')      # e.g., 12

    quotes = quotes_historical_yahoo_ohlc('INTC', date1, date2)


    weekday_quotes = [tuple([i]+list(quote[1:])) for i,quote in enumerate(quotes)] #####

    print weekday_quotes




    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(weekFormatter)
    ax.xaxis.set_minor_formatter(dayFormatter)

    #plot_day_summary(ax, quotes, ticksize=3)
    candlestick_ohlc(ax, weekday_quotes, width=0.6) ####
    #candlestick_ohlc(ax, quotes, width=0.6) ####



    ax.set_xticks(range(0,len(weekday_quotes),5))####
    ax.set_xticklabels([mdates.num2date(quotes[index][0]).strftime('%b-%d') for index in ax.get_xticks()])####

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

    plt.show()