Visualisation in Julia

Dear All,

I want to use the following plot function from Matlab in Julia.
I could not find the correct commands of Julia to do this work.

 subplot(1,1,1)
     pcolor(con), shading interp, ...
     axis('off'), axis('equal'), title('simulation');
 colorbar

 temp=['fig',num2str(jj),'.png']; saveas(gca,temp);

drawnow




Thanks a lot for your support in advance!

Julia doesnā€™t have builtin plotting so you would need to use a plotting library (package).

What have you tried so far?

1 Like

Hi Kristoffer,

thank your for reply!

I am trying to plot the phase-field value in 2D using FDM, example con := zeros(Float64,32,32)

In Matlab :

pcolor(con), shading interp

this simple command works well.

Yeah, but what have you tried in Julia? What plotting packages have you used and what examples of these have you tried to modify to get something that looks like the Matlab plot?

2 Likes
using PyPlot

M = rand(10, 10)
pcolormesh(M)

There are some Issues with PyPlot in MKL Julia, hence, How can I use Plots.jl ?

Have a look at the Plots.jl examples here: http://docs.juliaplots.org/latest/examples/gr/ and see whether thereā€™s a plot that looks like the one youā€™re after.

You can use Plots like any other package by doing using Plots, by default it will use the GR backend.

Use can try the Plots.jl package. This can be installed by typing ] add Plots in the REPL.

using Plots
d = rand(5, 5)
p = heatmap(d, aspect_ratio=:equal, axis=false, title="simulation")
# or
p = contour(d, aspect_ratio=:equal, axis=false, title="simulation")
savefig(p, "myplot.png")
1 Like

Hi @feanor12 @nilshg @kristoffer.carlsson,

many thanks for your support!

how can define the name of file automatically in Julia, i.e.

 temp=['fig',num2str(jj),'.png']; saveas(gca,temp);

fig1.png
fig2.png
ā€¦
fig30.png
ā€¦

savefig(p, "fig"*string(jj)*".png")

2 Likes

String interpolation is really useful for that:

savefig(p, "fig$(jj).png")
2 Likes

very nice, thank you!

1 Like

If you really got stuck with plotting using native Julia, as a last resort, you can also use MATLAB to plot directly from Julia using MATLAB.jl.

using MATLAB
mxcall(:subplot,0, "111")
mxcall(:pcolor, 0, con)
mxcall(:shading,0,"interp")
mxcall(:axis,0,"off")
mxcall(:axis,0,"equal")
mxcall(:title,0,"simulation")
mxcall(:colorbar,0)
temp="fig$(jj).png"
@mput temp
mat"saveas(gca,temp)"

So basically, con is a julia Array{Float64,2} it can be used as julia type directly, and temp is String, but it need to put into MATLAB session, because Julia canā€™t hold the gca type, so saveas(gca,temp) has to be eval inside MATLAB as an expression.

or just put the data you want to plot into MATLAB session and eval the whole script:

using MATLAB
temp="fig$(jj).png"
@mput con
@mput temp 
mat"""
subplot(1,1,1)
     pcolor(con), shading interp, ...
     axis('off'), axis('equal'), title('simulation');
 colorbar

saveas(gca,temp);
"""
2 Likes

@cchderrick, thanks a lot, your suggestion is very useful!

can we convert the matrices to .vtu file very easily in Julia ?

https://github.com/jipolanco/WriteVTK.jl

2 Likes

it works well, thanks!

for some special cases, it doesnot work:

using WriteVTK
 A = rand(100, 100)
vtk_write_array("fig"*string(1)*", A, "my_property_name")

ERROR: syntax: cannot juxtapose string literal

Even the syntax highlighting in discourse shows your typo. It is a good idea to try a little bit yourself before immediately turning to discourse. Learning how to debug things will help you in the future.

5 Likes