gcalderone / Gnuplot.jl

Julia interface to gnuplot
Other
119 stars 16 forks source link

parametric plot #5

Closed lazarusA closed 4 years ago

lazarusA commented 4 years ago

Hello again,

it's possible to define a parametric plot? something like:

using Gnuplot
u = LinRange(0,π,50)
v = LinRange(0,2π,50)
f(u,v) = r*sin(u)*cos(v)
g(u,v) = r*sin(u)*sin(v)
h(u,v) = r*cos(u)
@gsp(f(u,v), g(u,v), h(u,v), "set parametric")

or something different but that it works.

gcalderone commented 4 years ago

Yes, but keep in mind that similar code in Julia and Gnuplot leads to different results:

In the former case you will have one point for each of the 50 coordinate pairs. In the latter case you will have a point for each combination of the u and v parameters, and there will be lines connecting all the points across the u and v directions.

gcalderone commented 4 years ago

Also, if you want a parametric plot on a Cartesian grid, leaving all math on Julia side:

using Gnuplot
r = 1.
f(u,v) = r*sin(u)*cos(v)
g(u,v) = r*sin(u)*sin(v)
h(u,v) = r*cos(u)

@gsp " " :-  # this is just to reset session
v = LinRange(0,2π,50)
for u in LinRange(0,π,10)
    @gsp :- f.(u,v) g.(u,v) h.(u,v) "w l notit lc rgb 'black'" :-
end

u = LinRange(0,π,50)
for v in LinRange(0,2π,10)
    @gsp :- f.(u,v) g.(u,v) h.(u,v) "w l notit lc rgb 'black'" :-
end
@gsp
lazarusA commented 4 years ago

Yes, I like more this approach. I think that leaving everything math related to Julia is best (less confusion for teaching and doing), and use Gnuplot just for plotting.

lazarusA commented 4 years ago

although, with this approach I'm not longer able to simply just call pm3d, am I? and get some colors... as in http://gnuplot.sourceforge.net/demo/hidden2.html (last example).

gcalderone commented 4 years ago

You're right: this is currently not possible. I'm already working on a new release, expect it in a few days...

lazarusA commented 4 years ago

Good, I hope not a lot of breaking changes. Because I already did a lot examples with your current version. If is ok with you I will do a pull request with them. Plus all of them are already documented in a separated web page, done with Franklin.

gcalderone commented 4 years ago

There will be no breaking change at all.
It is just a case that I never considered.

Also, PR with new examples are very VERY welcome! Thank you!

gcalderone commented 4 years ago

I updated the master branch. You can test it with:

Pkg.develop("Gnuplot")
Pkg.update("Gnuplot")

The code to create the desired plot is available here: https://github.com/gcalderone/Gnuplot.jl/blob/master/examples/hidden2.3.jl

In the following days I will perform some more tests, then I'll deploy a new release.

lazarusA commented 4 years ago

Hello, I tested this example and it works really good. However, the other one, hidden2.1(in your folder) doesn't show anything.

gcalderone commented 4 years ago

Yes, my fault... Now it works.

lazarusA commented 4 years ago

Oh, are you? I still don't get any output, everything runs but no output. Also, I noticed that you documented palette support. Probably it will be good also to be able to call more cmaps. I did this function to have the whole thing... the downside is to have dependencies... but if included in the distribution then my code will be shorted :D.

using Colors, ColorSchemes
function gp_palette(colormap=:viridis)
    cmap = get(colorschemes[colormap], LinRange(0,1,256))
    ctmp = "0 '#$(hex(cmap[1]))',"
    for i in 2:256; ctmp = ctmp*"$(i-1) '#$(hex(cmap[i]))'," end;
    "set palette defined("*ctmp[1:end-1]*")"
end
lazarusA commented 4 years ago

small example..

# This file was generated, do not modify it. # hide
using Gnuplot, Colors, ColorSchemes, Random
function gp_palette(colormap=:viridis)
    cmap = get(colorschemes[colormap], LinRange(0,1,256))
    ctmp = "0 '#$(hex(cmap[1]))',"
    for i in 2:256; ctmp = ctmp*"$(i-1) '#$(hex(cmap[i]))'," end;
    "set palette defined("*ctmp[1:end-1]*")"
end
# Archimedes spiral
a = 1.5
b = -2.4
t = LinRange(0,5*π,500)
x = (a .+ b*t) .* cos.(t)
y = (a .+ b*t) .* sin.(t)
@gp " " :-  # this is just to reset session
@gp :- "set multiplot layout 3,3; set key off; 
    unset ytics; unset xtics; unset border" :-
colormaps = [:magma, :viridis, :plasma, :inferno, :berlin, 
    :leonardo, :devon, :spring, :ice]
for i in 1:9
    @gp :- i title = "$(colormaps[i])" "set size square" :-
    @gp(:-, x, y, t, "w l lw 3 dt 1 lc palette", 
        gp_palette(colormaps[i]),"set cbtics out nomirror", :-)
end
@gp
save(term="pngcairo size 900,800", output="plt_ex9.png") # hide
lazarusA commented 4 years ago

this part of the gallery I'm working on now.

gcalderone commented 4 years ago

I find your proposal very interesting, since:

However:

I'll think about a way to solve the latter issues. Feel free to drop a PR if you already solved them.

lazarusA commented 4 years ago

you can always change the 256 range to a lower one (let's say 10). And get the discretized version.

get(colorschemes[colormap], LinRange(0,1,10)), although I'm not sure how gnuplot generates(extrapolates this set) in their definition.

In the above definition I was just mimicking some definitions from gnuplot-palettes.

gcalderone commented 4 years ago

I implemented both palettes() following your example and linestyles(). Both functions uses only the minimum number of colors or styles. I also updated the last paragraph in the README.

Oh, are you? I still don't get any output, everything runs but no output.

Please ensure you updated the repo (git pull in ~/.julia/dev/Gnuplot)

lazarusA commented 4 years ago

Now, your first example works but the one discussed here it doesn't. I'm getting something weird. https://github.com/gcalderone/Gnuplot.jl/blob/master/examples/hidden2.3.jl I just copy and paste into the julia repl.

GNUPLOT (default) -> multiplot> splot  
GNUPLOT (default) ->                   ^
GNUPLOT (default) ->            line 26514: function to plot expected
GNUPLOT (default) -> multiplot> splot  
GNUPLOT (default) ->                   ^
GNUPLOT (default) ->            line 26514: function to plot expected
GNUPLOT (default) -> multiplot> splot  
GNUPLOT (default) ->                   ^
GNUPLOT (default) ->            line 26514: function to plot expected
GNUPLOT (default) ->            line 30716: undefined variable: unset
GNUPLOT (default) ->            line 30716: undefined variable: unset
GNUPLOT (default) ->            line 30716: undefined variable: unset
GNUPLOT (default) ->            line 30716: undefined variable: unset

and not the right figure.

This is my env. I'm not sure if this is a problem in my end.

gpDev) pkg> st Gnuplot
    Status `~/gpDev/Project.toml`
  [dc211083] Gnuplot v1.0.0+ [`~/.julia/dev/Gnuplot`]
gcalderone commented 4 years ago

I believe your repository is not updated. Please try the following:

cd ~/.julia/dev/Gnuplot
git pull

Then re-start Julia and retry.

lazarusA commented 4 years ago

Yes, you were right. In this version they do work perfectly. Nice! Closing issue.