increment of ytics in gnuplot - gnuplot

I plotted a graph and I'd like to show ytics as a function of x. For example:
I plot x^2 and I'd like to show ytics for 0,1,4,9... . Is there any way to do this automatically or I have to set manually every tic in y-axis? I tried to set a function when defining ytics but gnuplot doesn't accept it.
Thanks for any help

You can use a for loop. Of course, here you need to know your x-range in advance:
f(x)=x**2
set ytics ( sprintf('%f',f(-10)) f(-10) )
set for [i=-9:10] ytics add ( sprintf('%f',f(i)) f(i) )
plot f(x)

Here is my semiautomatic answer, using a for loop to build the ytics string:
#!/usr/bin/env gnuplot
set terminal pngcairo
set output 'test.png'
# x range
xmin = 0
xmax = 10
set xrange [xmin:xmax]
# define function of x to make plot, define tics
yfn(x) = x**2
# integer counting over tic positions
imin = int(xmin)
imax = int(xmax)
# build tics string
tix = '("'.sprintf('%d', yfn(imin)).'" '.sprintf('%d', yfn(imin))
do for [i=(imin+1):imax] {
tix = tix.', "'.sprintf('%d', yfn(i)).'" '.sprintf('%d', yfn(i))
}
tix = tix.')'
set macros
# set ytics using macro expansion
set ytics #tix
plot yfn(x)
This is the result:

i do not answer quite exactly to your question,
Although, that makes me think of customising the label of ytis.
Example :
f(x)=x**2
f1(x)=sqrt(abs(x))
set ytics format ""
set ytics scale 1
set t qt 0 enhanced font "Sans,9"
#set mytics 8
# ploting once the function for getting the GPVAL_ variable
plot f(x) t "f(x)=x**2"
# calculating function for the list of increment
linRg(start,end,increment)=system(sprintf("seq %g %g %g", start, increment, end))
# forcing the tics, otherwise, the tics of the first plot will still be marked
set ytics GPVAL_Y_MIN, (GPVAL_Y_MAX-GPVAL_Y_MIN)/8, GPVAL_Y_MAX
do for [i in linRg(GPVAL_Y_MIN, GPVAL_Y_MAX, (GPVAL_Y_MAX-GPVAL_Y_MIN)/8)] { pr i; set ytics add ('f(|'.gprintf("%.1s%c",f1(i)).'|)'.gprintf("=%.1s%c",i) i)}
replot
will produced :

Related

rotate the gnuplot raxis label to 45 degrees without rtics

I have a polar plot in Gnuplot given by
set polar
set angles degrees
unset xtics
unset ytics
unset raxis
set ttics 0,30
set grid polar linestyle 1 dashtype 2 linecolor rgb 'grey'
set samples 200, 200
unset border
set xrange [-0.51:0.51]
set yrange [-0.51:0.51]
set size square
unset key
plot sin(2*t)*cos(2*t)
Which gives the following output:
However,
I want the rlabels (0, 0.1, 0.2, etc) at an angle of 45 degrees instead of them being placed on the x-axis, and
There should be no rtics
Any help will be appreciated.
In order to remove the rtics do set rtics scale 0.
You can set the starting angle for polar graph (check help theta), however, only to {right|top|left|bottom} and not to 45°, but this still wouldn't help you here.
So, probably you have to set the labels "manually". Maybe there is an automatic way which I am not aware of. Check the following example.
Code:
### set rtics on 45° "axis"
reset session
set polar
set angles degrees
unset xtics
unset ytics
unset raxis
set ttics 0,30
set grid polar linestyle 1 dashtype 2 linecolor rgb 'grey'
set samples 200, 200
unset border
set xrange [-0.51:0.51]
set yrange [-0.51:0.51]
set size square
unset key
set rtics scale 0
set format r ""
myRTics = "0.1 0.2 0.3 0.4 0.5"
do for [i=1:words(myRTics)] {
r = word(myRTics,i)
set label i sprintf("%s",r) at r*cos(45),r*sin(45) center
}
plot sin(2*t)*cos(2*t)
### end of code
Result:
I addition to #theozh's excellent answer, I realized I can generate the labels on the fly, by providing the start, end, and increment of the labels. So instead of
myRTics = "0.1 0.2 0.3 0.4 0.5"
do for [i=1:words(myRTics)] {
r = word(myRTics,i)
set label i sprintf("%s",r) at r*cos(45),r*sin(45) center
}
I will write
rangle = 45
rstart = 0.1 ; rend = 0.5 ; rinc = 0.1 ;
n = floor((rend-rstart)/rinc) + 1
do for [i=1:n] {
r = rstart + (i-1)*rinc
set label i sprintf("%g",r) at r*cos(rangle),r*sin(rangle) center
}
The rest of it will be as already answered by #theozh. The output doesn't change.
I am a bit surprised that Gnuplot doesn't have any inbuilt method to handle this.

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.

Define and use vector for graph ticks

