-1

enter image description here

I'm struggling with plot of time from dataframe. After many tries and almost every error, I just ploted ticks with set_xtickslabels, but date didn't match to chart...showed next items from the list on every printed tick. And also when pointed on chart date was not in correct format. This wasn't supprise, because ploted this with float. Through all those readings on stack forum I get to that date is an object, and tried to format it in many ways, but still didn't get to correct solution. So how to do it correctly or where to look for solution?

This is history(for sure not all of them) of my strugglings and errors that I got:

def getCandles(df, ax):
    ohlc = df[['open_time','o', 'h', 'l', 'c', 'v']].copy()
    ohlc['open_time'] = ohlc['open_time'].values.astype(float)
    ohlc['o'] = ohlc['o'].astype(float)
    ohlc['h'] = ohlc['h'].astype(float)
    ohlc['l'] = ohlc['l'].astype(float)
    ohlc['c'] = ohlc['c'].astype(float)
    ohlc['v'] = ohlc['v'].astype(float)
    candlestick_ohlc(ax, ohlc.values, width = 0.8, colorup="#77d879", colordown="#db3f3f")

    plt.xlabel('TIME')
    plt.ylabel('PRICE')
    plt.tight_layout() 
    plt.show()

EDIT: I found a solution here: https://stackoverflow.com/a/41822115/6635518

Thanks All for help.

Full.Of.Life
  • 169
  • 2
  • 10
  • What is your time variable? – Juan C Mar 21 '19 at 21:21
  • Your code is kinda messy. Could you try to organize it better and just leave what is essential to it? – Juan C Mar 21 '19 at 21:23
  • Done. I've pasted all those ##lines to show my attempts, which was doing with stack forum examples. I just want to print that chart with proper format of date. I would like to print with ohlc['open_time'] as x. – Full.Of.Life Mar 21 '19 at 21:56
  • Can you provide an example of the data in 'open_time'? – Tolio Mar 21 '19 at 22:17
  • before any format : ['open_time','o', 'h', 'l', 'c', 'v'] = [[1551387600000 '0.00002980' '0.00002990' '0.00002950' '0.00002970' '281493.43000000'] also: open_time int64 o object h object l object c object v object dtype: object – Full.Of.Life Mar 21 '19 at 22:23
  • I think there are a lot of questions/answers around which show that you need to convert the dates to the matplotlib datetime units of days since 01-01-0001 plus 1. So you need to convert `Timestamp -> datetime -> matplotlib dateformat`. See [matplotlib.dates](https://matplotlib.org/api/dates_api.html). – ImportanceOfBeingErnest Mar 22 '19 at 00:23
  • Thx. Will follow that. – Full.Of.Life Mar 22 '19 at 19:16

1 Answers1

0

You are converting everything to float, including open_time.

Since your date format is the epoch in milliseconds, the code below converts epoch to a datetime string.

    # Datetime from epoch in milliseconds
    ohlc['open_time'] = pd.to_datetime(ohlc['open_time'], unit='ms')

Example enter image description here

Check the docs to match the formatting of your dates.

Tolio
  • 1,023
  • 13
  • 30
  • It's working if I want to plot with separate columns. But with candlestick_ohlc from mpl_finance showing error: "TypeError: unsupported operand type(s) for -: 'Timestamp' and 'float'". Thx for the effort. – Full.Of.Life Mar 25 '19 at 01:00