Gnuplot, how to add a grid without ticks? - gnuplot

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

Related

Changing x axis scale on the fly

I am using the following gnuplot script to plot data from a set of files that are being added to constantly, once every five minutes:
set terminal x11 size 1900, 900
# The plot will not jump to the current window on update.
set term x11 1 noraise
set obj 1 rectangle behind from screen 0,0 to screen 1,1
set obj 1 fillstyle solid 1.0 fillcolor rgbcolor "black"
set grid lc rgb "white"
set key left top
set key textcolor rgb "white"
set border lc rgb "white"
set xtics textcolor rgb "white"
set xtics font "Times,12"
set xtics 1
set xlabel "Hours" textcolor rgb "white"
set ytics textcolor rgb "white"
set ytics font "Times,12"
set ytics 5
set ylabel "Hits" textcolor rgb "white"
set yrange [0:50]
set y2tics font "Times,10"
set y2tics 1 # Figures on the right side of the plot as well
set y2range [0:50]
plot "/tmp/Stats/One" using ($1)/(12.):2 title "One" with lines, "/tmp/Stats/Two" using ($1)/(12.):2 title "Two" with lines, "/tmp/Stats/Three" using ($1)/(12.):2 title "Three" with lines
pause 300
reread
The tics in the x axis correspond to hourly intervals. This works fine, until the script has been running for a day or so - at which point the tics in the x axis start to look a bit cluttered.
Would it be possible to change this dynamically from within the gnuplot script itself? The idea is that if the script has been running for more than, say, half a day, the tics in the x axis should be present once every two hours, rather than once every hour. And possibly other similar changes later on - e.g. after one week, there should be one tic per day. The text label would have to change consequently.
Is gnuplot capable of this, or are we talking about a shell script-driven approach instead? The latter is obviously possible, but it would be more cumbersome.
According to the SO-rule "no answer in comments"...
With the line set xtics 1 you are explicitely setting the xtic interval to 1,
hence not allowing gnuplot to automatically decide which tic interval seems reasonable.
Check help xtics.
Remove the line:
set xtics 1
or
replace it by
set xtics auto

gnuplot absolutely no y-markings at all and x-axis to front

I would like to create a color bar plot of a single variable that draws a box to the left in red if the variable is negative and green to the right if positive.
I am failing to get rid of the y axis completely. There shall be no marking of it whatsoever. Second the x-axis and tics is hidden behind the box. I need it visible. Third the plot of the line at 0 is really unnecessary as I have already drawn all I need but gnuplot wants a plot cmd with some sort of argument. I tried plot 0 lt bgnd but that left an ugly white line in my box. I guess I can live with that. Arrows at the ends of the x-axis would be nice, too.
This is the current state of the code. (the variable v will later come from the outside world as command line argument)
v= 7.3
if (v<0){boxcolor= 'red'}
if (v>=0){boxcolor= 'green'}
unset border
unset ytics
unset key
set yzeroaxis
set xzeroaxis
set xtics axis
unset ytics
set xrange [-10:10]
set object 1 rect from 0.0,-0.5 to v,0.5 back fillcolor rgb boxcolor
plot 0
Result currently:
You are probably looking for something like this:
Update: improved version
using graph and first coordinates for the arrow (check help coordinates), hence independent of the actual x-range.
using xzeroaxis (check help xzeroaxis)
Script:
### only x-axis with arrows
reset session
set border 0
unset ytics
v= 7.3
boxcolor = (v<0) ? 'red' : 'green'
set xrange [-10:10]
set xtics axis mirror
set xzeroaxis lt 1 lc "black"
set object 1 rect from 0.0,-0.5 to v,0.5 behind fillcolor rgb boxcolor
set arrow 1 from graph -0.03, first 0 to graph 1.03, first 0 heads filled
plot cos(x)
### end of script
Result:
Another answer: This one uses the built-in axis variants rather than an arrow:
set border 0
unset key
# In newer gnuplot versions there is a keyword "nodraw"
# Here we define a synonym that works with older versions also
hide = -4
set yzeroaxis lt hide lc hide
set xzeroaxis lt black
set tics front
unset ytics
set xtics axis
# define rectangle here
set object 1 rect from 0,-.5 to 5,.5 behind fs noborder fc "green"
#
plot 0 with lines lc "black"

gnuplot grid remove grid from axes lines

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

How to plot multiple y-axes?

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.

Gnuplot, Multiplot: Mix normal and parametric plot with a single legend box?

I create overlapping graphs in Gnuplot, because I mix normal and parametric plots (and also pm3d maps and parametric surfaces). This works fine mostly, except for one thing: If both plots have a title, the legends usually overlaps. A typical example looks like this:
#legends.gp
set term pngcairo enhanced color linewidth 1.5 dashed dashlength 1.4 rounded
set output "legends.png"
set title "legends test"
set multiplot
# make a box around the legend
set key box
set border 15 lw 1
# fix the margins, this is important to ensure alignment of the plots.
set lmargin at screen 0.15
set rmargin at screen 0.98
set tmargin at screen 0.90
set bmargin at screen 0.15
set xlabel "x"
set ylabel "sin(x)"
set xrange[0:2*pi]
set yrange[-1:1]
set grid x y
# add single tic at 0.62
set xtics add ("x0" 0.62)
# main plot command
plot sin(x) title "sinus"
# turn everything off
set format x "" #numbers off
set format y ""
set xlabel "" #label off
set ylabel ""
set border 0 #border off
unset xtics #tics off
unset ytics
unset grid #grid off
unset title #title off
#plot vertical line at 0.62
set parametric
plot 0.62,t ls 2 lw 2 title "parametric Line"
unset parametric
unset multiplot
My question is now, is there a simple, mostly automatic way to create a single legend for multiple plots?
P.S. Sorry, I ended up making the example file more complex than it had to be by showing some more features, that are hopefully helpful for future readers.
Here's a VERY dirty hack that works for me. change:
plot sin(x) title "sinus"
to:
plot sin(x) title "sinus",NaN w l ls 2 lt 2 title "parametric line"
Then plot the parametric line without a title (e.g. notitle instead of title "parametric line").
This works because gnuplot ignores NaN's when plotting -- Essentially the second thing we're plotting above just adds one element to the legend. I specify the linetype, etc to be the same as your parametric plot linestyle/type so that it shows up properly in the legend. To my knowledge, this is the only way to do something like this...
Of course, you could just edit it so that both are plotted parametrically and forgo the entire multiplot buisness...
set xrange [0:2*pi]
set yrange [-1:1]
set parametric
set trange [-10:10]
plot t,sin(t) title "Hello", 0.62,t title "World"
that's probably the "cleaner" solution...(but less fun working with gnuplot "magic")
From the gnuplot info manual:
To draw a vertical line from the bottom to the top of the graph at
x=3, use:
set arrow from 3, graph 0 to 3, graph 1 nohead

Resources