# Heatmap plots with X and Y matrix instead of vectors

**URL:** <https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [November 2, 2022, 3:29pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665 "2022-11-02T15:29:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![fontainee](https://avatars.discourse-cdn.com/v4/letter/f/bcef8e/32.png) [@fontainee](https://discourse.julialang.org/u/fontainee)\
**Post date:** [November 2, 2022, 3:29pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/1 "2022-11-02T15:29:52Z")

</div>

Hi all,

I want to know if it is possible to make heatmap plots type with X and Y matrix instead of using X and Y vectors. In fact my X and Y matrix are longitude and latitude and the Z matrix are geostationnary satellites radiances where all matrix have same size ; 3712x3712 up to 12000x12000.  
for all matrix their corners are nan (space pixels). I didnot find examples on this website [Home · Plots](https://docs.juliaplots.org/latest/)  
later I will have to draw the cost lines over this image plot.  
I used to use Matlab ( pcolor(X,Y,Z) ) but I have no more a licence for that. I manage to getting close with a scatter plot in scilab but it’s too slow to plot, didnt manage to do that with R and GNUPLOT and I try to avoid python.

Julia sounds great, but before starting and take time to learn deeply Julia I make this little survey if you don’t mind.

Many thanks,

E.F

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 2, 2022, 3:34pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/2 "2022-11-02T15:34:56Z")

</div>

Maybe you are after something like this example where the Z, lon, lat matrices (but with no NaN in coordinates, the horror) are used to create a grid and display it (in this case)

[https://www.generic-mapping-tools.org/RemoteS.jl/dev/gallery/Aqua\_sst/remotes\_L2\_SST/](https://www.generic-mapping-tools.org/RemoteS.jl/dev/gallery/Aqua_sst/remotes_L2_SST/)

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 2, 2022, 3:37pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/3 "2022-11-02T15:37:34Z")

</div>

Can you clarify the content of X and Y?

For example, are all x and y built with matlab’s meshgrid?

If that is the case (a regular grid) using X and Y matrices is bad use of memory, but your could manage by extracting one row and one column of X and Y, no?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 2, 2022, 3:42pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/4 "2022-11-02T15:42:58Z")

</div>

