Gnuplot Vertical Dashed Line - linux

I would like to add a gray dashed vertical line on my graph with GNUPLOT.
My terminal is;
set terminal postscript portrait enhanced color dashed lw 1 "DejaVuSans" 12 size 6,6
set output "pic.ps"
set xrange [30:110]
plot 'smth.txt' w lines ls 7 t ""
I have looked into several ideas around but there seems to no explanation about how to get a dashed gray line vertically at let's say x = 40 (the line gonna be parallel to y-axis)

The key is to use the set arrow command and remove the head to make it a line:
set arrow from 40,0 to 40,100 nohead lc rgb 'red' dt 2
Your graph does not specify any y value, so adjust it to a "larger than needed" value.
For dash type (dt), checkout p.42 of manual. It is terminal-dependent (I didn't tried with postscript), but you can adjust as needed.

Related

How to make dashed grid lines intersect making crosshairs in gnuplot?

I'm plotting some data and I want to use dashed grid lines.
Any dashed grid line would suffice, but I prefer a "long dash, short dash, long dash" format.
For example, given the following code
set grid lc rgb "#000000" lt 1 dt (50, 25, 20, 25)
plot x**2
I get this result
But I would rather the grid lines intersection to happen always at the middle of two dashes, like this
If I could make horizontal grid lines different to vertical grid lines and I could add some offset to each one, then I'd imagine there's a way to accomplish this. But I can't seem to do that either.
It looks like gnuplot cannot have two different dashstyles for x-grid and y-grid.
One workaround I see currently is to plot two identical plot on top of each other. One with appropriate x-grid lines and the other with appropriate y-grid lines.
If you want a dash pattern with proportions of (50-25-20-25), this correspond to (25-25-20-25-25-0) or (5-5-4-5-5-0) between two tics.
Furthermore, the dash and gap length numbers, e.g. in dt (50,25,20,25), seem to be in a fixed relation to the graph size. The "empirical" factor is 11 with good approximation (at least for the wxt terminal which I tested under gnuplot 5.2.6).
Edit: actually, the code below gives different results with a qt terminal. And it's not just a different factor. It's more complicated and probably difficult to solve without insight into the source code. So, the fact that the following seems to work with wxt terminal (maybe even just under Windows?) was probably a lucky strike.
With this you can create your dash lines automatically resulting in crosshairs at the intersections of the major grid lines.
Assumptions are:
your first and last tics are on the borders
you know the number of x- and y-intervals
You also need to know the graph size. These values are stored in the variables GPVAL_TERM..., but only after plotting. That's why you have to replot to get the correct values.
This workaround at least should give always crosshairs at the intersection of the major grid lines.
Edit 2: just for "completeness". The factors to get the same (or similar) looking custom dashed pattern on different terminals varies considerably. wxt approx. 11, qt approx. 5.6, pngcairoapprox. 0.25. This is not what I would expect. Furthermore, it looks like the factors slightly depend on x and y as well as graph size. In order to get "exact" crosshairs you might have to tweak these numbers a little further.
Code:
### dashed grid lines with crosshairs at intersections
reset session
TERM = "wxt" # choose terminal
if (TERM eq "wxt") {
set term wxt size 800,600
FactorX = 11. # wxt
FactorY = 11. # wxt
}
if (TERM eq "qt") {
set term qt size 800,600
FactorX = 5.58 # qt
FactorY = 5.575 # qt
}
if (TERM eq "pngcairo") {
set term pngcairo size 800,600
set output "tbDashTest.png"
FactorX = 0.249 # pngcairo
FactorY = 0.251 # pngcairo
}
set multiplot
set ticscale 0,0
Units = 24 # pattern (5,5,4,5,5,0) are 24 units
# set interval and repetition parameters
IntervalsY = 10
RepetitionsY = 1
IntervalsX = 4
RepetitionsX = 3
# initial plot to get graph size
plot x**2
gX = real(GPVAL_TERM_YMAX-GPVAL_TERM_YMIN)/IntervalsY/Units/FactorY/RepetitionsY
gY = real(GPVAL_TERM_XMAX-GPVAL_TERM_XMIN)/IntervalsX/Units/FactorX/RepetitionsX
# first plot with x-grid lines
set grid xtics lt 1 lc rgb "black" dt (gX*5,gX*5,gX*4,gX*5,gX*5,0)
replot
unset grid
# second plot with y-grid lines
set grid ytics lt 1 lc rgb "black" dt (gY*5,gY*5,gY*4,gY*5,gY*5,0)
replot
unset multiplot
set output
### end of code
Result:
Not really. The closest I can think of is
set grid x y mx my
set grid lt -1 lc "black" lw 1 , lt -1 lc bgnd lw 16
set ticscale 1.0, 0.01
set mxtics 4
plot x**2 lw 2
But that leaves the vertical grid lines solid.

Histogram in GNUplot, each bar with a different color

I am trying to make a barplot with GNUplot, were each bar has a different color. I have found out in the manual that it can be done using lc rgbcolor variable.
My data folder looks like this,
ACB 0.106372
ASW 0.10909
BEB 0.110973
CDX 0.106577
CEU 0.102091
CHB 0.108829
CHS 0.110807
CLM 0.108803
My plot script is as follows,
set style histogram gap 2
set autoscale y
set style fill solid
set xtics rotate by 90 offset 0,-1.2
rgb(r,g,b) = 65536 * int(r*10) + 256 * int(g*10) + int(b*10)
set boxwidth .5
set output "Plot.eps"
plot "plot.tsv" using 2:xticlabels(1):(rgb($2,$2,$2)) with boxes lc rgb variable
I have used the rgb function that is shown in the GNUplot manual. But it shows the following error.
plot "plot.tsv" using 2:xticlabels(1):(rgb($2,$2,$2)) with boxes lc rgb variable
^
"#Plot.plt", line 18: x range is invalid
I can't figure out what the error means.
Please help.
Thanks in advance.
You are actually plotting with style "boxes", not "histograms". That probably is what you want, but whereas for histograms the x coordinate is implicit, for boxes you have to provide an x coordinate in the first column even if you're OK with the ordinal numbers 0,1,2,3,...
So
plot "plot.tsv" using 0:2:xticlabels(1):(rgb($2,$2,$2)) with boxes lc rgb variable
Oh, and you probably want to set the fillstyle also so that the colors are visible:
set style fill solid

How to add symbols representing point types in y labels

I want to add the symbols representing point types to y and y2 labels.
Here is the test code:
set key center top;
set ylabel "x";
set y2label "x^2";
plot x w lp pt 5 pi 5, x**2 w lp pt 7 pi 5 axes x1y2;
It gives:
I want the following:
Is there anyway to achieve this in Gnuplot?
I am using Gnuplot 4.6.5.
Thanks.
One option would be to use set label ... point pt 7 lt 2 to place a dot somewhere. But that needs a lot of tweaking to get the correct position. And that position would need to be adapted manually depending on the font, font size, canvas size etc.
If only the point shape is important, you can use the respective glyph from the unicode block: geometric shapes. This is also from where I copied the symbols to the script. You must of course also use a font which includes the respective glyphs (e.g. Arial Unicode or DejaVu) and a proper terminal (e.g. pdfcairo, pngcairo, wxt are all fine):
set terminal pngcairo enhanced font "DejaVu" size 600,300
set output 'square-circle.png'
set key center top
set encoding utf8
set ylabel "x {/*0.8 ■}"
set y2label "x^2 {/*0.8 ●}"
plot x w lp pt 5 pi 5, x**2 w lp pt 7 pi 5 axes x1y2;
Result with 4.6.5 is:

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.

Line styles on gnuplot 4.4

Well, I'm trying to plot this graphic:
gnuplot> plot exp(-x) t 'MB' w l lw 3, (exp(x)-1)**(-1) w l lw 3 t 'BE', (exp(x)+1)**(-1) w l lw 3 t 'FD'
and exporting with
set terminal latex
How, or what, I have to do to the graphics be plotted in different lines styles, as dotted and dashed?
In order to specify line styles check the documentation here. You can specify the line type, line color, line width, point type etc.
To specify a blue dotted line on an X11 terminal do
set style line 1 lt 0 lc 3
plot x**2 ls 1
Note that the styles also depend on the terminal you are using. To see what possibilities there are use the test page of the current terminal by running test in your gnuplot console.
Examples of different line styles can be found here and there.

Resources