x axis on top of graph in gnu plot - gnuplot

I'm able to put an x axis on the top of a graph in gnu plot via
set x2label "label" and
set x2tics
I have a column of data of data1 values and I'd like to plot these values multiplied by two on the upper x axis against data data2 in the lower x axis. Both data1 and data2 are in the same data file. Here is some sample data
data2 data1
20 1.2e-2
40 3.0e-3
60 1.4e-3
... ...
I'd like to plot 2*data1 on upper axis against data2 on lower axis. Preferably, I'd like to just put a tick mark on the 2*data1 axis for every data2 value. On the y axis I will plot some other quantity against data2 but all I want to ask about here is how to plot x2 versus x1.
Thanks!

still guessing what exactly you want. Maybe we'll find out faster with this example.
reset session
$Data <<EOD
# x2 x1 y
20 1.2e-2 1
40 3.0e-3 2
60 1.4e-3 3
EOD
set key top center
set xtics nomirror
set x2tics nomirror
plot $Data u ($1*2):3 axes x2y1 w lp pt 7 title "y-data vs. x2-axis", \
'' u 2:3 axes x1y1 w lp pt 7 title "y-data vs. x1-axis"
Result:
Edit:
You can link x1 and x2 axes via a function (in the example below conversion between nm and eV) and then set your x2tics as desired.
Graph1: corresponding "odd" values from x1,
Graph2: "even" values by given interval x2tics 0.2,
Graph3: manual setting of x2tics.
Example:
### linked axes x1, x2
reset session
set xlabel "Wavelength / nm"
set xtics nomirror
set x2label "Energy / eV"
set x2tics nomirror
set link x via 1239.8/x inverse 1239.8/x
set ylabel "Intensity / a.u."
set ytics 0.2
set samples 400
Spectrum(x) = exp(-(x-500)**2/(400))
set xrange[380:780]
set multiplot layout 3,1
set format x2 "%.2f"
plot Spectrum(x) w l title "Spectrum"
set format x2 "%.1f"
set x2tics 0.2
replot
set x2tics ()
myTics = "1.7 1.8 1.9 2.0 2.1 2.3 2.5 2.7 3.0"
do for [i=1:words(myTics)] { set x2tics add (word(myTics,i) real(word(myTics,i))) }
replot
unset multiplot
### end of code
Result:

Related

plotting data with too many zero on x axis

I have this data1.dat data that I'd plot using gnuplot.
x y
0.007 3.09216
0.008 3.60607
0.009 5.86643
then I tried to plot this with this plot script
set terminal svg size 400,300 enhanced fname 'arial'
set output 'out2.png'
set xlabel 'diameter'
set ylabel 'number density'
set title 'density number plot'
plot "data1.dat" using 3:4 title "" with lines
and the output in the x axis was like this
how to convert the number data on the x axis so it only shows only for example 7 or 8 only? this is an example and probably my data would getting bigger, so i can't change the data on data1.dat one by one in the x column, thanks in advance
Apparently, gnuplot's tic algorithm doesn't take the length of the ticlabels into account. That's why the numbers are overlapping.
You can do the following:
set the xtic increment manually to avoid overlap, e.g. set xtic 0.0005
multiply your values by a factor, e.g. 1000, especially if you have meters you can display millimeters, i.e. ($1*1000) which is identical to (column(1)*1000).
Code:
### avoid overlap of tic labels
reset session
$Data <<EOD
x y
0.007 3.09216
0.008 3.60607
0.009 5.86643
EOD
set ytics 1
set multiplot layout 3,1
set xlabel 'diameter / m'
plot $Data using 1:2 w l notitle
set xlabel 'diameter / m'
set xtic 0.0005
plot $Data using 1:2 w l notitle
set xlabel 'diameter / mm'
set xtics 0.2
plot $Data using ($1*1000):2 w l notitle
unset multiplot
### end of code
Result:

Align ylabel in gnuplot multiplot

Is there a way to align vertically the y labels of in gnuplot's multiplot so that they are below each other? The problematic example is below (the red line is annotation showing the problem):
set xrange[0:10]
set multiplot layout 2,1 margins 0.1,0.95,0.1,0.95 spacing 0,0
set ylabel "y1\nlabel"
set ytics
set format y "%.2f"
plot -1000*cos(x)**2 w l lt 4 lw 2
set ylabel "y2\nlabel"
set format y "%.1f"
plot cos(x) w l lt 5 lw 2
unset multiplot
which generates:
And I would like to automatically position the labels such, that the red annotated line "touches the same text". Note that I am really interested in automatic way or more correct way that a workaround using a trial and error with set ylabel "lable" offset -x,0
As you already noted, you can set an offset to the x- or y-label (check help xlabel), however, no absolute position.
This you can do with setting your own label. You can set the position relative to the screen and/or relative to the graph. gnuplot keeps these values for the second plot, no need to specify again. Check help label.
Check the following example:
Code:
### align ylabels in multiplot
reset session
set xrange[0:10]
set multiplot layout 2,1 margins 0.15,0.95,0.1,0.95 spacing 0,0
set format x ""
unset ylabel
set label 1 "y1\nlabel" at screen 0.02, graph 0.5 center rotate by 90
set ytics 200
set ytics add ("" -1000) # remove -1000
set format y "%.2f"
set grid x,y
plot -1000*cos(x)**2 w l lt 4 lw 2
set format x
set label 1 "y2\nlabel"
set ytics 0.4
set format y "%.1f"
plot cos(x) w l lt 5 lw 2
unset multiplot
### end of code
Result:

