# PColormesh equivalent in Makie

**URL:** <https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075>\
**Category:** Visualization\
**Tags:** makie, cairomakie\
**Created:** [September 20, 2023, 6:19pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075 "2023-09-20T18:19:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dchang10](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dchang10/32/44162_2.png) [@dchang10](https://discourse.julialang.org/u/dchang10)\
**Post date:** [September 20, 2023, 6:19pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/1 "2023-09-20T18:19:27Z")

</div>

I’m looking for a Makie equivalent to the matplotlib `pcolormesh`. I attempted to do this with the `mesh` plotting function in CairoMakie.

I’m testing my code by trying to reproduce this plot from the `pcolormesh` [documentation](https://matplotlib.org/stable/gallery/images_contours_and_fields/pcolormesh_levels.html#sphx-glr-gallery-images-contours-and-fields-pcolormesh-levels-py).

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

Here is my attempt:

```julia
using GeometryBasics, CairoMakie
x = hcat([range(-0.5, 10, length=10) for i in 1:6]...)'
y = hcat([collect(range(4.5, 11, length=6)) for i in 1:10]...)
X2d = x.+0.2.*y
Y2d = y +0.3.*x
Z2d = hcat([[0.7003673 , 0.74275081, 0.70928001, 0.56674552, 0.97778533,
0.70633485, 0.24791576, 0.15788335, 0.69769852, 0.71995667],
[0.25774443, 0.34154678, 0.96876117, 0.6945071 , 0.46638326,
0.7028127 , 0.51178587, 0.92874137, 0.7397693 , 0.62243903],
[0.65154547, 0.39680761, 0.54323939, 0.79989953, 0.72154473,
0.29536398, 0.16094588, 0.20612551, 0.13432539, 0.48060502],
[0.34252181, 0.36296929, 0.97291764, 0.11094361, 0.38826409,
0.78306588, 0.97289726, 0.48320961, 0.33642111, 0.56741904],
[0.04794151, 0.38893703, 0.90630365, 0.16101821, 0.74362113,
0.63297416, 0.32418002, 0.92237653, 0.23722644, 0.82394557],
[0.75060714, 0.11378445, 0.84536125, 0.92393213, 0.22083679,
0.93305388, 0.48899874, 0.47471864, 0.08916747, 0.22994818]]...)'

data = vec([(Float32(xv), Float32(yv)) for (xv,yv) in zip(vec(X2d), vec(Y2d))])
heights = [i for i in vec(Z2d)]
faces = decompose(QuadFace{GLIndex}, Tesselation(Rect(0, 0, 1, 1), size(X2d)))

msh = GeometryBasics.Mesh(GeometryBasics.Point2f.(data),faces)
fig = CairoMakie.Figure();
ax = Axis(fig[1,1], aspect=1)

CairoMakie.mesh!(ax, msh, color=heights)
display(fig)

```

This code produces an image that looks like this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/3/23ecaccca86d738fa6cf2ddcf953af2f79e6d06a.jpeg)

Which is very close to the correct answer except that it seems like the the mesh interpolates the vertex colors. Is there a way to stop that interpolation and to just have a flat color shading on each `QuadFace`?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2023, 7:07pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/2 "2023-09-20T19:07:16Z")

</div>

You can do this easily if you are using Meshes.jl. We provide a Makie recipe that assigns colors to the quadrangle geometries of a grid (or general mesh), or to the vertices:

```julia
using Meshes
using Rotations

import GLMakie as Mke

grid = CartesianGrid(10, 10) |> Rotate(Angle2d(pi/4))

Mke.plot(grid, color = 1:nelements(grid))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/0/60004139278461e80671101e76d49f6a304df502.png)

```julia
Mke.plot(grid, color = 1:nvertices(grid))

```

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

Others can share a pure Makie.jl solution with the built-in `heatmap`, and `image` and `mesh` recipes.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 20, 2023, 8:35pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/3 "2023-09-20T20:35:27Z")

</div>

You need four separate vertices for each face which all have the same color. The shared vertices of neighboring faces need to be duplicated because they carry a different color for each face.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 20, 2023, 8:40pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/4 "2023-09-20T20:40:18Z")

</div>

Exactly. The recipe in Meshes.jl duplicates vertices in polygons to assign the same color to “disconnected” geometries. It works with triangles, quadrangles, or n-gons in general.

---

<div class="post-metadata">

**Author:** ![dchang10](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dchang10/32/44162_2.png) [@dchang10](https://discourse.julialang.org/u/dchang10)\
**Post date:** [September 20, 2023, 9:09pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/6 "2023-09-20T21:09:08Z")

</div>

Success. Thanks so much. Here’s the Meshes, and Makie code I used to reproduce the `pcolormesh` plot.

```julia
using Meshes
using Rotations
import CairoMakie as CMke

x = hcat([range(-0.5, 10, length=11) for i in 1:7]...)'
y = hcat([collect(range(4.5, 11, length=7)) for i in 1:11]...)
X2d = x.+0.2.*y
Y2d = y +0.3.*x
Z2d = hcat([[0.7003673 , 0.74275081, 0.70928001, 0.56674552, 0.97778533,
0.70633485, 0.24791576, 0.15788335, 0.69769852, 0.71995667],
[0.25774443, 0.34154678, 0.96876117, 0.6945071 , 0.46638326,
0.7028127 , 0.51178587, 0.92874137, 0.7397693 , 0.62243903],
[0.65154547, 0.39680761, 0.54323939, 0.79989953, 0.72154473,
0.29536398, 0.16094588, 0.20612551, 0.13432539, 0.48060502],
[0.34252181, 0.36296929, 0.97291764, 0.11094361, 0.38826409,
0.78306588, 0.97289726, 0.48320961, 0.33642111, 0.56741904],
[0.04794151, 0.38893703, 0.90630365, 0.16101821, 0.74362113,
0.63297416, 0.32418002, 0.92237653, 0.23722644, 0.82394557],
[0.75060714, 0.11378445, 0.84536125, 0.92393213, 0.22083679,
0.93305388, 0.48899874, 0.47471864, 0.08916747, 0.22994818]]...)'

data = vec([(Float32(xv), Float32(yv)) for (xv,yv) in zip(vec(X2d), vec(Y2d))])
Z = collect(vec(reshape(Z2d, (1,60))))

grid= SimpleMesh(data, GridTopology(6,10))
CMke.plot(grid, color = Z)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/0/2034579782f5c0ac1ce17590a43f4706445c3256.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:** [September 20, 2023, 10:21pm UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/7 "2023-09-20T22:21:55Z")

</div>

Another one. I a mix of the above and this [non-regular example](https://www.generic-mapping-tools.org/GMTjl_doc/examples/images/02_pcolor/#non-rectangular_grid)

```julia
using GMT

X,Y = GMT.meshgrid(-3:6/17:3);
XX = X .+ 0.2 * Y;
YY = Y .+ 0.3 * X;

pcolor(XX,YY, reshape(repeat([1:18; 18:-1:1], 9,1), size(XX)), lc=:black, show=true)

```

 ![GMTjl_j](https://global.discourse-cdn.com/julialang/original/3X/4/d/4de7a7d1379364d56a343fc387d6f8ba543e9b30.png)

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [March 25, 2024, 4:01am UTC](https://discourse.julialang.org/t/pcolormesh-equivalent-in-makie/104075/8 "2024-03-25T04:01:16Z")

</div>

Please reply to [Python pcolormesh() alternative in Julia](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885)
