Plotting 2 Variables With A Heat Map
Solution 1:
If you start with 2 mono-dimensional vectors, x
and y
to compute a function of x
and y
on a grid of 2D points you have first to generate said 2D grid, using numpy.meshgrid
.
from numpy import linspace, meshgrid
x, y = linspace(1, 5, 41), linspace(0.03, 0.70, 68)
X, Y = meshgrid(x, y)
It's somehow a surprise to see that you get two grids... one contains only the x
values and the other only the y
values (you have just to print X
and Y
to see for yourself).
With these two grids available, you can compute a grid of results, but this works only when you use numpy
's ufuncs
... e.g.
def f(x, y):
from numpy import sin, sqrt
return sin(sqrt(x*x+y*y))
and eventually you can plot your results, the pyplot
method that you want to use is pcolor
, and to add a colorbar you have to use, always from pyplot
, the colorbar
method...
from matplotlib.pyplot import colorbar, pcolor, show
Z = f(X, Y)
pcolor(X, Y, Z)
colorbar()
show()
When this point is eventually reached, tradition asks for a sample output That's all
Solution 2:
Lets say z = f(x, y)
. Your heatmap's key is able to represent one dimension only (usually that would be z). What do you mean by
represent the method f(x,y)
For 2-dimensional plotting, you would need a 3-dimensional heatmap.
Post a Comment for "Plotting 2 Variables With A Heat Map"