Related
I try to plot contour with only one cntrlabel per line but I do not succeed. I tried
set cntrlabel onecolor start 50 interval 10000000
and
set cntrlabel onecolor start 50 interval -1
but it does not work. Is there a mean to force 1 label per line ?
Moreover, I would like to shift the cntrlabel in order to prevent them to be overlayed (as observed on the top-left of the graph with the label 45, 50, 55, and 60). How should I do ?
The code used to obtain this graph is the following:
FILE = "data_sensibilite_correlation_phiFR_Tpfr_fusion_ordre"
set contour base
set cntrparam level discrete 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45, 50, 55, 60
#set cntrparam level incremental 2, 4, 60
set cntrlabel onecolor start 50 interval 10000000
set xrange [-10:10]
set yrange [0.55:0.95]
#set cbrange [0:20]
set style textbox opaque
unset key
set view map
set xlabel "{/Symbol e}_{/Symbol q} [%]"
set ylabel "T_b / T_{c} uncertainties on T_c"
set cblabel "{/Symbol e}_{{/Symbol F} cs} [%]"
set pm3d noborder
splot FILE u 1:2:3 w pm3d, \
FILE u 1:2:3 w l lc "black" nosurface, \
FILE with labels boxed
The data is available here: https://filesender.renater.fr/?s=download&token=c718b69b-1496-47db-9da4-21d48cf08aa4
Some time ago, I experienced the same issue.
Since your data is not accessible anymore, I am creating some test data in the script itself.
The problem is that you see too many labels although you are trying to limit them via
set cntrlabel {start <int>} {interval <int>}.
Let me try to explain: If you plot the contour line into a datablock you will notice that although some contour lines in the graph look like continuous lines, however, in fact, they are interrupted by empty lines in the data.
I guess this comes from the algorithm how gnuplot determines the contour lines. It seems, the more data points you have, the higher the probability that some contour lines are interrupted.
In the first graph below I made the interruptions visible by setting variable line color depending on pseudocolumn -1 (check help pseudocolumns). For the time being ignore the yellow line.
For example, the contour line for the level 8 consists of 5 pieces (i.e. 5 different colors).
This means when plotting the contour labels even if you set every 200 (or interval 10000000) you will get at least as many labels per contour line as many "broken" parts you have for that line.
Test Graph:
So, you could try to merge these interrupted lines which might be feasible, however, is not so easy because:
you have contour lines which should not be merged, e.g. level 15 on the left side and on the right side of the graph
you cannot easily connect the interrupted lines by simply removing the empty lines because the data points of the line parts are not in the right order
A different approach:
Hence, here is another "simple" idea, but not so simple to realize:
Define a parametric curve (yellow line in the above graph) which will intersect all the contour lines which you want to have labeled.
The script will determine the intersection points and will place a label at these positions.
The determination of the intersections is somewhat lengthy and the code is taken from here.
This procedure is certainly slow and not very efficient because it checks each yellow segment against all other segments.
Currently, the sampling of the yellow line needs to be high enough (here: N=21) such that each yellow segment will intersect with one contour line segment. The calculation time for the intersections can probably be shortened considerably if one yellow segment can intersect several contour line segments. Alternatively, the intersection lines could be filtered by level and then intersected. I will try these options asap.
If anyone has a more ideas to improve, please let me know.
Script:
### add contour labels nicely aligned
reset session
# create some test data
f(x,y) = ((4*x)**2 + (-y-5)**2)/10.
set samples 200
set isosamples 200
set table $Data
splot '++' u (x):(y):(f(x,y))
set table $Contours
unset surface
set contour
set cntrparam levels discrete 2,4,6,8,10,15,20,25,30,40
splot $Data u 1:2:3
unset table
set colorsequence classic
set style textbox opaque
set key noautotitle
set view map
# define parametric function for "line of labels"
xmin = -5.5
xmax = 5.5
N = 21
g(x) = 0.25*x**2 - 2
gx(t) = xmin + t*(xmax-xmin)/N
gy(t) = g(gx(t))
set table $LabelLine
plot '+' u (gx($0)):(gy($0)) every ::::N w table
unset table
set xrange [:] noextend
set yrange [:] noextend
# this plotting part can be skipped, it's just for illustration purpose
plot $Contours u 1:2:-1 w l lc var, \
'' u 1:2:3 every 200 w labels boxed, \
$LabelLine u 1:2 w lp pt 7 lc "yellow" noautoscale
pause -1
# some necessary functions
# orientation of 3 points a,b,c: -1=clockwise, 0=linear, +1=counterclockwise
Orientation(a,b,c) = sgn((word(b,1)-word(a,1))*(word(c,2)-word(a,2)) - \
(word(c,1)-word(a,1))*(word(b,2)-word(a,2)))
# check for intersection of segment a-b with segment c-d,
# 0=no intersection, 1=intersection
IntersectionCheck(a,b,c,d) = \
(Orientation(a,c,b)==Orientation(a,d,b)) || (Orientation(c,a,d)==Orientation(c,b,d)) ? 0 : 1
# calculate coordinates of intersection point, "" if identical points
M(a,b) = real(word(a,1)*word(b,2) - word(a,2)*word(b,1))
N(a,b,c,d) = (word(a,1)-word(b,1))*(word(c,2)-word(d,2)) - \
(word(a,2)-word(b,2))*(word(c,1)-word(d,1))
Intersection(a,b,c,d) = N(a,b,c,d) !=0 ? sprintf("%g %g", \
(M(a,b)*(word(c,1)-word(d,1)) - (word(a,1)-word(b,1))*M(c,d))/N(a,b,c,d), \
(M(a,b)*(word(c,2)-word(d,2)) - (word(a,2)-word(b,2))*M(c,d))/N(a,b,c,d)) : ""
# looping data segments for finding intersections
set print $Intersections
do for [i=1:|$LabelLine|-1] {
a = sprintf("%s %s", word($LabelLine[i], 1),word($LabelLine[i], 2))
b = sprintf("%s %s", word($LabelLine[i+1],1),word($LabelLine[i+1],2))
Line = ''
Intersection0 = ''
do for [j=1:|$Contours|-1] {
c = $Contours[j]
d = $Contours[j+1]
if (strlen(c)!=0 && strlen(d)!=0 && c[1:1] ne '#' && c[1:1] ne '#') {
if (IntersectionCheck(a,b,c,d)) {
Intersection1 = Intersection(a,b,c,d)
if ((Intersection0 ne Intersection1)) {
Level = word($Contours[j],3)
print sprintf("%s %s %s",Intersection0, Intersection1, Level)
}
Intersection0 = Intersection1
}
}
else {Intersection0 = ''}
}
}
set print
set palette rgb 33,13,10
plot $Data u 1:2:3 w image, \
$Contours u 1:2 w l lc "black", \
$Intersections u 1:2:3 w labels boxed
### end of script
Result:
Here is a much simpler solution resulting in only one label per level, however, depending on your data you won't know where exactly the labels will be positioned.
The contour lines per level are separated by two empty lines. The different pieces within a contour line of one specific level might be separated into (sub-)blocks separated by a single empty line.
Now, you can address specific rows and sub-blocks via every (check help every). For example, if you only want to plot each second row of each first sub-block you can specify every ::1:0:1:0 (indices are zero-based). You need to play with these numbers, depending on your data and how many contour line breaks you have. However, most likely the labels will not be nicely aligned as in my other (much more complicated) answer. Furthermore, the labeling will be only once per level, i.e. no labels on the left side of the plot in the example below.
Script:
### add contour labels, only one per level
reset session
# create some test data
f(x,y) = ((4*x)**2 + (-y-5)**2)/10.
set samples 200
set isosamples 200
set table $Data
splot '++' u (x):(y):(f(x,y))
set table $Contours
unset surface
set contour
set cntrparam levels discrete 2,4,6,8,10,15,20,25,30,40
splot $Data u 1:2:3
unset table
set colorsequence classic
set style textbox opaque
set key noautotitle
set view map
set xrange [:] noextend
set yrange [:] noextend
set tics out
set palette rgb 33,13,10
plot $Data u 1:2:3 w image, \
$Contours u 1:2 w l lc "black", \
$Contours u 1:2:3 every ::1:1:1:1 w labels boxed
### end of script
Result:
I have a simple data I want to plot as 3D plot (3 columns divided by a comma):
33.26,0.0000001,1
67.02,0.0000010,2
101.64,0.0000100,3
137.53,0.0001000,4
175.06,0.0010000,5
214.59,0.0100000,6
256.47,0.1000000,7
301.09,1.0000000,8
348.78,10.0000000,9
399.92,100.0000000,10
454.87,1000.0000000,11
513.99,10000.0000000,12
577.65,10000.0000000,13
646.22,10000.0000000,14
720.05,10000.0000000,15
799.51,10000.0000000,16
884.96,10000.0000000,17
976.77,10000.0000000,18
1075.29,10000.0000000,19
1180.89,10000.0000000,20
1293.92,10000.0000000,21
1414.77,10000.0000000,22
1431.83,10000.0000000,23
1449.15,10000.0000000,24
1466.97,10000.0000000,25
1485.79,10000.0000000,26
1505.97,10000.0000000,27
1527.88,10000.0000000,28
1551.87,10000.0000000,29
1578.3,10000.0000000,30
1607.56,10000.0000000,31
1639.98,10000.0000000,32
1675.95,10000.0000000,33
1715.82,10000.0000000,34
1759.96,10000.0000000,35
1808.72,10000.0000000,36
1862.49,10000.0000000,37
1921.6,10000.0000000,38
1986.44,10000.0000000,39
2057.35,10000.0000000,40
2134.71,10000.0000000,41
2218.87,10000.0000000,42
2310.2,10000.0000000,43
2409.06,10000.0000000,44
2515.83,10000.0000000,45
I wrote a simple script to plot the above data:
#!/usr/bin/gnuplot
set palette rgbformulae 33,13,10
set datafile separator ","
set terminal postscript eps size 10.5, 5.62 enhanced color font 'Helvetica,20' linewidth 2
set output 'test.eps'
set xlabel "time [s] (no operation)" offset -4, 0, 0
set xtics left offset 0,-0.3 rotate by 45 right
set xrange [0:400]
set ylabel "ranges" offset 2, 0, 0
set ytics left offset 0,-0.5
set zlabel "devices" offset -4, 0, 0
set zrange [0:50]
set autoscale
set title " "
set key inside left top;
set dgrid3d 30,30
set hidden3d
set style line 1 linecolor rgb '00FF00' linetype 4 linewidth 1
splot "data.csv" u 1:2:3 title "" with lines palette
And my output:
As you all can see, the output image (or, I should say), the x,y and z ticks on axis x,y, and z are not enough detailed. It is hard to say that the output image was plotted with this data.
Is there a way that would let me manipulate the x,y, and z ticks, to be taken from file, in some elegant way?
I also would like the image to be more readable with new x,y, and z ticks, so I think that the 10000.0000000 value should appear only once, when it appeared for the first time in data file.
Thank you.
Not exactly an answer to your question, and it is my personal opinion, but you might be interested in the ideas:
The data seems not to be grid data, so I would not use a surface plot of any kind.
Plotting only the datapoints in 3d does not give a useful picture, it is only a single line somewhere in space. I would try to use a 2D plot which contains the height information as color.
I would use a logscale for the y-axis.
This leads to the following script:
set terminal pngcairo
set output 'test.png'
set datafile separator ","
set palette rgbformulae 33,13,10
# Set margins to keep colorbox label inside the picture
set lmargin screen 0.12
set rmargin screen 0.85
set xlabel "time [s] (no operation)"
set ylabel "ranges"
set cblabel "devices"
unset key
set yrange [1e-8:1e5]
set ytics format "1e%+T"
set logscale y
set view map
set cbrange [0:50]
set zrange [0:50]
splot "data.csv" u 1:2:3 w p pt 7 palette ,\
"data.csv" every 5::4 u ($1+0):($2/3):(0):($3 != 30 ? 3 : "") with labels
It also prints the z-labels of some datapoints, skipping 30 for spacing reasons.
This is the result:
I want to create a streamline like arrow lines in Gnuplot,I already have the data points that I needed, so I think my problem is not the same as this post says and different from this post because I have already obtain the data needed for stramlines.
What I have done is like this:
So the red lines are vectors show flow field and green line is streamlines to guide the readers the direction of the flux. And all the large blue arrows are my aim to be plotted in GNUPLOT. I have kown how to plot middle arrows as this post has shown but what code I need to do if I want to plot more arrows along the lines?
To be more detailed, How can I plot like this:
I supply my data file here :
velocity.txt is for vector flow field data as "index,X,Y,vx,vy,particle-numbers"
line.txt is for streamline data as "X,Y"
and My gnu file is bleow:
set terminal postscript eps size 108,16 enhanced font "Arial-Bold,100"
set output 'vector.eps'
unset key
set tics
set colorbox
set border 0
set xtics 2
#set xlabel 'x'
#set ylabel 'y'
set xrange [0:108]
set yrange [0:16]
#set cbrange [0:40]
set nolabel
set style line 4 lt 2 lc rgb "green" lw 2
plot 'velcoity.txt' u 2:3:(250*$4):(250*$5) with vectors lc 1,'line.txt' u 1:2 ls 4
Thank you!
To plot arrows along a line you can again use the vectors plotting style like you do already for the stream field.
But to get a proper plot you must consider several points:
Usually gnuplot limits the size of the arrow heads to a fraction of the arrow length. So, if you want to plot a continuous line with arrows heads, the arrows themselves should have a very short length. To avoid downscaling of the arrow heads, use the size ... fixed option, which is available only since version 5.0
You have only the trajectory, x and y values, of the line. To extract the arrow direction, the simplest approach would be to use the difference between two neighbouring points (or at a distance of two or three points).
You can extract these differences in the using statement. As pseudo code, one could do the following:
if rownumber modulo 10 == 0:
save x and y values
else if rownumber modulo 10 == 1:
draw arrow from previous point to current point, only with a head
else
ignore the point.
Putting this pseudo-code in the using statement gives the following:
ev = 10
avg = 1
sc = 0.1
plot 'line.txt' u (prev_x = (int($0)%ev == 0 ? $1 : prev_x), prev_y = (int($0)%ev == 0 ? $2 : prev_y), int($0)%ev == avg ? $1 : 1/0):2:(sc*(prev_x-$1)):(sc*(prev_y-$2)) w vectors backhead size 2,20,90 fixed ls 4
To make things more flexible, I introduced some variables: ev tells you the difference count between two arrows heads, avg the distance between two points used to calculate the arrow direction, and sc the length of the arrow shaft.
As further improvement you can use the length of the stream field arrows to colour the stream field vectors. This gives the following script
reset
unset key
set tics
set colorbox
set border 0
set xtics 2
set autoscale xfix
set autoscale yfix
set autoscale cbfix
set style line 4 lt 2 lc rgb "green" lw 2
ev=30
avg=3
sc=0.1
field_scale=500
plot 'velcoity.txt' u 2:3:(field_scale*$4):(field_scale*$5):(sqrt($4**2+$5**2)) with vectors size 1,15,45 noborder lc palette,\
'line.txt' u 1:2 ls 4 w l,\
'' u (prev_x = (int($0)%ev == 0 ? $1 : prev_x), prev_y = (int($0)%ev == 0 ? $2 : prev_y), int($0)%ev == avg ? $1 : 1/0):2:(sc*(prev_x-$1)):(sc*(prev_y-$2)) w vectors backhead size 2,20,90 fixed ls 4
With the result (qt terminal):
I have an infrared spectrum for a compound of interest that I would like to plot, and I have a spectrum.dat file with all of the data points. It is of the form:
# X Y
300 100
301 100
302 99
303 70
...
3999 98
4000 100
I would like to plot this using an x axis typical of IR spectra, but I am having trouble doing so. If you are unfamiliar, this is what a typical IR spectrum might look like (aside from the labels on the graph itself). Notice that the x-axis is reversed, and that it abruptly doubles its scaling above 2000 units (reciprocal centimeters). Is there a way to coerce Gnuplot into plotting my data this way? I so far have managed to come up with the following script:
# Make an SVG of size 800x500
set terminal svg size 800,500 fname 'CMU Sans Serif' fsize '10'
set output 'ir.svg'
# Color definitions
set border linewidth 1.5
set style line 1 lc rgb '#a0a0a0' lt 1 lw 2 pt 7 # gray
# Format graph
unset key
set xlabel 'Wavenumbers'
set ylabel 'Transmittance'
set xrange [4000:300]
# Plot data
plot 'spectrum.dat' with lines ls 1
This reverses the x-axis nicely, but I can't figure out how to change the scaling in such an unusual way.
As a chemist I am motivated to answer...
As far as I know gnuplot doesn't easily allow for arbitrary axis scaling (unless anyone has bright ideas about how to use set link). My strategy in this kind of situation is to plot the two halves separately and have them join seamlessly:
#!/usr/bin/env gnuplot
set terminal png size 800,500
set output 'ir.png'
set xlabel 'Wavenumbers' offset 20
set ylabel 'Transmittance'
set tics out nomirror
set key bottom right
set bmargin 4
set yrange [0:1]
set multiplot layout 1,2 title 'IR Spectrum of Cholesterol'
# left half of plot
set xrange [4000:2000]
set rmargin 0
set border 7
plot 'cholesterol.txt' notitle
# right half of plot
set xrange [1999:300]
set lmargin 0
set rmargin 2
set border 13
unset xlabel
unset ylabel
unset ytics
plot 'cholesterol.txt' title 'Cholesterol'
unset multiplot
My one quibble is that the 2000 is written twice and looks bolder on my screen, but I will leave fidgeting with the tics to you.
andyras' answer is a nice one, this is an arguably simpler (more elegant :-P) solution in terms of layout options. This should also be a more universal solution. If there are not too many tics (read below the figure if there are too many), then this could be done scaling the curve itself beyond 2000, and then adding all the tics by hand. Since I don't have IR spectrum data available I will use the dummy file "+" and plot log(x) from 4000 to 500:
xmax=4000 ; xmin = 500
pivot = 2000 ; rescfactor = 2.
rescale(x) = (x >= pivot ? x : pivot + rescfactor*(x-pivot))
set xrange [rescale(xmax):rescale(xmin)]
set xtics ("4000" 4000, "3000" 3000, "2000" 2000, \
"1500" rescale(1500), "1000" rescale(1000), "500" rescale(500))
plot "+" u (rescale($1)):(log($1)) w l
In your case you just substitute log($1) by 2 or whatever you're plotting.
In newer versions of gnuplot (starting from 4.4) adding the tics can be done automatically using a loop:
xmax = 4000 ; xmin = 500 ; step = 500
set xtics (sprintf("%i",xmax) rescale(xmax)) # Add the first tic by hand
set for [i=xmin:xmax-step:step] xtics add (sprintf("%i",i) rescale(i))
Starting from gnuplot 4.6 also a different for construction can be made using do for:
do for [i=xmin:xmax-step:step] {set xtics add (sprintf("%i",i) rescale(i))}
Title is pretty self explanatory but here is a picture of what I'd like to do. I'm having a tough time figuring out if its even possible.
Plot borrowed from:
Evaluation of geochemical background levels around sulfide mines – A new statistical procedure with beanplots. Gusstavason et al. 2012.
Doing the plot in exactly this orientation could be very cumbersome, if possible at all.
My suggestion is to plot everything with the usual orientation (i.e. having the 'sediments' axis as x-axis, or rather as x2-axis), rotate all labels a bit and finally rotate the complete output by 90 degree (pdf file with e.g. pdftk etc).
With this you can use any plot style as usual. In the script below I just show you how to plot the violet and yellow filled curves (using pseudo data) for two different data sets. Adding the other peaks should be straight forward (plot the bars with e.g. boxes or vector plotting style).
In order to have distinct ytics for the different plots, I associated a certain y-value with a certain plot, 1=Water, ..., 4=Gyttja).
Putting all toghether gives the following script:
reset
set terminal pdfcairo linewidth 2
outfile='bean'
set output outfile.'.pdf'
set encoding utf8
set x2range [0.5:9000]
set logscale x2
set x2tics (1, 5, 10, 50, '' 100, 500, '' 1000, 5000) out
set x2label 'mg/kg (sediments), µg/L (water)'
unset xtics
set yrange[0.5:4.5]
set ytics ('Water' 1, 'Minerogenic' 2, 'Peat' 3, 'Gyttja' 4) center rotate by -90 out
set label at graph 0.95, graph 0.05 right rotate by -90 'Nickel' font ',20' front
# cover possible data overlapping with the label
set object rectangle from graph 0.9, graph 0 to graph 1,graph 0.2 fillcolor rgb 'white' fillstyle solid noborder front
unset key
set macros
fs1="fillcolor rgb '#fc9e00' linewidth 2 fillstyle solid border lt -1"
fs2="fillcolor rgb '#9119f7' linewidth 2 fillstyle solid border lt -1"
# use pseudo data
set samples 500
plot '+' using 1:(4-0.3*exp(-(($1-10)/5.0)**4)) axes x2y1 with filledcurves y1=4 #fs1,\
'' using 1:(4+0.2*exp(-(($1-70)/50.0)**4)) axes x2y1 with filledcurves y1=4 #fs2,\
'' using 1:(1-0.4*exp(-(($1-5)/2.0)**2)) axes x2y1 with filledcurves y1=1 #fs1,\
'' using 1:(1+0.1*exp(-(($1-30)/20.0)**2)) axes x2y1 with filledcurves y1=1 #fs2
set output
system(sprintf('pdftk %s.pdf cat 1W output %s-rot.pdf', outfile, outfile))
system(sprintf('pdftocairo -r 150 -png %s-rot.pdf', outfile))
This gives (conventional and rotated output side-by-side) with 4.6.3:
Some stuff is required for the pseudo data. For a real data file, the plotting line looks a bit differently. The different plots have a separation of 1 in y-direction, so you must scale your data accordingly (done here manually with a scaling factor sc):
sc = 5.1
plot 'datafile.txt' using 1:(4 + $2/sc) axes x2y1 with filledcurves y1=4 #fs1
You can of course also do the scaling automatically, by extracting some minimum/maximum values using the stats command.