Python pcolormesh() alternative in Julia

I want to get equivalent plot in Julia that i have using Python pcolormesh() 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

density = ax.pcolormesh(x, z, rho[t]*v_p[t], norm=LogNorm(), cmap=colormap)`
Python script is this
# 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 :point_right:

My Julia script is :point_down:

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

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,:,:].


What modifications should i do in Julia script?
@juliohm

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

using Meshes
import GLMakie as Mke

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

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

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

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

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?

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

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

Vec != vec

1 Like

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

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

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

1 Like

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. :face_exhaling:

In Meshes.jl what is equivalent of norm parameter in pcolormesh() ?

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.

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

1 Like

I am converting Python script for plotting outputs from The PLUTO Code for Astrophysical GasDynamics. It is intensive task so it would be better if i use packages in pure Julia. :thinking: 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 ?

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

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)