# Python pcolormesh() alternative in Julia

**URL:** <https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885>\
**Category:** Visualization\
**Tags:** plotting, glmakie\
**Created:** [March 20, 2024, 3:47pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885 "2024-03-20T15:47:50Z")\
**Posts on this page:** 16\
**Page:** 1

<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 20, 2024, 3:47pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/1 "2024-03-20T15:47:50Z")

</div>

I want to get equivalent plot in Julia that i have using Python [pcolormesh()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pcolormesh.html) function. Here `x` and `z` are two `270x100` matrices. You can download folder containing that Python script and data at [VelocPythJul.tar.gz - Google Drive](https://drive.google.com/file/d/1IHs9rkQI8hBm77NxzexTePWla5R7iqGW/view?usp=sharing)

```julia
density = ax.pcolormesh(x, z, rho[t]*v_p[t], norm=LogNorm(), cmap=colormap)`

```

> **Python script is this**
>
> ```julia
> # Create the plot
> fig = plt.figure(figsize=(20, 20))
> ax = plt.subplot(111)
> #colormap = custom_colormap('cmap.csv', reverse=False)
> colormap = 'jet'
> 
> ax.set_xlim((0, x_lim))
> ax.set_ylim((0, y_lim))
> ax.set_aspect('equal',adjustable='box')
> 
> vmin=1.e-8
> vmax=1.e-2
> density = ax.pcolormesh(x, z, rho[0]*v_p[0], cmap=colormap, 
> norm=plt.Normalize(vmin=np.log10(vmin), vmax=np.log10(vmax)))#, cmap=colormap, vmin=np.log10(vmin), vmax = np.log10(vmax))
> aspect = 40
> pad_fraction = 0.5
> divider = make_axes_locatable(ax)
> width = axes_size.AxesY(ax, aspect=1./aspect)
> pad = axes_size.Fraction(pad_fraction, width)
> cax = divider.append_axes("right", size=width, pad=pad)
> contour = ax.contour(x, z, psi[0], 100, colors='white',linewidth=4,
> levels=contour_levels)                 
> cbar = plt.colorbar(density, cax=cax)
> cbar.set_label(r"$\mathbf{\rm{log}_{10}(\rho{\rm v_p})}$")
> 
> # Save all frames as PNGs
> dir = directory 
> if not os.path.isdir(dir): os.makedirs(dir)
> for t in range(N_files):
> ax.cla()
> ax.set_xlabel(xlabel)
> ax.set_ylabel(ylabel)
> ax.set_xlim((0, x_lim))
> ax.set_ylim((0, y_lim))
> ax.minorticks_on()
> ax.set_title(title[t],y=1.04)
> xticks = ax.xaxis.get_major_ticks()
> xticks[0].label1.set_visible(False)
> ax.xaxis.set_tick_params(labelsize=50)
> ax.yaxis.set_tick_params(labelsize=50)
> ax.xaxis.set_tick_params(width=3,length=8)
> ax.yaxis.set_tick_params(width=3,length=8)
> ax.set_aspect('equal',adjustable='box')
> print(psi[t])
> density = ax.pcolormesh(x, z, rho[t]*v_p[t], norm=LogNorm(), cmap=colormap)
> contour = ax.contour(x, z, psi[t], 100, colors='white',linewidths=2,levels=contour_levels)
> contour_alf = ax.contour(x, z, alfsurf[t], 100, colors='black',
> linewidth=.5, linestyles='dashed', levels=[1])
> plt.clabel(contour,fmt=r'$\mathbf{%.2f}$', fontsize=35)
>     
> ssc1=20
> velocity = ax.quiver(x[drv21::dv,::dv], z[drv21::dv,::dv], v_x[t,drv21::dv,::dv], v_z[t,drv21::dv,::dv]
> , angles='xy',width=0.004, units='width',scale=ssc1, color='red')
> 
> velocity4 = ax.quiver(x[drv4::dv4,::dv4], z[drv4::dv4,::dv4], v4_x[t,drv4::dv4,::dv4], v4_z[t,drv4::dv4,::dv4]
> , angles='xy',width=0.004, units='width',scale=ssc1, color='black') 
>     
> ax.quiverkey(velocity, -3, -1.7, 1, '', coordinates='data')
> plt.text(-83.3, -8.5, "$\mathbf{%d v_p}$" % facv1,color='red',fontsize=35)
> ax.quiverkey(velocity4, -3, -3.5, 1, '', coordinates='data')
> plt.text(-83.3, -9., "$\mathbf{%d v_p}$" % facv4,color='black',fontsize=35)
> 
> rct=69.6
> plt.text(-rct, -8.3, '$\mathbf{|}$',color='black',fontsize=70)
> plt.text(-rct-1, -8.8, '$\mathbf{R_{cor}}$',color='black',fontsize=50)
>     
> fname = 'z2rhovp.%04d.png' % t
> print('Saving frame',fname)
> plt.savefig(fname,bbox_inches='tight')
> 
> ```

output of python script plot is 👉

 ![Screenshot from 2024-03-20 21-12-34-1](https://global.discourse-cdn.com/julialang/original/3X/2/3/238822bfdf37ce397fe14760d407ca8344a4ae9a.jpeg)

My Julia script is 👇

```julia
begin
	fig=Figure()
	ax=Axis(fig[1,1]; xscale=log10, xlabel = "X", ylabel = "Z")
	hm = heatmap!(ax, rho[1,:,:].*v_p[1,:,:]; colormap=:jet, interpolate=true)
	Colorbar(fig[1, 2], hm; label = "values")
	colsize!(fig.layout, 1, Aspect(1, 1.0))
	colgap!(fig.layout, 7)
	contourf!(psi[1,:,:], levels = 0.3:0.1:1, mode = :relative)
	contourf!(alfsurf[1,:,:], levels = 0.3:0.1:1, mode = :relative)
	fig
end

```

Output of Julia script looks like

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

I have to use two matrices `x` and `z` which are input for pcolormesh() in Python ; and i have to plot `rho[1,:,:].*v_p[1,:,:]`.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/2/623298a8bf69859c293f504ef631ff3eaa051890.png)  
What modifications should i do in Julia script?  
@juliohm

---

<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:** [March 25, 2024, 10:47am UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/2 "2024-03-25T10:47:56Z")

</div>

Hi @raman_kumar , you probably want to visualize a `StructuredGrid(X, Y)` from Meshes.jl. Create the grid and call the `viz` function:

```julia
using Meshes
import GLMakie as Mke

X = rand(100, 100)
Y = rand(100, 100)
grid = StructuredGrid(X, Y)

viz(grid, color = 1:nelements(grid))

```

---

<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, 12:15pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/3 "2024-03-25T12:15:25Z")

</div>

> [@raman\_kumar](#):
>
> `density = ax.pcolormesh(x, z, rho[t]*v_p[t], norm=LogNorm(), cmap=colormap)``

👍 Thanks, How to add plot of variable `rho[1,:,:].*v_p[1,:,:]` on that mesh?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/512f47a3af688ec627f8c0117f8ac5fc900da7d0.jpeg)

