7

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.000249
1    2015-03-13 08:01:00  71.541  71.563  71.461  71.501  0.000215
2    2015-03-13 08:02:00  71.521  71.537  71.504  71.533  0.000048
3    2015-03-13 08:03:00  71.530  71.530  71.510  71.524  0.000016
4    2015-03-13 08:04:00  71.504  71.578  71.504  71.515  0.000045
5    2015-03-13 08:05:00  71.524  71.581  71.522  71.538  0.000062
6    2015-03-13 08:06:00  71.562  71.621  71.542  71.550  0.000095
7    2015-03-13 08:07:00  71.555  71.576  71.544  71.565  0.000051
8    2015-03-13 08:08:00  71.555  71.566  71.554  71.565  0.000023
9    2015-03-13 08:09:00  71.564  71.564  71.502  71.504  0.000017
10   2015-03-13 08:10:00  71.508  71.549  71.486  71.516  0.000097
11   2015-03-13 08:11:00  71.521  71.523  71.443  71.447  0.000103
12   2015-03-13 08:12:00  71.451  71.496  71.444  71.480  0.000206
13   2015-03-13 08:13:00  71.473  71.485  71.389  71.418  0.000147
14   2015-03-13 08:14:00  71.424  71.442  71.394  71.398  0.000107
15   2015-03-13 08:15:00  71.393  71.415  71.350  71.356  0.000141
16   2015-03-13 08:16:00  71.377  71.463  71.366  71.436  0.000142
17   2015-03-13 08:17:00  71.428  71.467  71.391  71.440  0.000091
18   2015-03-13 08:18:00  71.357  71.450  71.353  71.420  0.000147
19   2015-03-13 08:19:00  71.420  71.476  71.415  71.439  0.000062
20   2015-03-13 08:20:00  71.443  71.471  71.403  71.435  0.000196
21   2015-03-13 08:21:00  71.442  71.475  71.425  71.469  0.000032

How to plot one minute candlestick OHLC bars showing the minute timeframe on the xaxis?

I tried this but it doesn't work

df = df[['date', 'open', 'high', 'low', 'close', 'volume']]
df = df.reset_index()
f1 = plt.subplot2grid((6, 4), (1, 0), rowspan=6, colspan=4, axisbg='#07000d')
f1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))
candlestick_ohlc(f1, df.values, width=.6, colorup='#53c156', colordown='#ff1717')
plt.ylabel('Stock Price')
plt.xlabel('Date Hours:Minutes')
plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
RaduS
  • 2,465
  • 9
  • 44
  • 65

3 Answers3

16

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()
user1032677
  • 363
  • 2
  • 11
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Could you tell me where is this `candlestick_ohlc` function coming from? Do I have to import some module before calling this function? – StayFoolish Apr 25 '17 at 12:39
  • 1
    Use `from matplotlib.finance import candlestick_ohlc` – jezrael Apr 25 '17 at 12:40
  • I add all possible modules, please check it. – jezrael Apr 25 '17 at 12:41
  • 1
    jezrael's solution is brilliant! However, making candles' width dynamic is better, or the graph will look very ugly when plotting certain amount of candles, for example, use `width=1/len(df)*0.6`. – j0e1in Dec 23 '17 at 04:28
  • 1
    It seems `len(df)` is not the critical factor - the time between rows is! If your candlesticks are still ugly, try `width=0.6/(24*60)` for minute by minute data. For full details: https://stackoverflow.com/questions/48081854/matplotlib-candlestick-in-minutes/48083085#48083085 – Heath Raftery Jan 03 '18 at 18:29
4

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.

enter image description here

MichaelG
  • 652
  • 1
  • 10
  • 17
  • nice ! but mplfinance seems to expect panda dataframe... and I didn't plan to pull panda on my project... – yota May 24 '21 at 17:01
1

It's hack is to just simply reduce your candlestick width. Like for 15 min chart width=0.01