Hide the tics behind a 3D figure in GNUPLOT - gnuplot

I am trying to draw a figure using some perspective in GNUPLOT. I have already used set xyplane at 0 to add the perspective effect. The problem now is that the tics appear in front of my sphere. For a 3D effect, I want the sphere to obstruct some of the tics, leaving them in the background (the ones on the y and x axis). I tried using set tics back but it doesn't work.
Here is the code:
# set term x11 0
set xlabel 'X'
set ylabel 'Y'
set zlabel 'Z'
set title 'Ray on Sphere 2'
set parametric
set isosamples 50,50
set hidden
set view 120, 200
set view equal
R = 3e-6
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
set xyplane at 0
set xtics back
splot R*cos(u)*cos(v),R*cos(u)*sin(v)-1.5e-6,R*sin(u) w l lc rgb 'yellow' title 'Silica Particle',\
'-' w p title 'Incidence Point'
and the result:

I think your best bet is to move the tic labels to the other side of the plot.
# All as before
set xlabel 'X'
set ylabel 'Y'
set zlabel 'Z'
set title 'Ray on Sphere 2'
set parametric
set isosamples 50,50
set hidden
set view 120, 200
set view equal
R = 3e-6
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
set xyplane at 0
# Now a change in the tic label placement
set xtics offset graph 0, 1.2
set ytics offset graph -1.2, 0
set xlabel offset graph 0, 1.5
set ylabel offset graph -1.5, 0
splot R*cos(u)*cos(v),R*cos(u)*sin(v)-1.5e-6,R*sin(u) w l lc rgb 'yellow' title 'Silica Particle'
I'm sure you can make it nicer by tweaking the placement, the font size, etc

Your view setting is a bit strange for me.
With set view 60,160 it looks like this
One can play with the view interactively and read the setting from the bottom bar.

Related

How to avoid pm3d surface from occluding other objects which are infront of it?

