2

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?

Yotam
  • 10,295
  • 30
  • 88
  • 128
  • "heatmap" can be a histogram, 2D with square cells, or hexbin. You seem to be describing a surface contour/colormap – f5r5e5d Apr 08 '17 at 06:28
  • @f5r5e5d I'm sorry, I didn't understand what you mean. Maybe heatmap is the wrong word here. I want to have a surface colored based on the ``z`` values. – Yotam Apr 08 '17 at 06:31
  • Try [`plot_trisurf`](http://matplotlib.org/examples/mplot3d/trisurf3d_demo2.html), like in [this example](http://stackoverflow.com/a/43073311/3026320) – berna1111 Apr 08 '17 at 10:25
  • First, you should be very precise about if you intend to have a 2D or a 3D plot. The question then is not so much whether the spacings between points along one dimension is equal or not, but rather how exactly your data is organized (e.g. do you have 2 1D arrays `x` and `z` and a 2D array `xz` or are all of them 1D or 2D? In the case they re 1D, what would be the mapping to 2D? You then need to be very specific about the kind of plot you want to obtain. – ImportanceOfBeingErnest Apr 08 '17 at 12:32
  • The data sets are all in 2D, and the plot needs to be in 2D. – Yotam Apr 08 '17 at 13:40

1 Answers1

2

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.

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712