I'd like to know how to set the same number of ytics for both independent y and y2 axis using gnuplot, still using automatic scaling, so that the grid is well aligned.
Right now, as you can see below, there are 6 ticks for the y axis and 7 for y2, and the chart looks poorly readable.
As far as I know, Gnuplot lets you only to specify the increment defining the tics not their count. A slightly dirty workaround would be to first generate the plot into a "fake" terminal, remember the detected autoscaled ranges on each axis, calculate the required tics spacing, and finally generate the image with these settings.
N = 6
set term unknown
set ytics
set y2tics #setting y2tics affects autoscale
set ylabel 'MSE'
set y2label 'CE'
set grid
set format y "%.2f"
set format y2 "%.1f"
plot \
'mse.dat' u 1:2 axis x1y1 w l t 'MSE', \
'ce.dat' u 1:2 axis x1y2 w l t 'CE'
min_y1 = GPVAL_Y_MIN
max_y1 = GPVAL_Y_MAX
min_y2 = GPVAL_Y2_MIN
max_y2 = GPVAL_Y2_MAX
dy1 = (max_y1 - min_y1) / N
dy2 = (max_y2 - min_y2) / N
set ytics min_y1, dy1
set y2tics min_y2, dy2
set yr [min_y1:max_y1]
set y2r [min_y2:max_y2]
set term png enhanced
set output "test.png"
replot
This then produces (using a digitized approximation of your data):
Related
I try to plot two horizontal lines in a coordinate system using GNUPlot. The two lines represent average values in two datasets. Each dataset has the following constants: max, min and average. The same logic is to be applied to both datasets, so we can simply focus at one of them.
The output should be a 800 x 800 PNG image. They share the X axis, but their Y axis is different from each other in terms of the ranges' values and unit of measurement. Naturally, the numerical values of the two lines can vary arbitrarily. More precisely, I need to plot the two lines at, say, y = 300 and y = 500 in pixel coordinates, regardless of the value of average.
As far I as can tell, there is no way to tell GNUPlot to plot something at a specific pixel coordinate. However, I do believe it is possible to to it indirectly by adjusting the ranges to appropriate values. After poking around in GNUPlot, I managed to find proper values. When the proper range values are set, I think the datapoints in the set should be plotted nicely such that they fit into the graph. Now I need a general approach for any values.
I have the following GNUPlot script with arbitrary values for two horizontal lines:
set term png size 800, 800
set multiplot layout 1, 1
# Green line
min_green = 0
max_green = 50
set size 1,1
set ytics 20
set yrange [min_green : max_green]
avg_green = 22
plot avg_green linecolor rgb "green"
# Blue line
min_blue = 10
max_blue = 70
set size 1,1
set ytics 20
set yrange [min_blue : max_blue]
avg_blue = 14
plot avg_blue linecolor rgb "blue"
Use it like this: gnuplot -p script > plot.png
I need two procedure that looks something like this:
range_min = get_new_min_range(pixel_target_y, min, max, avg)
range_max = get_new_max_range(pixel_target_y, min, max, avg)
The ranges is put into set yrange in GNUPlot. The green line must be at y = 500 and the blue line must be at y = 300 (this is the pixel_target_y patameter). Any help is greatly appreciated!
Let me try to repeat in my words if I understood your question correctly:
You want to plot two datasets where the average (or mean) of each datasets have a fixed y-pixel-(or screen) position within the output graph (independent of data values and graph margins), correct?
For this you need the gnuplot variables GPVAL_TERM_YMIN and GPVAL_TERM_YMAX. In order to get these values you have to plot a dummy graph first. Then you need to do some calculations to get the proper range.
As you can see in the result from 3 different plots: the green and blue lines are independent of x-labels or graph titles.
Code:
### have average lines at fixed pixel position within the plot
reset session
myTermSizeX = 800
myTermSizeY = 800
set term pngcairo size myTermSizeX, myTermSizeY
myOutputFile = "Output.png"
set output myOutputFile
myFixY1 = 500
myFixY2 = 300
set title "Some graph title"
set xlabel "x-Axis title"
# create some test data
set table $Data1
plot '+' u 1:(rand(0)*50+40) smooth bezier
unset table
set table $Data2
plot '+' u 1:(rand(0)*40+10) smooth bezier
unset table
stats $Data1 u 2 name 'Data1' nooutput
stats $Data2 u 2 name 'Data2' nooutput
print Data1_min, Data1_mean, Data1_max
print Data2_min, Data2_mean, Data2_max
# dummy plot to get GPVAL_TERM_YMIN, GPVAL_TERM_YMAX
plot x
R_grph1 = real(myFixY1 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_grph2 = real(myFixY2 - GPVAL_TERM_YMIN)/(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)
R_data1 = (Data1_mean - Data1_min)/(Data1_max-Data1_min)
R_data2 = (Data2_mean - Data2_min)/(Data2_max-Data2_min)
if (R_data1 > R_grph1) {
Y1min = Data1_min
Y1max = (Data1_mean - Data1_min)/R_grph1 + Data1_min
}
else {
Y1max = Data1_max
Y1min = Data1_max - (Data1_max - Data1_mean)/(1-R_grph1)
}
print Y1min,Y1max
if (R_data2 > R_grph2) {
Y2min = Data2_min
Y2max = (Data2_mean - Data2_min)/R_grph2 + Data2_min}
else {
Y2max = Data2_max
Y2min = Data2_max - (Data2_max - Data2_mean)/(1-R_grph2)
}
print Y2min,Y2max
set yrange [Y1min:Y1max]
set ytics nomirror
set y2range [Y2min:Y2max]
set y2tics nomirror
set output myOutputFile # it seems you have to specify the output again
set key top center
plot \
$Data1 u 1:2 axes x1y1 w lp pt 7 lc rgb "red" ti "Data1", \
Data1_mean axes x1y1 w l lw 2 lc rgb "green" ti "Data1 mean", \
Data1_min axes x1y1 lt 0 not, \
Data1_max axes x1y1 lt 0 not, \
$Data2 u 1:2 axes x1y2 w lp pt 7 lc rgb "orange" ti "Data2", \
Data2_mean axes x1y2 w l lw 2 lc rgb "blue" ti "Data2 mean", \
Data2_min axes x1y2 lt 0 not, \
Data2_max axes x1y2 lt 0 not
set output
### end of code
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:
My data file looks like:
0 12.6 303.1 3.4577387486
120 10.5 278.9 2.8814489572
...
640 9.45 301.3 2.5933040615
My code looks like:
set xlabel "Tiempo (min)"
set ylabel "Corriente en el anillo (mA)"
set y2label "Flujo (fotones/s)"
set ytics nomirror
set y2tics
set tics out
set autoscale y
set autoscale y2
plot 'tan_time_curr_flux.dat' using 1:3 lc rgb 'black' pt 6 lw 2 w lp axes x1y1 title "Corriente", 'tan_time_curr_flux.dat' using 1:4 lc rgb 'blue' pt 5 lw 2 w lp axes x2y2 title "Flujo"
My plot looks like:
As you can see, there is a little offset, in x, between respective data points. It is more clear as x grows. For example the actual last point for flux (flujo) is 640,2.59. But the plot shows that point at 700, 2.59. How can I tell gnuplot not to add any offset to the second (blue) line?
Thanks a lot! :)
Ps If I plot time (col. 1) vs either flux (col. 4) or current (col. 3)...
The values for x stay normal. (i.e. There is no offset)
I guess that's a bug.
However, simply use axes x1y2 for your second plot, since you only have one x-value.
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))}
I seem to be having some difficulty finding the answer to this question online. The title is the basic question, but to be more specific I would like to have two x axes, one at the top of the figure that is dependent on the one at the bottom. However, this is not a simple relationship, i.e. x2!=5*x1 or something like that. The relationship is given by the data file itself. So to be more specific I have a file that looks something like this:
V T P
2.0 15.0 0.586
3.0 17.4 0.798
4.0 25.3 1.023
5.0 28.9 1.124
6.0 30.2 1.456
I would like to make a plot of T with respect to (wrt) P on the x1y1 axes and have T wrt V on the x2y1 axes. So the x1 axis would display the P range and x2 would display the V range in the corresponding places of x1, i.e. 0.586 on x1 axis would have 2.0 on x2 axis at the same place. Is this actually possible in Gnuplot or do I have to have a relationship with the two x axes to do this? Any help would be greatly appreciated. Thank you in advance.
Here is how you can achieve this. I first show you the script and the result, and later explain the steps:
reset
set xtics nomirror
set x2tics
set autoscale xfix
set autoscale x2fix
set xlabel 'P'
set ylabel 'T'
set x2label 'V'
plot 'data.txt' using 3:2 with linespoints ps 2 lw 2 title 'T wrt P', \
'' using 3:2:x2tic(1) axes x2y1 with points ps 2 lw 2 title 'T wrt V'
I first plot T wrt P on x1y1. Afterwards I plot T wrt V on x2y1 and use for this the range and tic positions of P, but use the V values as tic labels for the x2 axis. This gives a linear scale for P and adapts V accordingly.
In order for this to work you must use set autoscale xfix and set autoscale x2fix. This uses the exact ranges and does not expand an axis to the next major tics, which would be done only for the x axis, but not for the x2 axis, which has custom tics.
You could of course also reverse the process and use a linear scale for V and adapt the P tics. In any case, for the custom tics, which are placed with xtic() or x2tic, the numbers are used like they are formatted in the data file.
reset
set xtics nomirror
set x2tics 1
set autoscale xfix
set autoscale x2fix
set xlabel 'P'
set ylabel 'T'
set x2label 'V'
plot 'data.txt' using 1:2:xtic(3) with linespoints ps 2 lw 2 title 'T wrt P', \
'' using 1:2 axes x2y1 with points ps 2 lw 2 title 'T wrt V'
Here, the points are shown for both plot lines, to demonstrate, that they really coincide.
In order to have the one command only generating the xtics, one can use NaN for the y-value. And if only some of the custom tics should be labels, one needs an appropriate check in the x2tic call. Here, I set labels only for all even rows $0 is the current row number, starting from 0):
reset
set xtics nomirror
set x2tics
set autoscale xfix
set autoscale x2fix
set xlabel 'P'
set ylabel 'T'
set x2label 'V'
plot 'data.txt' using 3:2 with linespoints ps 2 lw 2 title 'T wrt P', \
'' using 3:(NaN):x2tic((int($0) % 2) ? '' : stringcolumn(1)) axes x2y1 t ''
With the result:
The best way: Plot your data as usual on the x1 and y1 axes, but place additional labels on the x2-axis with x2tic(column_number):
set x2tics
set xtics nomirror
plot 'data.txt' using 3:2:x2tic(1) w lp
see: Plot y1 in x1 with respect to x2 axis