using x2y2 axes in a heatmap with gnuplot

Creating a heatmap with gnuplot, I want to have the tic labels read from the datafile on top and right of the heatmap, but setting axes x2y2 doesn't work as expected.
Example data file:
. sample1 sample2 sample3
gene1 10 0 17
gene2 11 2 21
gene3 9 27 0
Using
plot "data.csv" matrix rowheaders columnheaders with image
I get this plot, which is fine:
However, if I want to have the tic labels on the x2 and y2 axes, using i.e.
unset xtics
unset ytics
set x2tics
set y2tics
plot "data.csv" matrix rowheaders columnheaders using 1:2:3 axes x2y2 with image
gnuplot uses numbers instead of my labels:
Is there a way to attach the tic labels read from the file to the x2 and y2 axis?
You have to set x2label and y2label:
set x2label "Some label"
set y2label "Some other label"
plot sin(x) notitle axes x2y2
gives
If you also want to move the tic marks to the upper and right side, then you have to enable them for the x2 and y2 axes and disable them for x and y:
set x2label "Some label"
set y2label "Some other label"
unset xtics
unset ytics
set x2tics
set y2tics
plot sin(x) notitle axes x2y2
gives
Update
Thanks for editing the question. It looks like your main question is whether you can use the labels read from the data file for the x2label and y2label. I am not sure if that is possible; it seems that the rowheaders and columnheaders options always refer to the first coordinate system.
As a workaround you can stick to xlabel/ylabel, and move them to the other side of the graph. This might require playing with the margins a little:
set tmargin 2
set lmargin 0.5
set rmargin at screen 0.75
set colorbox user origin screen 0.88, graph 0 size screen 0.05, graph 1
set xtics offset 0, graph 1.07
set ytics offset graph 1.15, 0
plot "data.csv" matrix rowheaders columnheaders with image
gives

polar plot in gnuplot putting ytics at the center of the plot

I want to put xlabel and ylabel beside their axis at right and above respectively. then I want to set ytics beside the axis not at the left.
Here is my data.
#!usr/bin/gnuplot
unset border
set xzeroaxis
set yzeroaxis
set polar
set nokey
set xtics 0.05
set ytics 0.01
set autoscale fix
set label " k=0.2 " at 0.012, 0.0095
set label " k=0.3 " at 0.022, 0.015
set label " k=0.4 " at 0.032, 0.025
#unset border
#set notics
unset xtics
set ytics
set xlabel "kx"
set ylabel "ky"
plot "T1.txt" u 1:2 w l ,"T2.txt" u 1:2 w l , "T3.txt" u 1:2 w l
It sounds like you want to produce the following plot
Gnuplot has 2 x axes and 2 y axes names x1, x2, y1, and y2. When you just specify the x or y axis, you are actually working with the x1 and y1 axis. The other two are opposite. So to get the labels as you want, we just use the x2 and y2 labels
set x2lab "kx"
set y2lab "ky"
As far as the y-axis marks, gnuplot can put them on the border or on the axis (see help xtics, ytics are similar). Thus, to put the ytics on the axis itself, we just issue
set ytics axis

How can I divide the axis range? [duplicate]

I have a histogram with some small values and some very big values. How can I break the y-axis in two parts?
EDIT:
gnuplot sample:
set style histogram columnstacked
set style data histograms
set key autotitle columnheader
plot for [i=2:6] 'test.dat' using i
test.dat:
action device1 device2 device3
load 200 203 190 2 4
process 3 9 6 7 3
save 4 2 7 4 3
My answer is based on the example and comment on this website. In essence, you are looking for a broken y-axis, which can be achieved using the multiplot option:
reset
unset key
bm = 0.15
lm = 0.12
rm = 0.95
gap = 0.03
size = 0.75
kk = 0.5 # relative height of bottom plot
y1 = 0.0; y2 = 15.0; y3 = 180.0; y4 = 220.0
set style histogram columnstacked
set style data histograms
set key autotitle columnheader
set style fill solid 1.0 border -1
set multiplot
set border 1+2+8
set xtics nomirror
set ytics nomirror
set lmargin at screen lm
set rmargin at screen rm
set bmargin at screen bm
set tmargin at screen bm + size * kk
set yrange [y1:y2]
plot for [i=2:6] 'test.dat' using i
unset xtics
unset xlabel
set border 2+4+8
set bmargin at screen bm + size * kk + gap
set tmargin at screen bm + size + gap
set yrange [y3:y4]
plot for [i=2:6] 'test.dat' using i
unset multiplot
Remarks:
The first block of code specifies your plot size, as well as the two min/max for the y-axis range.
The second block of code sets your histogram style. I suggest you used filled columns (else, you might get the wrong colors where the plot is cut off by the y-range).
The third block starts you multiplot environment and sets up the lower plot (4th block).
The fifth block of code defines you upper plot.
To customize your plot, change the parameters in the first block (bm, lm, rm, gap, size, kk and yi)
And this is the result:

Resources