2

I have a data visualisation-based question. I basically want to create a heatmap from a pandas DataFrame, where I have the x,y coordinates and the corresponding z value. The data can be created with the following code -

data = ([[0.2,0.2,24],[0.2,0.6,8],[0.2,2.4,26],[0.28,0.2,28],[0.28,0.6,48],[0.28,2.4,55],[0.36,0.2,34],[0.36,0.6,46],[0.36,2.4,55]])
data=np.array(data)
df=pd.DataFrame(data,columns=['X','Y','Z'])

Please note that I have converted an array into a DataFrame just so that I can give an example of an array. My actual data set is quite large and I import into python as a DataFrame. After processing the DataFrame, I have it available as the format given above.

I have seen the other questions based on the same problem, but they do not seem to be working for my particular problem. Or maybe I am not applying them correctly. I want my results to be similar to what is given here https://plot.ly/python/v3/ipython-notebooks/cufflinks/#heatmaps

Any help would be welcome.

Thank you!

  • https://stackoverflow.com/questions/33282368/plotting-a-2d-heatmap-with-matplotlib – Seb Nov 17 '19 at 14:38

2 Answers2

2

Found one way of doing this -

Using Seaborn.

import seaborn as sns
data = ([[0.2,0.2,24],[0.2,0.6,8],[0.2,2.4,26],[0.28,0.2,28],[0.28,0.6,48],[0.28,2.4,55],[0.36,0.2,34],[0.36,0.6,46],[0.36,2.4,55]])
data=np.array(data)
df=pd.DataFrame(data,columns=['X','Y','Z'])
df=df.pivot('X','Y','Z')
diplay_df = sns.heatmap(df)

Returns the following image -

Heatmap

sorry for creating another question.

Also, thank you for the link to a related post.

  • 1
    This is greatly appreciated. I have been looking for a way to plot `x,y,z` data with such a simple format. Thank you! – double0darbo Sep 02 '21 at 17:55
0

How about using plotnine, A Grammar of Graphics for Python

data

data = ([[0.2,0.2,24],[0.2,0.6,8],[0.2,2.4,26],[0.28,0.2,28],[0.28,0.6,48],[0.28,2.4,55],[0.36,0.2,34],[0.36,0.6,46],[0.36,2.4,55]])
data=np.array(data)
df=pd.DataFrame(data,columns=['X','Y','Z'])

Prepare data

df['rows'] = ['row' + str(n) for n in range(0,len(df.index))]
dfMelt = pd.melt(df, id_vars = 'rows')

Make heatmap

ggplot(dfMelt, aes('variable', 'rows', fill='value')) + \
        geom_tile(aes(width=1, height=1)) + \
        theme(axis_ticks=element_blank(),
              panel_background = element_blank()) + \
        labs(x = '', y = '', fill = '')

heatmap with plotnine

Alfonso
  • 644
  • 7
  • 17
  • Hello. I don't think that does the job, as the data in the columns X, Y and Z are precisely what they are supposed to be. I actually ended up solving it using seaborn. – Somsubhro Chaudhuri Nov 17 '19 at 15:07