Reputation: 16817
I have some data which is arranged as a 2-dimension numpy array.
The data has 4 columns, which correspond to the statistics:
I want to plot these with a candle plot. As far as I know the only way to do this with matplotlib is with the mplfinance
package.
I have converted my data to a pandas dataframe using the following:
data_frame = pandas.DataFrame(data_in, columns=['Low', 'High', 'Open', 'Close'])
and tried to plot using
mplfinance.plot(data_frame, type='candle')
however this error resulted:
raise TypeError('Expect data.index as DatetimeIndex')
So I guess that I need to have some kind of column containing date/time data.
I have not used this package before so I am unsure of what I am doing here.
I could potentially add date/timestamps to my input data, although this is somewhat inconvenient and would require that I change a lot of other scripts.
The data is sourced from some sensors which are sampled at (approximately) fixed time intervals. (The exact time of when the samples are obtained is not particularly important.)
Therefore I have 4 columns of data to plot, and the data are sequential.
(Note some pre-processing was done to compute the min, max, mean, sd, etc. Again, this is not of critical importance and essentially makes the time information irrelevant.)
Is there a way to plot the candle plot by just specifying that the data are "in order" and that the x-axis should be something like "from 1 to N" where N is the number of samples in the data.
Upvotes: 1
Views: 1307
Reputation: 7714
There are a couple of things that you could try:
matplotlib boxplot ... not candlesticks, but similar.
use mplfinance as you are doing, and generate an arbitrary datetime index starting at January 1st, and use %j
(day of year) datetime formatting:
data_frame = pandas.DataFrame(data_in, columns=['Low', 'High', 'Open', 'Close'])
data_frame.index = pandas.date_range('1/1/2021',periods=(len(data_frame))
mplfinance.plot(data_frame, type='candle', datetime_format='%j')
hope that helps. full disclosure: I am the maintainer of the mplfinance library.
Upvotes: 1