veritaS
veritaS

Reputation: 519

matplotlib only plotting date instead of given datetime variable

I am trying to plot candle sticks but for some reason, the plot does not grab the entire DateTime variable. But only the date. Plotting all candles on the same X axis rather than all 15 min as the data is given in.

Following code.

        ##  PLOT Candles
        plt.figure()

        #define width of candlestick elements
        width = .5
        width2 = .05

        #define up and down prices
        up = df[df.close>=df.open]
        down = df[df.close<df.open]
        print(up)
        #define colors to use
        col1 = 'green'
        col2 = 'red'
        col3 = 'blue'
        col4 = 'grey'

        #plot up prices
        plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
        plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
        plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)

        #plot down prices
        plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
        plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
        plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
        
        plt.show()
        exit()

Following is the print of up enter image description here

enter image description here

print(df[:5].to_dict())

{'open': {Timestamp('2016-01-14 08:15:00'): 1.08719, Timestamp('2016-01-14 08:30:00'): 1.08735, Timestamp('2016-01-14 08:45:00'): 1.08674, Timestamp('2016-01-14 09:00:00'): 1.08674, Timestamp('2016-01-14 09:15:00'): 1.08671}, 'high': {Timestamp('2016-01-14 08:15:00'): 1.08749, Timestamp('2016-01-14 08:30:00'): 1.08739, Timestamp('2016-01-14 08:45:00'): 1.08734, Timestamp('2016-01-14 09:00:00'): 1.08722, Timestamp('2016-01-14 09:15:00'): 1.08673}, 'low': {Timestamp('2016-01-14 08:15:00'): 1.0869, Timestamp('2016-01-14 08:30:00'): 1.08673, Timestamp('2016-01-14 08:45:00'): 1.08669, Timestamp('2016-01-14 09:00:00'): 1.08666, Timestamp('2016-01-14 09:15:00'): 1.08582}, 'close': {Timestamp('2016-01-14 08:15:00'): 1.08736, Timestamp('2016-01-14 08:30:00'): 1.08673, Timestamp('2016-01-14 08:45:00'): 1.08673, Timestamp('2016-01-14 09:00:00'): 1.08671, Timestamp('2016-01-14 09:15:00'): 1.08618}, 'volume': {Timestamp('2016-01-14 08:15:00'): 2181, Timestamp('2016-01-14 08:30:00'): 1738, Timestamp('2016-01-14 08:45:00'): 1938, Timestamp('2016-01-14 09:00:00'): 3010, Timestamp('2016-01-14 09:15:00'): 2734}}

Upvotes: 2

Views: 87

Answers (1)

Drakax
Drakax

Reputation: 1513

Well, let's do this!

First things first: https://pypi.org/project/mplfinance/

The New API This repository, matplotlib/mplfinance, contains a new matplotlib finance API that makes it easier to create financial plots. It interfaces nicely with Pandas DataFrames.

More importantly, the new API automatically does the extra matplotlib work that the user previously had to do "manually" with the old API.

pip install --upgrade mplfinance
#or
import mplfinance as mpf

Given the sample I asked you:

data = {'open': {'2016-01-14 08:15:00': 1.08719, '2016-01-14 08:30:00': 1.08735, '2016-01-14 08:45:00': 1.08674, '2016-01-14 09:00:00': 1.08674, '2016-01-14 09:15:00': 1.08671}, 'high': {'2016-01-14 08:15:00': 1.08749, '2016-01-14 08:30:00': 1.08739, '2016-01-14 08:45:00': 1.08734, '2016-01-14 09:00:00': 1.08722, '2016-01-14 09:15:00': 1.08673}, 'low': {'2016-01-14 08:15:00': 1.0869, '2016-01-14 08:30:00': 1.08673, '2016-01-14 08:45:00': 1.08669, '2016-01-14 09:00:00': 1.08666, '2016-01-14 09:15:00': 1.08582}, 'close': {'2016-01-14 08:15:00': 1.08736, '2016-01-14 08:30:00': 1.08673, '2016-01-14 08:45:00': 1.08673, '2016-01-14 09:00:00': 1.08671, '2016-01-14 09:15:00': 1.08618}, 'volume': {'2016-01-14 08:15:00': 2181, '2016-01-14 08:30:00': 1738, '2016-01-14 08:45:00': 1938, '2016-01-14 09:00:00': 3010, '2016-01-14 09:15:00': 2734}}

Transform "data" into a df and convert the index to the right format:

df = pd.DataFrame(data)
df.index = pd.to_datetime(df.index)
df

Then plot it!

mpf.plot(df, type='candle')

Output:

enter image description here

Upvotes: 2

Related Questions