Market Profile Chart using Python!

Continuing from the earlier code  where I created the profile data using ‘Alphabets, here let me try to plot the chart. This one turned out bit tricky and used an example snippet from Matplotlib for stacked bar chart!

Logic is simple, instead of Alphabets, have an a number for each occurrence, use Matplotlib stacked bar chart to plot the colors.

mi=defaultdict(list)
TGroups=df.groupby([pd.Grouper(key='DateTime', freq=frequency)])
#iterate over each group and add to dictionary, 
#dictonary keys are 'High' and 'Low' of each group, 
#rounded and values are char A, B incremented for each period(freq group)
#default 30 min since we grouped based on freq, 
#for each group increment the char i.e +1
min_price=np.round(df.min().Low)
max_price=np.round(df.max().High)
for t,g in TGroups:
    g_min_price=np.round(g.Low.min())
    g_max_price=np.round(g.High.max())
    for price in range(int(min_price), int(max_price+1)):
        if (price > g_max_price) or (price < g_min_price):
            mi[price].append(0)
        else:
            mi[price].append(0.1)

Here is the code using Matplotlib

import seaborn as sns
%matplotlib inline
m=np.asmatrix(np.stack(mi.values()))
data=np.asarray(m.transpose())
start = min(mi.keys())
ind = np.arange(start,start+data.shape[1])
left = np.vstack((np.zeros((data.shape[1],), dtype=data.dtype),
np.cumsum(data, axis=0)[:-1]))
#colors = ('#ff3333', '#33ff33', '#3333ff', '#33ffff')
colors = sns.color_palette("Blues_d", n_colors=data.shape[0])
fig, ax = plot.subplots()
ax.set_xlim(0,2)
plot.xticks([], [])
fig.set_size_inches(10,8)
for dat, col, left in zip(data, colors, left):
   plot.barh(ind, dat, color=col, left=left)

Here is the chart using Matplotlib