Yotam
Yotam

Reputation: 10685

how to create heat map from irregular xyz data in pyplot?

I have a bunch of xz data sets, I want to create a heat map using these files where the y axis is the parameter that changes between the data sets. The problem is that the x values in each of these data sets is different. Furthermore, the differences between the x values in each of these data sets is not fixed (e.g. x[100] - x[99] =/= x[200]-x[199]). I know I can interpolate the data, generate a grid, and then use imshow to display the data, the question is if there is a more straight forward solution?

Upvotes: 2

Views: 6841

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

You can use a pcolormesh plot. This works fine with a regular (i.e. sorted, rectilinear, but not necessarily equally spaced) grid.

Here is an example:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

x = np.array([1,1.5,3,4,5,5.5])
y = np.array([-1,0,2,3.6,4])
X,Y = np.meshgrid(x,y)
Z = np.random.randint(1,11, size=(len(y), len(x)))

plt.pcolormesh(X,Y,Z)
plt.plot(X[1,2],Y[1,2] , marker="o", color="crimson")
plt.text(X[1,2],Y[1,2], str(Z[1,2]), color="crimson", fontsize=20)
plt.colorbar()
plt.show()

enter image description here

Note that the value in Z[i,j] is plotted at in the cell ranging from position X[i,j],Y[i,j] to X[i+1,j+1],Y[i+1,j+1]. So the grid points are the cell edges.
This also implies that if X,Y,Z have the same shape, the last row and column of Z is not plotted. You may however provide a grid which is one larger in both dimentsions than the value array Z.

In order to investigate the different plots for different parameters, you may use a technique like the one I proposed in this answer: Paging/scrolling through set of 2D heat maps in matplotlib.

Upvotes: 1

Related Questions