Skip to content Skip to sidebar Skip to footer

Matplotlib Candlestick Chart Error Xy = (t-offset, Lower), Typeerror: Unsupported Operand Type(s) For -: 'datetime.date' And 'float'

I am trying to plot a candlestick chart using matplotplib. I have queried my database, returned the relevant data and appended in to an anrray named candleAr, in the required forma

Solution 1:

Matplotlib uses floating point numbers representing ordinals (counting days from 0001-01-01), with decimals representing fractions of a day.

This is documented in the candlestick docstring:

time must be in float days format - see date2num

The matplotlib.dates module gives you tools to convert datetime objects to such numbers:

>>>from datetime import date>>>from matplotlib.dates import date2num>>>date2num(date(2013, 3, 1))
734928.0

For your code that looks like:

from matplotlib.dates importdate2numappendLine= date2num(line[1]), line[3], line[6], line[5], line[4]

For datetime.date() this basically comes down to datetime.date.toordinal() as a float instead of an integer.

Post a Comment for "Matplotlib Candlestick Chart Error Xy = (t-offset, Lower), Typeerror: Unsupported Operand Type(s) For -: 'datetime.date' And 'float'"