I am trying to plot a candlestick chart with matplotlib. I just want to show hours and minutes in x axis, but my code does not work. My input list is like this: ['17:00',355.750,567.05,147.00,146.99]
.
My code:
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
import matplotlib.dates as mpl_dates
from mplfinance.original_flavor import candlestick_ohlc
data = np.array([date2num(datetime.datetime.strptime("17:00", "%H:%M")),355.750,567.05,147.00,146.99])
data = data.reshape(1,5)
data = pd.DataFrame(data, columns =['Date', 'High', 'Low', 'Open', 'Close'])
ohlc = data.loc[:, ['Date', 'Open', 'High', 'Low', 'Close']]
fig, ax = plt.subplots()
candlestick_ohlc(ax, ohlc.values, width=0.01, colorup='green', colordown='red', alpha=0.8)
date_format = mpl_dates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()
fig.tight_layout()
plt.show()
I would be grateful if you could help me to plot the chart . Many thanks.
CodePudding user response:
Your code seems to work. Hours and minutes in x axis are correctly displayed.