I am trying to plot a simple linear surface of the equation x + y + 2z = 0. This is my file:
set xrange [-4:4]
set yrange [-4:4]
set zrange [-4:4]
set xlabel 'x'
set ylabel 'y'
set zlabel 'z'
set xyplane at -4.0
unset xzeroaxis
unset yzeroaxis
unset zzeroaxis
set border 1023-128
set xtics out nomirror
set ytics out nomirror
set ztics out
set xtics add ('' -4)
set label 1 "{/:Italic x} + {/:Italic y} + 2{/:Italic z} = 0" at 4,4.2,-2 font 'Times New Roman, 11'
set arrow 1 from 0,0,-4 to 0,0,4 filled
set arrow 2 from 0,-4,0 to 0,4,0 filled
set arrow 3 from -4,0,0 to 4,0,0 filled
set arrow 4 from 3.9,4.1,-2.1 to 3.6,3.6,-2.5
unset key
set pm3d lighting primary 0.5 specular 0.6
set style fill transparent solid 1 noborder
set palette defined (0 "cyan", 1 "green")
unset colorbox
set pm3d depthorder
splot -x/2-y/2 with pm3d
The result
I'm using arrows to show the axis, since they seem to be stuck to the xy plane, and moving it causes further issues with the tics and border. But now the problem is that they are completely occluded by the surface. Is there a setting which allows to appear in front when they "pierce" the surface? I'd like to make the surface semitransparent, but the problem is clearer with these settings.
I guess your original idea, i.e. "piercing" a 3D surface with an arrow or line does not work right away in gnuplot, because gnuplot will not calculate the piercing points automatically. Please correct me if I am wrong and let me know in case there might be a simple solution to this.
As you did in your simple case, you can just split the arrow at the origin, because you already know the piercing point. However, what do you do if the surface is irregular or has several piercing points?
Solution: take the effort to create a segmented 3D arrow and let gnuplot automatically show and hide the surfaces as needed. This is probably getting close to what you had in mind. However, this solution will show surprises when you want to change the color of the arrows. So, there is still room for improvement.
Code: (simple version with arrows just along x,y,z axes)
### arrows "piercing" a 3D surface
reset session
set view equal xyz
set xyplane relative 0.0
set xrange [-4:4]
set yrange [-4:4]
set zrange [-4:4]
# create 3D arrow
r = 0.01 # radius of arrow
rHead = 0.1 # radius of arrrowhead
n = 6 # corners of arrow
set print $myArrow
do for [h=-100:90] {
do for [a=360/n:360:360/n] {
print sprintf("%g %g %g",r*cos(a),r*sin(a), h/100.)
}
print ""
}
do for [h=90:100] {
do for [a=360/n:360:360/n] {
print sprintf("%g %g %g",rHead*(100-h)/10.*cos(a), \
rHead*(100-h)/10.*sin(a), h/100.)
}
print ""
}
set print
unset key
unset colorbox
set pm3d depthorder
set samples 100
set isosamples 100
set view 65,46,1.3
# function to demonstrate "piercing"
f(x,y) = (sin(x*3)/x + sin(y*3)/y - 3)/2
splot \
f(x,y) w pm3d, \
$myArrow u 1:2:($3*4):(0) w pm3d lc rgb var, \
$myArrow u 2:($3*4):1:(0) w pm3d lc rgb var, \
$myArrow u ($3*4):1:2:(0) w pm3d lc rgb var
### end of code
Result:
gnuplot> help layer
A gnuplot plot is built up by drawing its various components in a fixed order.
This order can be modified by assigning some components to a specific layer
using the keywords `behind`, `back`, or `front`. For example, to replace the
background color of the plot area you could define a colored rectangle with the
attribute `behind`.
set object 1 rectangle from graph 0,0 to graph 1,1 fc rgb "gray" behind
The order of drawing is
behind
back
the plot itself
the plot legend (`key`)
front
Within each layer elements are drawn in the order
grid, axis, and border elements
pixmaps in numerical order
So basically you need to add the "front" attribute to the objects you want to appear in front of the plot.

No mxtics in logscale with gnuplot

