2

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: enter image description here

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

seizetheday
  • 333
  • 1
  • 6
  • 15
  • Possible duplicate of [Python/Matplotlib - Is there a way to make a discontinuous axis?](http://stackoverflow.com/questions/5656798/python-matplotlib-is-there-a-way-to-make-a-discontinuous-axis) – tmdavison Mar 14 '16 at 15:36
  • Also see http://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib – tmdavison Mar 14 '16 at 15:37
  • And the gallery example [here](http://matplotlib.org/examples/pylab_examples/broken_axis.html) – tmdavison Mar 14 '16 at 15:37
  • @tom, thanks, but I want to remove the gap to make the chart continuous, not to make a discontinuous axis. – seizetheday Mar 14 '16 at 16:39
  • OK, fair enough. But, won't that be misleading, since time the x axis will not be linear? – tmdavison Mar 14 '16 at 16:40
  • There are no trading on holiday, so x axis may be not continuous. – seizetheday Mar 14 '16 at 16:48
  • 1
    this does exactly what you like: http://stackoverflow.com/questions/9673988/intraday-candlestick-charts-using-matplotlib – tribol Mar 15 '16 at 17:35
  • Possible duplicate of [Intraday candlestick charts using MatPlotLib](http://stackoverflow.com/questions/9673988/intraday-candlestick-charts-using-matplotlib) – lanery Feb 02 '17 at 06:39

1 Answers1

1

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()
  • 2
    Please add at least a little explanation. This code dump is not going to be very helpful to someone not already familiar with the answer. – Mad Physicist Jun 09 '16 at 00:58