Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three variables as heatmap

I want to plot my data as a heatmap which has the following structure:

X = [1,1,1,1,1,1,1,1,1,1], Y = [1,2,3,4,5,6,7,8,9,10] Z = [0.2, 0.33, 0.1, 0.25, 0.0, 0.9, 0.75, 0.88, 0.44, 0.95]

The x and y-axis shall be represented by X and Y, while the 'heat' is represented by the values of Z.

E.g. at coordinate (x,y) = (1,2) the intensity shall be 0.33 How can this be achieved by using matplotlib? Looking at posts which relate to the keyword heatmap or even to those related to the term contour map, I could not transfer it to this problem yet.

Thank you in advance for any hints Dan

like image 679
Daniyal Avatar asked Aug 19 '16 14:08

Daniyal


People also ask

How many variables are in a heatmap?

Heatmaps are used to show relationships between two variables, one plotted on each axis.

Can heatmap be used for categorical data?

If we want to see how categorical variables interact with each other, heatmaps are a very useful way to do so. While you can use a heatmap to visualize the relationship between any two categorical variables, it's quite common to use heatmaps across dimensions of time.

What can be used instead of heatmap?

Unlike alternative heatmap software providers like FullStory, Hotjar, or Amplitude, Smartlook allows users to retroactively define events, funnels, and heatmaps — providing better real-time business intelligence.


1 Answers

I hope your data is just an example because it will look funny (it's more a sequence of strips; the x-dimension is constant).

I would recommend the usage of pandas (general data-analysis) and seaborn (matplotlib-extensions) which makes it a bit nicer.

Code

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
X = [1,1,1,1,1,1,1,1,1,1]
Y = [1,2,3,4,5,6,7,8,9,10]
Z = [0.2, 0.33, 0.1, 0.25, 0.0, 0.9, 0.75, 0.88, 0.44, 0.95]
data = pd.DataFrame({'X': X, 'Y': Y, 'Z': Z})
data_pivoted = data.pivot("X", "Y", "Z")
ax = sns.heatmap(data_pivoted)
plt.show()

Output

enter image description here

like image 101
sascha Avatar answered Sep 23 '22 15:09

sascha