简体   繁体   中英

How to remove the gap in candlestick chart created by matplotlib?

The data of stock is like this:

    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

From 2016-02-05 to 2016-02-15, there is a date gap.

Then, I have created a candlestick chart with 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)

The chart is as the following: 在此处输入图片说明

How to remove the gap on the chart to make the candlestick line continuous?

Here it is.

    #!/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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM