0

I'm trying to create a candlestick graph using some data that I have stored in a CSV file and the mpl_finance library:

The first five rows look like this:

              Date               Open      High       Low     Close    Volume

0  2017-08-24 04:00:00.000000  0.002890  0.002890  0.002670  0.002670  371.64
1  2017-08-24 04:05:00.000000  0.002669  0.002669  0.002669  0.002669    4.80
2  2017-08-24 04:10:00.000000  0.002600  0.002649  0.002600  0.002649   61.71
3  2017-08-24 04:15:00.000000  0.002640  0.002640  0.002640  0.002640   20.00
4  2017-08-24 04:20:00.000000  0.002620  0.002620  0.002600  0.002602   92.68   

Below is the code I'm using:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ochl

df = pd.read_csv("data.csv")
df["Date"] = pd.to_datetime(df["Date"])

candlestick2_ochl(ax = df["Date"],opens=df["Open"],closes=df["Close"],highs =df["High"],lows = df["Low"])
plt.show()

When I run this code I get the following error:

Traceback (most recent call last):
  File "Untitled 5.py", line 16, in <module>
    candlestick2_ochl(ax = df["Date"],opens=df["Open"],closes=df["Close"],highs =df["High"],lows = df["Low"])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_finance.py", line 552, in candlestick2_ochl
    alpha=alpha)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mpl_finance.py", line 633, in candlestick2_ohlc
    ax.update_datalim(corners)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py", line 3614, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'update_datalim'

(PS: I've tried running the code without df["Date"] = pd.to_datetime(df["Date"]) but it output the same error)

Thank you very much for your help!

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58

1 Answers1

0

from the documentation of candlestick2_ochl:

matplotlib.finance.candlestick2_ohlc(ax, opens, highs, lows, closes,
width=4, colorup='k', colordown='r', alpha=0.75) 

Represent the open, close as a bar line and high low range as a vertical line.

NOTE: this code assumes if any value open, low, high, close is missing they all are missing

Parameters: ax : Axes

an Axes instance to plot to

[...]

So, ax specifies, which plot/subplot your graph will end up on.

in your code, data is assigned to this variable. So, you should create you own ax, and feed that to the function, like so:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

candlestick2_ochl(ax=ax, opens=df["Open"],closes=df["Close"],highs =df["High"],lows = df["Low"])

as for the question with plt.subplot(1,1,1): This means 1x1 grid, first location. You can go to this post for more information on this.

warped
  • 8,947
  • 3
  • 22
  • 49