Charting Candlestick_ohlc One Minute Bars With Pandas And Matplotlib
Given the following example of Pandas dataframe date open high low close volume 0 2015-03-13 08:00:00 71.602 71.637 71.427 71.539 0.00024
Solution 1:
Note: matplotlib.finance
was taken out of mpl and moved into its own module. mplfinance
can now be found here.
You need convert dates to mdates.date2num
, because
time must be in float days format - see date2num
Then I try implement this solution:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
#if necessary convert to datetime
df.date = pd.to_datetime(df.date)
df = df[['date', 'open', 'high', 'low', 'close', 'volume']]
df["date"] = df["date"].apply(mdates.date2num)
f1 = plt.subplot2grid((6, 4), (1, 0), rowspan=6, colspan=4, axisbg='#07000d')
candlestick_ohlc(f1, df.values, width=.6, colorup='#53c156', colordown='#ff1717')
f1.xaxis_date()
f1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))
plt.xticks(rotation=45)
plt.ylabel('Stock Price')
plt.xlabel('Date Hours:Minutes')
plt.show()
Solution 2:
As an update to Jazrael's answer, mplfinance has a new API that handles the matplotlib work for you. Now you can just call:
import pandas as pd
import mplfinance as mpf
daily = pd.read_csv('examples/data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
daily.index.name = 'Date'
mpf.plot(daily, type='candle')
You also don't have to worry about the date data type conversion mentioned by Jazrael.
Solution 3:
It's hack is to just simply reduce your candlestick width. Like for 15 min chart width=0.01
Post a Comment for "Charting Candlestick_ohlc One Minute Bars With Pandas And Matplotlib"