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"
Related
In gnuplot, we can set the tick scale, but this only affects the length of the ticks.
How can we change the width of the ticks?
I don't see any option in the tags of xtics or ytics that would set the appropriate width.
As theozh says, the tics are drawn using the 'border' line type. If necessary I suppose you could turn off the border, set the border linetype to whatever you want the tics to be, and then use a graph-sized rectangle to replace the border.
set border lc "black" lw 0.25 # thin lines for the tics
set border 0 # don't draw the normal border
set tics scale 3.0 # longer than usual tics
set obj 1 rectangle from graph 0,0 to graph 1,1
set obj 1 lc "black" lw 2.5 # heavy line for the "border"
set obj 1 fillstyle empty
set grid x y
plot sinc(x)
Check help border. My guess would be that the tic width can probably not set independently of the border. Well, you could play with multiple graphs with different borders and tics on top of each other. Check help multiplot.
Code:
### set tic width
reset session
set border lw 3
set xtics scale 4
set ytics scale 1
plot x
### end of code
Result:
I'm using gnuplot to generate some plots with an x axis ranging from 0 to 20. Is it possible to set the color of some tics or axis numbers to a different color from the standard black?
I only found a way to change the color of the all the numbers in the x axis red with set xtics textcolor rgb "red".
What I need is to be able to change the color of the tic or number at x=0,3,6,... to red and all the others should stay black. Is this possible with gnuplot?
The color of the tic marks is set by the border color, so one can do something like this:
reset
set multiplot
set xrange [-5:5]
set xtics -5,1,0 # these tic marks will show up in black (the default border color)
set yrange [] writeback # save auto-generated y range for later
plot sin(x)
set border 0 linecolor "red" # change border color, and turn off drawing of the border
set xtics 1,1,5 textcolor "blue" # these tic marks will show up in the new border color, and we can specify the color of the labels
unset ytics # we don't want to display the y tics again
set yrange restore # use same range for y axis as before
unset key
plot NaN
unset multiplot
This solution also uses multiplot, but uses the second plot only to draw tic marks and labels whose color is different from the default black. It is important that the two plots have the same ranges and margins.
This might be cheating a bit, just create the plot twice with different xtics command on top of each other:
set xrange [0:10]
set multiplot
set size 1,1
set origin 0,0
set xtics 1 textcolor rgbcolor "green"
plot sin(x)
set size 1,1
set origin 0,0
set xtics 1, 2 textcolor rgbcolor "red"
plot sin(x)
unset multiplot
seems to work for me (gnuplot: Version 5.2 patchlevel 2 last modified 2017-11-15)
in the following code I try to achieve the following:
1) As can be seen, the description 'talkative' does not appear. Is this really the y2axis? Or does y2axis not account for splot but only for plot?
Or do I have to label a border ?
2) How do I get tics only at the axis of interest?
3) A small detail: A close look reveals that the blue dot is transparent, but not the red and green dot. How to also make the blue dot filled?
set ticslevel 0
set xrange [0:1]
set yrange [0:1]
set zrange [0:1]
set object 1 polygon from \
0, 0, 1 to \
0, 1, 0 to \
1, 0, 0 to \
0, 0, 1
set view 56, 77
set style line 1 lc rgb "blue" pt 7 ps 2
set style line 2 lc rgb "red" pt 7 ps 2
set style line 3 lc rgb "green" pt 7 ps 2
unset xtics
unset ytics
unset ztics
set border 1+2+16
set xlabel "listening" offset +4,0
set y2label "talkative"
set zlabel "sleeping" rotate offset -1,-2
splot \
'-' with points ls 1 title "",\
'-' with points ls 2 title "",\
'-' with points ls 3 title ""
1 0 0
e
0 1 0
e
0 0 1
e
Unfortunately not a solution, but some explanations:
Yes, splot doesn't have an x2 and y2 equivalent. You can set a ylabel and shift it:
set ylabel 'talkative' offset graph -1.5
You mean getting tics only at the 'talkative' axis? That isn't possible out-of-the-box. You can have tics only on the y-axis (which gnuplot selects to always the the one in the front), or on both front and back y-axis.
That seems to be a bug to me. Usually you can set the border to be behind everything with set border back, but that doesn't work in this situation (tested with 4.6.5 and 5.0RC2). And it's also strange that it involves only one axis.
There are no second axes for splots. You can see this when you rotate the plot with the mouse: The drawn border, the tics and the label are jumping from one side to the other. You can also see the intention: If the volume is quite full of plot data, the axes in the rear would be hidden by your plots. So as soon as this can happen, gnuplot switches to axes in the front. There seems to be no way around.
(btw: when drawing the full border box, you have 4 axes in each direction. And there is definitively no y3tics or y4tics...)
set xtics nomirror
It's not really transparent, as only the "front border" is visible on the dot. The other lines (and also tick labels) are behind the dot. set border back should help, but it doesn't. The setting has an effect only for plot , not for splot. The data is plottet between front and rear axes, and so, the data hides the rear axes, but the front axes are drawn in front of the data.
In general, this all makes sense, but for special cases, there are options to tweak the plot as you want. However, only for plot ...
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
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