Closed lazarusA closed 4 years ago
Yes, but keep in mind that similar code in Julia and Gnuplot leads to different results:
if you wish to run the mathematics in Julia just drop set parametric
and use a .
to vectorize function calls:
u = LinRange(0,π,50)
v = LinRange(0,2π,50)
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 f.(u,v) g.(u,v) h.(u,v)
You will obtain a 3D plot of a spiral.
otherwise, if you leave all math calculations to gnuplot:
Gnuplot.exec("""
set parametric
set urange [0:pi]
set vrange [0:2*pi]
r = 1.
fx(v,u) = r*cos(v)*cos(u)
fy(v,u) = r*cos(v)*sin(u)
fz(v) = r*sin(v)
splot fx(v,u),fy(v,u),fz(v)
""")
You will obtain a sphere.
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.
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
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.
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).
You're right: this is currently not possible. I'm already working on a new release, expect it in a few days...
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.
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!
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.
Hello,
I tested this example and it works really good. However, the other one, hidden2.1
(in your folder) doesn't show anything.
Yes, my fault... Now it works.
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
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
this part of the gallery I'm working on now.
I find your proposal very interesting, since:
ColorSchemes
provides many more palettes than gnuplot-palettes;However:
I'll think about a way to solve the latter issues. Feel free to drop a PR if you already solved them.
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.
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
)
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`]
I believe your repository is not updated. Please try the following:
cd ~/.julia/dev/Gnuplot
git pull
Then re-start Julia and retry.
Yes, you were right. In this version they do work perfectly. Nice! Closing issue.
Hello again,
it's possible to define a parametric plot? something like:
or something different but that it works.