I like to store everything I can in a variable since each gnuplot script I make generates tens of plots at once and it makes things easier to track. Here is a sample of one plot (variable of interest: ytics):
# Setup style
set terminal pngcairo dashed
unset key
set style line 1 pointtype 7 pointsize 0.3 linecolor rgb "black"
# Setup the plots' ytics
ytics_H2 = (0,0.002,0.004,0.006,0.008,0.010,0.012);
# Store the range for each variable
min_T = 200; max_T = 1800;
min_H2 = 0; max_H2 = 0.012;
# Plot
set output 'my_output_H2.png'
set ytics ytics_H2
set xrange [min_T :max_T ]
set yrange [min_H2:max_H2]
plot 'scatter.dat' using 1:2 with points linestyle 1
Here is the result:
As you can see, only the last tick gets printed. If I replace the variable ytics by the vector to which it is set to, everything works as expected.
For such use cases gnuplot has macros:
set macros # necessary only for v < 5.0
ytics = "(1, 5, 8)"
set ytics #ytics
plot x
In order to use macros, you must define a string variable which contains the command parts you want to use at a later point, here ytics = "(1, 5, 8)". Later you can use its content with #ytics.
The important fact here is, that gnuplot first replaces #ytics with the content of the string variable ytics, i.e. expands set ytics #ytics to set ytics (1, 5, 5) and only then executes the whole command.
Because your intervals are fixed and the same, you could also use start, incr, end form:
set ytic 0, 0.002, 0.012

Position of tic marks in Gnuplot

I am looking for a way to position tic marks in gnuplot between the axis, but so far I only found solution to put them in or out:
set tics in
puts all tic marks inside of the canvas
set tics out
puts all tic marks outide of the canvas
All I want is to place tic marks on both sides of the axis, somethink like
--l--l--
Thanks for a hint!
As said in the comments, it seems not possible to place the tics on both sides of the axis. A workaround would be to plot the axis twice, or to draw the tics by hand with the set arrow.
Drawing tics by hand:
Consider the following settings:
Xmin = -4.0 # range in x
Xmax = 4.0
Ymin = -1.2 # range in y
Ymax = 1.2
NXtics = 8 # number of Xtics
NYtics = 4 # number of Ytics
epsX = 0.05 # length of Xtics
epsY = 0.03 # length of Ytics
dX = (Xmax-Xmin)/NXtics # distance between Xtics
dY = (Ymax-Ymin)/NYtics # distance between Ytics
Next, we draw the bottom, top, left, and right tics:
# xtics and x2tics
do for [i=0:NXtics] {
posX = Xmin+i*dX
set arrow from posX,Ymin-epsY to posX,Ymin+epsY nohead front # bottom
set arrow from posX,Ymax-epsY to posX,Ymax+epsY nohead front # top
}
# ytics and y2tics
do for [i=0:NYtics] {
posY = Ymin+i*dY
set arrow from Xmin-epsX,posY to Xmin+epsX,posY nohead front # left
set arrow from Xmax-epsX,posY to Xmax+epsX,posY nohead front # right
}
Since you are drawing the tics by hand, you will need to configure the axis numbers and ranges:
set xtics Xmin,dX,Xmax scale 0 offset 0,-epsY
set ytics Ymin,dY,Ymax scale 0 offset -epsX,0
set xrange [XMIN:XMAX]
set yrange [YMIN:YMAX]
Finally, your highly complicated plot:
plot sin(x)
Result:
This method also allows you to break the axis
Drawing axis twice:
This method is easier; but you need to set set margins of the canvas, and to use the multiplot mode:
set tmargin at screen 0.9 # top margin
set bmargin at screen 0.2 # bottom
set lmargin at screen 0.2 # left
set rmargin at screen 0.9 # right
set yrange [-1.2:1.2]
set multiplot
set tics scale 0.5 # scale size of the tics
plot 2 notitle # a plot outside the canvas, just to draw the axis
set tics out # tics outside
set format xy '' # delete the numbers
unset border # delete the border
plot sin(x) # your awesome plot
unset multiplot
The result is similar :)
Quick & dirty approach:
set multi
set tics scale 0.5
plot sin(x)/x
set tics out
replot
unset multi
Beware this overprints your graph with a second one. Should be OK for bitmap output, but don't do it if you have vector output (pdf, eps), especially if your graph is complicated or contains a lot of datapoints. It blows up the resulting file to twice its size.
Gnuplot at the moment (v 5.0pl1) has no option to place the tics centered on the axis. You'll have to use one of the workarounds shown here.

Gnuplot x-axis resolution

I'm trying to plot in gnuplot a log-periodic function: cos((log(abs(t-Tc))*PI/log10(lambda) ) + phi)
But because of the nature of log(x) near to x=0, the plot is getting ugly.
How to plot a log-periodic function in gnuplot so it looks nice?
My plot script looks like this:
phi = 1
TcFormated = 9.67e+8
lambda = 2
PI = 3.1415
g(t) = abs(cos((log(abs(t-TcFormated))*PI/log10(lambda) ) + phi))
set tmargin at screen 0.01
set bmargin at screen 0.99
set lmargin at screen 0.01
set rmargin at screen 0.99
set xrange [8.4e+8:1.04e+9]
set yrange [0:1]
unset xtics
unset ytics
plot g(x) t '' w l
pause -1
After setting:
set samples 10000
I got a much better looking graph:
If you want to increase the resolution try
set samples <X>
where <X> is an integer. Per default this integer is set to 100. Increase that number to your needs.
Though, the higher the integer is chosen the longer it will take gnuplot to plot the graph.

Resources