0

I would like to do a heatmap plot using three independent vectors x, y and z. I have looked at examples over the internet and most of them show how to do heatmap plot for x, y and z represented as a 2D matrix

So, can someone please help me on how can I convert 3 independent vectors to a 2d matrix, which I can eventually use for doing heatmap plots

One thing that I thought was to create matrix by first discretizing and arranging in ascending order x and y, and finding z at the new "x" and "y" combinations. But, there could be cases in which "z" cannot be computed due to lack of data during interpolation

I am bit confused, and I would like to seek help in this regard

Masoom Kumar
  • 129
  • 1
  • 8
  • 1
    Check https://stackoverflow.com/questions/18764814/make-contour-of-scatter/43407498#43407498 – ImportanceOfBeingErnest Jan 08 '20 at 16:27
  • I’m voting to close this, it’s far too broad and vague. OP, you probably need a guide/tutorial, or the documentation. – AMC Jan 08 '20 at 17:19
  • @ImportanceOfBeingErnest : Thanks for the link. I took some valuable stuff from it. I have now managed to create an interpolated matrix "z" as a function of "x" and "y" using griddata, and meshgrid. The contour plot works fine, but I still have to do the heatmap Now, i tried to use the seaborn package. The command was quite simple sns.heatmap(yourmatrix). But the problem is that the heatmap created comes has index from 1 to n (where n is the number of elements) on the axis instead of "x" and "y" values. Due to this the heatmap does not make sense. Any workarounds ? – Masoom Kumar Jan 08 '20 at 20:37
  • Do not use seaborn. matplotlib's `imshow` or `pcolormesh` are fine. – ImportanceOfBeingErnest Jan 08 '20 at 20:41
  • @ImportanceOfBeingErnest : Thanks a lot for the tip. I tried the pcolormesh, like this - plt.pcolormesh(zi). But do you know how can I enter information about "x" and "y", because without it the heatmap would not make any sense as it is plotted against axes which are numericals from 1 until the maximum number of values in "x" and "y" – Masoom Kumar Jan 08 '20 at 20:50
  • @ImportanceOfBeingErnest : Ok, nice it happened. I just entered plt.pcolormesh(xi, yi, zi). Looks like my initial requirements are satisfied – Masoom Kumar Jan 08 '20 at 20:52

2 Answers2

0

Have a look at pcolormesh. It does what you need: create a heat map of data that do not lie on a regular grid. You can specify how the data are interpolated (and extrapolated).

From the docs:

matplotlib.pyplot.pcolormesh(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, shading='flat', antialiased=False, data=None, **kwargs)

Create a pseudocolor plot with a non-regular rectangular grid.

Call signature:

pcolor([X, Y,] C, **kwargs)

X and Y can be used to specify the corners of the quadrilaterals.

Community
  • 1
  • 1
polwel
  • 597
  • 5
  • 17
0

Thank you group memebers. With your help, I have been able to reach closer to the solution. The thing that I did was

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

x = mdf_merged.get('VariableX').samples
y = mdf_merged.get('VariableY').samples
z = mdf_merged.get('VariableZ').samples
###
xi = np.linspace(min(x),max(x),10)
yi = np.linspace(min(y),max(y),20)
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear')
plt.pcolormesh(xi, yi, zi)
Masoom Kumar
  • 129
  • 1
  • 8
  • 1
    The point of `pcolormesh` is that it can deal with non-gridded data. If you are interpolating anyway, use `imshow`. – polwel Jan 09 '20 at 09:38