I am having troubles making Gnuplot show the intemediate tics (mxtics) in a log scale. I have looked up similar topics, but none of the answers work for me. Please find enclosed a minimal working example,
set size 1,1
set origin 0,0
unset xtics
set yrange [-1:1]
set xrange [1e-6:2]
set mxtics 10
set logscale x
set format x ""
set xtics 1, 1e-2
set xtics 1e-4, 1e-6
set xtics add ("10^{-4}" 1e-4,"10^{-2}" 1e-2, "1" 1) offset 0,0
set xtics nomirror
set format y ""
unset ytics
p 0.0
which provides the following output, with no mxtics:
I would like to have 10 mxtics per decade, i.e., one at 10^{-2}, one at 2 10^{-2}, ..., one at 9 10^{-2}. Do you know how to do that? I have tried changing the order of the command lines, but it didn't work.
FYI, here is the Gnuplot version that I am using:
$ gnuplot
G N U P L O T
Version 5.2 patchlevel 2 last modified 2017-11-15
EDIT: The following is tentative way to make this work:
set xrange [1e-6:1]
set yrange [-1:1]
set logscale x
set format x "10^{%T}"
set xtics add ("" 1e-3, "" 1e-5, "" 1e-1)
set mxtics 10
set xtics nomirror
set format y ""
unset ytics
unset ylabel
p 0 w l lw 2 linecolor rgb "black" noti
and it produces the output However, this does not work if I set term epslatex, which is the terminal that I ultimately want to use.
In fact, if I run the same script with epslatex terminal, the xtics at 10^{-3}, ... are not removed.
It seems you are trying a bit too complicated. Wouldn't the following do what you want or do you want something special and different?
Code:
### log tics and mtics
reset session
set xrange[1e-6:2]
set logscale x
set format x "10^{%T}"
set mxtics 10
set grid xtics,mxtics lt -1, lt 0
set xtics add ("" 1e-6, "" 1e-5, "" 1e-3, "" 1e-1, "1" 1)
plot x
### end of code
Result:
As for me, the epslatex output produced from your edit looks fine for me, it only contains the major xtics labeled at 10^-6, 10^-4, 10^-2 and 10^-0. Therefore this code is working perfectly (I only changed the xtics format to math so that latex doesn't complain):
set term epslatex standalone
set out "logtics.tex"
set xrange [1e-6:1]
set yrange [-1:1]
set logscale x
set format x "$10^{%T}$"
set xtics add ("" 1e-3, "" 1e-5, "" 1e-1)
set mxtics 10
set xtics nomirror
set format y ""
unset ytics
unset ylabel
p x w l lw 2 linecolor rgb "black" noti
unset out
unset term
I am using gnuplot 5.3, maybe there was some kind of bugfix in-between…
If that is not helping you, did you consider switching to cairolatex terminal? It has the same purpose, but it overcomes the troubles of converting eps to pdf prior to inclusion in pdflatex.

GNUPlot - Plotting a data set in polar form (r, θ, T(r,θ)) to a contour/heat map

I hope I don't get firebombed here, this is the first time I'm posting. Lol
So after a grueling simulation, I have this large data set of the form r, θ, and T(r,θ). I needed to plot a contour map for this data set. θ is in degrees.
So I ran to GNUPlot and I hoped that this will save me from my problems. I studied a bit about how it works, but I still cannot plot my 2D contour data.
I then researched about my problem then I saw this thread here in SE:
gnuplot 2D polar plot with heatmap from 3D dataset - possible?
I used both codes (that I assume to be working) in that post and NONE is working with my dataset. I hope someone can help me with my problem.
This is what I changed the code into:
reset
set terminal pngcairo size 800,800
set output '3d-polar.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d map
unset key
set multiplot
# plot the heatmap
set parametric
set isosamples 500
unset border
unset xtics
unset ytics
set angles degree
r = 0.05 # CHANGED THIS
set urange[0:r] # radius
set vrange[0:360] # angle
set xrange[-r:r]
set yrange[-r:r]
set colorbox user origin 0.9,0.1 size 0.03,0.8
splot 'data.dat' using 1:2:3 # CHANGED THIS
# now plot the polar grid only
set style line 11 lc rgb 'white' lw 2
set grid polar ls 11
set polar
set rrange[0:r]
unset raxis
set rtics format '' scale 0
unset parametric
set for [i=0:330:30] label at first (r+0.35)*cos(i), first (r+0.35)*sin(i)\
center sprintf('%d', i)
plot NaN w l
unset multiplot
So I had this little 'eureka' moment. Apparently I had to put dgrid3d and had to convert the coordinates from polar to cartesian.
Upon converting (and putting set dgrid3d) this ugly plot appeared:
reset
set terminal pngcairo size 800,800
set output '3d-polar.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d map
unset key
set multiplot
# plot the heatmap
set parametric
set isosamples 500
unset border
unset xtics
unset ytics
set angles degree
r = 0.05 # CHANGED THIS
set urange[0:r] # radius
set vrange[0:360] # angle
set xrange[-r:r]
set yrange[-r:r]
set colorbox user origin 0.9,0.1 size 0.03,0.8
set dgrid3d # ADDED THIS
splot 'data.dat' using 1:2:3 # CHANGED THIS
# now plot the polar grid only
set style line 11 lc rgb 'white' lw 2
set grid polar ls 11
set polar
set rrange[0:r]
unset raxis
set rtics format '' scale 0
unset parametric
set for [i=0:330:30] label at first (r+0.35)*cos(i), first (r+0.35)*sin(i)\
center sprintf('%d', i)
plot NaN w l
unset multiplot
The plot.
I'm at my limit here now. Three questions.
1. Am I correct to assume that I can use the mathematics in gnuplot to convert the coordinates from polar to cartesian? Or is there a way for me to use the polar coordinate data immediately?
2. How do I limit the 'colors' to the round polar plot?
3. What happened to the degree and radius readings? Where did they go?
Thanks!

Variable transparency is possible in Gnuplot?

I want to plot a filled circle in Gnuplot, but with transparency changing with the distance to the center of the circle, i.e. a transparency gradient (solid near to the center, and transparent near to the radius of circle).
Is there any way of doing this in Gnuplot?
Thank you in advance!
One option would be to define a custom function describing the gradient (e.g., a Gaussian) and then use pm3d map to visualize it:
set terminal pngcairo size 400,400
set output 'fig.png'
unset border
unset colorbox
unset xtics
unset ytics
set pm3d map
#force the plot to occupy the entire canvas
set lmargin at screen 0
set rmargin at screen 1
set tmargin at screen 1
set bmargin at screen 0
set isosamples 100,100
#specify custom palette
set palette model RGB
set palette defined ( 0 "white", 1 "royalblue" )
set xr [-10:10]
set yr [-10:10]
#a Gaussian might be a reasonable choice
splot exp(-0.2*(x*x+y*y))
This then produces:
Another choice of the defining function might be:
set xr [-pi:pi]
set yr [-pi:pi]
fn(r) = (r>(pi/2))?0:(cos(r))
splot fn(sqrt(x*x+y*y))
This yields:
Try this:
set xrange [0:10]
set yrange [0:10]
do for [i=1:100] {
set style fill transparent solid i/100. noborder
set object circle at 5,5 radius 1.-i/100. fc rgb 'blue'
}
plot -1
Not all terminals support transparency; see help transparent. I made this with the terminal x11.

gnuplot: annotate plot with X and Y lines

I would like to plot the inverse of y=xe^x, with dashed lines leading to the point (-exp(-1), -1)
set parametric
set style arrow 1 head filled size char 1.5,20,50
set arrow 1 from -4.1,0 to 4.1,0 heads
set arrow 2 from 0,-4.1 to 0,4.1 heads
set trange[-4:4]
set xrange[-4:4]
set yrange[-4:4]
set xlabel "x"
set ylabel "y"
unset border
set xtics axis format " "
set ytics axis format " "
plot [-4:0.999] log(1-t)/t, t
plot [-4:4] t*exp(t), t lt rgb "black" title '', -exp(-1),t lt rgb "black", t, -1 lt rgb "black"
I would like to restrict the vertical line to running from y=-1 to y=0, and the horizontal line from x=-exp(-1) to x=0. How can this be done?
Also, is there an easier way to set the line colour for all plots, rather than specifying it for each one?
I see, if I'm understanding it correctly you basically need to annotate your graph with the dashed lines, so why don't you use arrows for the same too.
For example:
# I'm using pngcairo dashed terminal
set terminal pngcairo dashed
set output 'graph.png'
set parametric
set style arrow 1 head filled size char 1.5,20,50
set arrow 1 from -4.1,0 to 4.1,0 heads
set arrow 2 from 0,-4.1 to 0,4.1 heads
#set trange[-4:4]
set xrange[-4:4]
set yrange[-4:4]
set xlabel "x"
set ylabel "y"
unset border
set xtics axis format " "
set ytics axis format " "
#plot [-4:0.999] log(1-t)/t, t
set arrow from -exp(-1),-1 to -exp(-1),0 nohead lt 3
set arrow from -exp(-1),-1 to 0,-1 nohead lt 3
plot [-4:4] t*exp(t),t lt rgb "black" title ''
As far as your questions about line colours is concerned, it is also dependent on the type of terminal that you are using. For instance, with the pngcairo terminal, Gnuplot will itself assign different linestyles to plots. If you want specific colours, then offcourse you have to state them. Take a look at this link. You'll find lots of info with a simple search on Gnuplot's linestyles/linecolour/linetypes etc.

Resources