---

<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:** [March 25, 2024, 12:21pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/4 "2024-03-25T12:21:28Z")

</div>

You pass the vec(rho) to the color option.

---

<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, 2:43pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/5 "2024-03-25T14:43:07Z")

</div>

Please explain in detail. I also have to also add contours of `psi[1,:,:]` and  
`alfsurf[1,:,:]` on plot. `rho[t]*v_p[t]` is also `270x100` matrix. How to put it in **color**?

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

---

<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:** [March 25, 2024, 3:03pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/6 "2024-03-25T15:03:29Z")

</div>

Check the vec function in Julia. It converts any array into a flat vector. That is what you need to pass.

---

<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, 3:42pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/7 "2024-03-25T15:42:38Z")

</div>

Vec() is not working, `rho[1,:,:].*v_p[1,:,:]` is a `Matrix{Float64} (alias for Array{Float64, 2}).`

---

<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:** [March 25, 2024, 3:52pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/8 "2024-03-25T15:52:51Z")

</div>

Vec != vec

---

<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:03pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/9 "2024-03-25T16:03:21Z")

</div>

Yes, Now `viz(grid, color=vec(rho[1,:,:].*v_p[1,:,:]))` is giving output. How to add contours to it?

> [@raman\_kumar](#):
>
> I also have to also add contours of `psi[1,:,:]` and  
> `alfsurf[1,:,:]` on plot.

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/6/16eed2c42ed88b094e4ac251c63810b31823f89b.png)

