I'm creating a GUI where there is live 'point in time' data for several records ("things") and fields. records are comparable based on field, but fields are not necessarily related (at least not on same scale). For my eventual GUI, I want to have the main page be a heatmap (really a bunch of 1-D heatmaps based on columns (fields), then if you click on one it will give a time series history and some other charts.
Anyway, what I'm after here, is trying to get the initial heatmap to show the way I want. As of now I can get the pcolormesh from Matplotlib to essentially show individual 1-D heatmaps based on field by hacking it and heatmapping based on percentile of column, and then adding text of the actual value on top.
But, as I said, fields are not necessarily related, and I would like to be able to have individual colormaps for each field. For example: say Fields 3 and 4 are qualitatively related to one another, but not to Fields 0-3 - so it would be nice to have those mapped to say the 'greens' colormesh rather than 'coolwarm'.
Here is my code so far and resulting pcolormesh/heatmap:
import pandas as pd
import matplotlib.pyplot as plt
def DFPercentiles(df,bycols=True):
p=pd.DataFrame(index=df.index,columns=df.columns)
if bycols!=True:
for j in df.index:
for i in df.columns:
p.loc[j,i]=(df.loc[j,i]-min(df.loc[j,:]))/(max(df.loc[j,:])-min(df.loc[j,:]))
else:
for i in df.index:
for j in df.columns:
p.loc[i,j]=(df.loc[i,j]-min(df.loc[:,j]))/(max(df.loc[:,j])-min(df.loc[:,j]))
return p
def Heatmap(df,figsize='auto'):
if figsize=='auto':
figsize=[shape(df)[1],shape(df)[0]/2]
fig=figure(figsize=figsize)
pdf=array(DFPercentiles(df,bycols=True)).astype(float)[::-1]
plt.pcolormesh(pdf,cmap=cm.coolwarm,alpha=0.8)
plt.yticks(arange(0.5,len(df)),df.index[::-1])
plt.xticks(arange(0.5,len(df.columns)),df.columns)
for y in range(df.shape[0]):
for x in range(df.shape[1]):
plt.text(x + 0.5, y + 0.5, '%.3f' % df[::-1].iloc[y, x],
horizontalalignment='center',
verticalalignment='center',
)
return plt
hmap=Heatmap(mydf)
hmap.show()
And the result:
I've had no luck trying to get multiple colormaps for separate fields.
See Question&Answers more detail:
os