1

The answer to this question appears relevant to my problem, however, it applies for ax.bar() instead of ax.vlines.

Matplotlib DateFormatter for axis label not working

The code below works with ax1.vlines(x, l, h, colors='k') and ax2.vlines(x, 0, v, colors='k') to plot vertical price and volume bars in a stock chart. But the horizontal axis is defined by a numpy array x = 0,1,2,3, ... etc. I have datetime objects in array d but if change to ax1.vlines(d, l, h, colors='k') and ax2.vlines(d,0,v,colors='k') then it throws an error. Thus d is defined but not used in the code below (it won't work using d but it works using x in the referenced code lines).

    import datetime
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt

    df = pd.read_csv(PATH+ticker+EXT, usecols=[0,2,3,4,5], header=None,
                     engine='python',skiprows=skr,skipfooter=skf)
    d = pd.to_datetime(df[0]) # numpy array date
    h = df[2].values # numpy array high
    l = df[3].values # numpy array low
    c = df[4].values # numpy array close
    v = df[5].values # numpy array volume
    x = np.arange(len(d))
    # Draw Chart to White Background        
    ax1_y_label = ticker
    fig1 = plt.figure()
    fig1.set_size_inches(WIDE,TALL)
    fig1.set_dpi(DTPI)
    fig1.autofmt_xdate()
    ax1 = plt.subplot2grid((5,4), (0,0), rowspan=4, colspan=4)
    ax1.set_ylabel(ax1_y_label)
    ax1.grid(True)
    ax1.vlines(x, l, h, colors='k')
    ax1.hlines(c, x, x+0.3, color='k')
    ax2 = plt.subplot2grid((5,4), (4,0), sharex=ax1, rowspan=1, colspan=4)
    ax2.set_ylabel(ax2_y_label)
    ax2.grid(True)
    ax2.vlines(x, 0, v, colors='k')
    ax1.spines['top'].set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax2.spines['right'].set_visible(False)
    plt.setp(ax1.get_xticklabels(), visible=False)
    plt.setp(ax1.get_yticklabels(), visible=False)
    plt.setp(ax2.get_yticklabels(), visible=False)
    plt.subplots_adjust(hspace=.01)
Community
  • 1
  • 1
SystemTheory
  • 339
  • 3
  • 15
  • 1
    If you could provide a [MCVE], people might be more willing to help you find a solution. In your code it's not even clear where `d` is used and what the problem is. – ImportanceOfBeingErnest Nov 26 '16 at 23:05

0 Answers0