---

<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:** [March 25, 2024, 4:04pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/10 "2024-03-25T16:04:25Z")

</div>

Are you sure it is working? I only see a single color in the preview.

---

<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:** [March 25, 2024, 4:05pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/11 "2024-03-25T16:05:38Z")

</div>

Contours are produced with other Makie recipes that we didn’t wrap yet. Please check the Makie.jl documentation for additional information.

---

<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 26, 2024, 2:37am UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/12 "2024-03-26T02:37:11Z")

</div>

There is something wrong. There should be some color variation in `rho[1,:,:].*v_p[1,:,:]` plot as i am getting in Python version of script. 😮‍💨

---

<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 28, 2024, 6:13pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/13 "2024-03-28T18:13:48Z")

</div>

> [@juliohm](#):
>
> I only see a single color in the preview.

In Meshes.jl what is equivalent of **norm** parameter in [pcolormesh()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pcolormesh.html) ?

```julia
density = ax.pcolormesh(x, z, rho[t]*v_p[t], norm=LogNorm(), cmap='jet')

```

I can get desired color variation in plot if i apply colormap normalisation to logarithmic scale.

---

<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:** [March 28, 2024, 6:43pm UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/14 "2024-03-28T18:43:57Z")

</div>

> [@raman\_kumar](#):
>
> I want to get equivalent plot in Julia that i have using Python [pcolormesh()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pcolormesh.html) function.

You can also just call Matplotlib directly from Julia with PythonPlot.jl or PyPlot.jl …

---

<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 29, 2024, 3:25am UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/15 "2024-03-29T03:25:18Z")

</div>

I am converting Python script for plotting outputs from [The PLUTO Code for Astrophysical GasDynamics](https://plutocode.ph.unito.it/). It is intensive task so it would be better if i use packages in pure Julia. 🤔 PyPlot.jl would be slower than Makie.jl or Plots.jl and Meshes.jl. Isn’t there a way to add `norm` parameter in Meshes.jl or Makie.jl ?

---

<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 30, 2024, 5:03am UTC](https://discourse.julialang.org/t/python-pcolormesh-alternative-in-julia/111885/16 "2024-03-30T05:03:02Z")

</div>

> [@raman\_kumar](#):
>
> Isn’t there a way to add `norm` parameter in [Meshes.jl](https://juliahub.com/ui/Packages/General/Meshes) or [Makie.jl](https://juliahub.com/ui/Packages/General/Makie) ?

I am able to apply `norm` (LogNorm) in Meshes.jl by writing few extra lines. 🤪 Now color variation is visible.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/6/264db9ff133025c3ad0141434c857d9f25355f2f.png)

```julia
grid = StructuredGrid(x, z)
set_theme!(backgroundcolor = :gray90)
function safe_log(x)
    if x == 0
      	return 0
    else
        return log(x)
    end
end
z_log = safe_log.(vec(alfsurf[t,:,:]))
fig, ax, plt = Meshes.viz(grid, color=z_log, alpha=0.8, colorscheme=:jet)

```
