I plotted a figure with a classical x-axis, let's say from 0 to 25.
I'd like to have the x2-axis having the sqrt of x1-values and I didn't find anything about how to do it.
With my values here, x2range would be [0:5]
But tics shouldn't be linear, tha's why i didn't succeed to make it.
x2-tic 5 would match with 25, x2-tic 4 with 16, 3 with 9, etc.
Any Help ?
I hope there's a not-too-hard way to do this
Cheers.
That can be done since gnuplot 5 with the command set link, which links a secondary axis to the respective primary axis via a transformation function:
set xrange [0:25]
set link x2 via sqrt(x) inverse x**2
set x2tics
set xtics nomirror
plot x
The commands as given above will generate a warning because the mapping isn't unique. But you can ignore that warning as long as your xrange starts at value greater or equal to zero.
For gnuplot 4.6 you must place all x2tics manually:
set xrange [0:25]
set x2range [0:25]
set xtics nomirror
# delete all automatic tics but the one at 0
set x2tics (0)
# add all other tics
set for [i=1:5] x2tics add (sprintf('%d', i) i**2)
plot x
Related
I am try to plot a graph in GNUPLOT which has both positive/negative values. It is plotting in 1st quadrant.I mean x axis start from some negative value, same with y axis. I want to plot on both sides, like we see a 4 quadrants. Like in middle should be 0, on left negative values and on right positive value, for both x and y axis. Please help.
Thanks in advance.
Please make sure that you've done a reasonable search before asking. There are a lot of examples around. Although, sometimes it might not be easy to find the exact graph configuration you are looking for. Basically, everything is in the manual, although it can be confusing for a gnuplot beginner.
You can get help for almost every keyword via typing help <keyword> in the gnuplot console, e.g. help xtics, help zeroaxis, help border, etc.
If I understood your question correctly, you are probably looking for something like this:
Code:
### plot a function in a 4-quadrant graph
reset session
set size square
set xrange[-10:10]
set yrange[-10:10]
set border 0
set xzeroaxis lt -1
set yzeroaxis lt -1
set xtics axis nomirror
set ytics axis nomirror
set mxtics 5
set mytics 5
set grid x y mx my
set xtic add ('' 0) # remove 0 from xtics
set ytic add ('' 0) # remove 0 from ytics
set key top left
f(x) = 0.02*x**3 - 0.5*x
plot f(x) w l lc "red"
### end of code
Result:
I plot with gnuplot the following:
$Data <<EOD
time_,value
1-23:59:58,1
1-23:59:59,2
2-00:00:00,3
2-00:00:01,4
2-00:00:02,5
EOD
set term png size 800,600
set output "ask.png"
set datafile separator comma
set grid
set xdata time
set timefmt "%d-%H:%M:%S"
set format x "%H:%M:%S"
set xtics nomirror
set autoscale xfix
set autoscale x2fix
startnumber=1
xticdata=2
mxticdata=2
set xtics xticdata rotate
set mxtics mxticdata
set x2data
set x2tics startnumber, xticdata rotate
set mx2tics mxticdata
set link x2 via x+1 inverse x-1
plot $Data using 1:2 title columnheader(2)
set output
Data of the column 2 which contains nearly 50,000 records is value of a parameter. set link has to be used to align x-axis and x2-axis. And I want to show x2tic labels for counter/index which must be related to the time(column 1).
The output is alright, but you can see from the attached figure that the labels on x2-axis are big numbers, which is not what I want. I want to get labels like "1,3,5...".
So what's wrong with my code? And how to correct it? Thanks.
If the idea is that the x2 axis should be labeled with the content of column 2 regardless of its numerical relationship to column 1, then you can use:
set xdata time
set timefmt "%d-%H:%M:%S"
set format x "%H:%M:%S"
set xtics nomirror
set x2tics nomirror
set link x2
plot $Data using 1:2:x2ticlabels( int($0+1)%2 ? strcol(2) : "" ) title columnheader(2)
This creates one x2 axis tick label for every data point. The even-numbered ones to blank and the odd-numbered ones are set to whatever is in column 2.
the quickest fix would be
set link x2 via x-86397 inverse x+86397
But it depends what timesteps you have and what numbers you have in column 2. If your time step it is strictly regular and 1 second, and column 2 just counts up, then column 2 is redundant.
Timedata is handled internally as seconds from 01.01.1970 00:00:00.
One day has 86400 seconds. Check help time/date.
gnuplot adds grid lines even on axes, it can cause unpleasant effects:
set logscale x
set xrange [0.01:100]
set xtics font ",12"
set x2tics font ",12"
set mxtics 10
set ytics font ",12"
set y2tics font ",12"
set grid xtics mxtics ytics lt 0 lw 3, lt 0 lw 0.5 behind
set grid
plot sin(x)
Especially if one then plots the above to eps, it looks like there are both logarithmically spaced and linearly spaced tics on the x-axis. Is there any nice way to get rid of the grid lines at axes? A workaround would be to make the axes thicker, but that is not the way I want. I really want to delete those grid lines.
To explain what I mean
The linearly spaced tics that are seen in the picture are actually the dotted grid, so it has nothing to do with tics...
As shown, there are both log and linear tics along x. That is because both the x axis and the x2 axis are contributing tics to both the top and bottom borders. You can turn that off with
set tics nomirror
Are you asking how to make the range of the tics smaller than the range of the axes? In the plot you show, that would be
set yrange [-1:1]
set ytics -0.8, 0.2, 0.8
set ytics add (-1 2, 1, 2)
The last command adds back explicit tics at y=-1 and y=1 without generating a corresponding grid line. See documentation for set xtics list
I saw this graph and only for the curiosity sake was wondering whether it was possible to plot figure with multiple y-axis as in the figure
Many thanks!
As andyras wrote, you can use the second y-axis if you only have two datasets. In this case, you also need to to
set ytics nomirror # remove the tickmarks of the left ayis on the right side
set y2tics # make the right y-axis 'visible'
If you want to plot more than one dataset, I would suggest to use multiplot. You can overlay several independent plots and put a unique offset to the y-axis for each of them.
However, you need to take care that the number of y-tics and y-tick positions is the same.
Plot:
(I did not care about the key here, this still needs adjustment)
Code:
set multiplot
set xrange[0:10]
# We need place to the left, so make the left margin 30% of screen
set lmargin screen 0.3
##### first plot
set ytics 0.4
set yrange[-1.2:1.2]
set ylabel "Voltage" textcolor rgb "red"
plot sin(x)
##### Second plot
set ytics 1
set yrange[-3:3]
set ytics offset -8, 0
set ylabel "Current" offset -8, 0 textcolor rgb "green"
plot 3*cos(x) linecolor 2
##### Third plot
set ytics 0.5
set yrange[-1.5:1.5]
set ytics offset -16, 0
set ylabel "Power" offset -16, 0 textcolor rgb "blue"
plot 3*sin(x)*cos(x) linecolor 3
unset multiplot
Yes, you can have two y axes for free, e.g.
plot x, x**2 axes x1y2
The axes specification lets you put things on x1y1, x2y1, etc. If you want more than two things plotted on the same y axes you have to normalize things yourself:
plot 'data1.dat' using 1:($2/MAX_1), \
'data2.dat' using 1:($2/MAX_2), \
'data3.dat' using 1:($s/MAX_3)
The variables MAX_X can be precalculated by using the stats command in gnuplot 4.6+, or you can put them in manually.
I have something like:
set grid xtics lt 0 lw 1 lc rgb "#a9a9a9"
set grid ytics lt 0 lw 1 lc rgb "#a9a9a9"
or the same without the "xtics" tag, and that works fine!
But if i add:
unset xtics
Then the grid disappears too :(
How can I only have a grid, without tics?
To hide the major ticks, you can use set tics scale 0:
set grid
set tics scale 0
plot x
Note, if one day you also want to use minor ticks and also hide them, you must use set tics scale 0,0.001.
If you only want to make the tic labels disappear then use set format:
set format x ""
set format y ""
set grid
plot x
If you don't want the tics either, then as far as I know you'd need a more complicated script, possibly using iterators and arrows, for example the following (you'll have to change the limits depending on your xrange and yrange and the arrow style to your liking):
unset xtics
unset ytics
set for [i=-5:5:5] arrow from i,-10 to i,10 nohead
set for [j=-5:5:5] arrow from -10,j to 10,j nohead
plot x