> [@fontainee](#):
>
> I used to use Matlab ( pcolor(X,Y,Z) ) but I have no more a licence for that.

And since you mention it, GMT.jl has a `pcolor` function. See [Pcolor](https://www.generic-mapping-tools.org/GMTjl_doc/examples/images/02_pcolor/#example_5741022823647589386) examples

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2022, 4:35pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/5 "2022-11-02T16:35:52Z")

</div>

> [@fontainee](#):
>
> I used to use Matlab ( pcolor(X,Y,Z) ) but I have no more a licence for that.

[PyPlot.jl](https://github.com/JuliaPy/PyPlot.jl) gives you access to all of the Python Matplotlib functionality in Julia, which includes `pcolor` and many other Matlab-like functions.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 2, 2022, 6:10pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/6 "2022-11-02T18:10:51Z")

</div>

> [@fontainee](#):
>
> if it is possible to make heatmap plots type with X and Y matrix instead of using X and Y vectors.

Would the following example help?

```julia
using Plots
X = [1 2 3; 1 2 3; 1 2 3]
Y = [5 5 5; 6 6 6; 7 7 7]
Z = rand(Int8, 3, 3)
heatmap(X[1,1:end], Y[1:end,1], Z)

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 2, 2022, 6:17pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/7 "2022-11-02T18:17:29Z")

</div>

Yes, this is generically true (always hated the awful memory waste of Matlab functions) but for satellite data where X,Y are longitudes and latitudes, most of times plotting without projection is a … needless _nearly error_ thing to do.

---

<div class="post-metadata">

**Author:** ![fontainee](https://avatars.discourse-cdn.com/v4/letter/f/bcef8e/32.png) [@fontainee](https://discourse.julialang.org/u/fontainee)\
**Post date:** [November 2, 2022, 8:27pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/8 "2022-11-02T20:27:28Z")

</div>

Hi, yes and no.  
Yes this is for MSG or himawari or GOES intead MODIS. But I don’t want it interpolated on a 1km space grid in a first step.  
I still keep the link it still an example to work on.

thanks

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 2, 2022, 8:29pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/9 "2022-11-02T20:29:38Z")

</div>

> [@fontainee](#):
>
> But I don’t want it interpolated on a 1km space grid in a first step.

If you read the functions docs you’ll see that the grid step is a variable. Just set to 1 km for those MODIS L2 scenes.

Edit; in fact, if I remember, it even tries to find a reasonable grid spacing based on coordinates pts distances.

---

<div class="post-metadata">

**Author:** ![fontainee](https://avatars.discourse-cdn.com/v4/letter/f/bcef8e/32.png) [@fontainee](https://discourse.julialang.org/u/fontainee)\
**Post date:** [November 2, 2022, 8:32pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/10 "2022-11-02T20:32:55Z")

</div>

Hi, no it would be more like

X=[0.9 2 2.85 ; 1 2 3 ; 1.1 2 3.2] ;  
Y=[4.9 5 5.05 ; 6 6 6 ; 6.85 7 7.1];

E.F

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 2, 2022, 8:47pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/11 "2022-11-02T20:47:07Z")

</div>

How does Matlab pcolor display the data on such an irregular grid?

NB:  
_the Plots.jl scatter plot below is just to show the irregularity_

 ![Matlab_pcolor_heatmap_irregular_grid](https://global.discourse-cdn.com/julialang/original/3X/2/d/2d358c412c79f919f59d54cc8c845859a1e7cb6c.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2022, 9:15pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/12 "2022-11-02T21:15:53Z")

</div>

> [@rafael.guerra](#):
>
> How does Matlab pcolor display the data on such an irregular grid?

This is what `PyPlot.pcolor` (i.e. the matplotlib `pcolor`) does for `pcolor(X, Y, rand(size(X)...))`:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc755ded036cfa4ed56454219f3b765aeefd6944.jpeg)  
i.e. it just plots irregular quadrilaterals. [`pcolormesh`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.pcolormesh.html) produces virtually the same plot, but is reportedly much faster for large meshes.

(More generally, even for an arbitrary unstructured “cloud” of points, you can do a heatmap plot [via Delaunay triangulation](https://matplotlib.org/3.1.1/gallery/images_contours_and_fields/tricontour_smooth_delaunay.html#sphx-glr-gallery-images-contours-and-fields-tricontour-smooth-delaunay-py) using [`tripcolor`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.tripcolor.html), e.g. `plt.tripcolor(rand(1000), rand(1000), rand(1000))`.)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 2, 2022, 10:15pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/13 "2022-11-02T22:15:23Z")

</div>

For fun (does what is mentioned above) and after warm up

```julia
using GMT

@time contourf(rand(1000,3), show=true)
  1.708162 seconds (1.00 k allocations: 89.234 KiB)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/0/0/0027cb709344645832f947b368b21b6a1587907b.jpeg)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 3, 2022, 12:21am UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/14 "2022-11-03T00:21:54Z")

</div>

> [@stevengj](#):
>
> i.e. it just plots irregular quadrilaterals

Ok, thanks for this.  
Using Plots.jl’s shapes, the same type of heatmap can be produced:

 ![Plots_gr_like_Matlab_pcolor_heatmap_irregular_grid](https://global.discourse-cdn.com/julialang/original/3X/f/2/f25b49553a37c6183650a81f23fffdd569535039.png)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 3, 2022, 12:46am UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/15 "2022-11-03T00:46:50Z")

</div>

Why are there 3x3 _pixels_ and not only 2x2?  
This is what I get with GMT

```julia
X=[0.9 2 2.85 ; 1 2 3 ; 1.1 2 3.2] ;
Y=[4.9 5 5.05 ; 6 6 6 ; 6.85 7 7.1];
pcolor(X, Y, rand(size(X)...), show=1)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/9/e/9efc419ed3c4f3cf4f271db7cfddd97a9ce7f276.png)

I think the difference lies on how the X,Y coordinates are interpreted and this is another case of the [pixel _vs_ grid registration](https://docs.generic-mapping-tools.org/latest/cookbook/options.html#gridline-registration) or the pixel-is-area _vs_ pixel-is-point as it is known in the GDAL world. But while both of the above cases make sense in rectangular grids the same is less evident with these losange _pixels_. How to extrapolate to the corner coordinates?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 3, 2022, 12:49am UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/16 "2022-11-03T00:49:24Z")

</div>

We assume the data points are in the center and construct the quadrilaterals around them.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 3, 2022, 2:06pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/17 "2022-11-03T14:06:47Z")

</div>

Yes, but it requires extrapolation from irregular cells to compute the border coordinates.  
BTW, this is what Matlab produces.

```julia
>> X=[0.9 2 2.85 ; 1 2 3 ; 1.1 2 3.2];
>> Y=[4.9 5 5.05 ; 6 6 6 ; 6.85 7 7.1];
>> pcolor(X, Y, rand(3))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/b/fb980aa024305c6fc99c0a631dc1c1e883df28fa.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 3, 2022, 2:12pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/18 "2022-11-03T14:12:03Z")

</div>

> [@joa-quim](#):
>
> Why are there 3x3 _pixels_ and not only 2x2?

`PyPlot.pcolor` does 2x2 if you give it 3x3 coordinates and a 2x2 array of values:

```julia
pcolor(X, Y, rand((size(X) .- 1)...))

```

gives  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/25b1a0397998dfad974aa5210cf9312562d548eb.png)

This is explained in the [`matplotlib.pyplot.pcolor` documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pcolor.html):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/c/3c9e79ba568eae638a7dc9454410a66956fd7f66.png)

Note that Matlab’s `pcolor` corresponds to `shading="flat"`, and is **silently dropping** some of your data: the colors are given by a 2x2 submatrix of your 3x3 value array! (The [Matlab `pcolor` documentation](https://www.mathworks.com/help/matlab/ref/pcolor.html) gives a 3x3 example and says explicitly that _Four of the nine vertices determine the colors of the faces_.)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 3, 2022, 2:14pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/19 "2022-11-03T14:14:23Z")

</div>

Good to know, thanks.

---

<div class="post-metadata">

**Author:** ![fontainee](https://avatars.discourse-cdn.com/v4/letter/f/bcef8e/32.png) [@fontainee](https://discourse.julialang.org/u/fontainee)\
**Post date:** [November 3, 2022, 3:23pm UTC](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665/20 "2022-11-03T15:23:10Z")

</div>

I must thank all of you, this was really helping. In some ways you’ve bring better solutions than I was using in MATLAB.  
Hence, I will start using Julia the community is great here.  
Last request, what do you advise for developping environnement in order to fully enjoy Julia for data analysis and method developement ? Does Visual Studio code is the only one, not very satisfy about the first try I have done ? or may be notepad++ and Julia console is enough ?  
E.F

[Next page](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665.md?page=2)
