GNUPlot - How to control interline spacing in tick labels? - label

I'm trying to plot a graph that has two-line labels on its xticks. I cannot reduce the font anymore because it becomes unreadable (the plot is part of a 2x2 multiplot).
The problem is that the separation between the two lines in a label is too big, so that they become too close to the labels of the next columns.
I would like to reduce the interline spacing so that the lines of a label become closer between them, but further to the lines of the other labels.
I've thought about placing the labels manually (using a function to compute the position of each line), but before doing it I would like to know if someone has a simpler solution.
Thanks a lot!
I'm using "gnuplot 4.6 patchlevel 4" on Ubuntu 14. The folowing is a MWE:
# Requires gnuplot >= 4.6
set terminal pdf color solid font "Helvetica, 10" enhanced size 4, 3.72
set output 'mwe.pdf'
set border 3 lc rgb "#000000"
set bmargin 9
set ylabel offset -1
TicksFont = ", 10"
Title2Font = ", 14"
set yrange[0:180 < * ]
set format y "%.0f%%"
set xtics out scale 0, 0 nomirror rotate by 90 right offset 0,0 font TicksFont
set ytics out nomirror font TicksFont
unset key
set datafile separator ";"
set style fill transparent solid 1.0 border -1
set style data boxes
set boxwidth 1.0
#Bottom-left plot
set title "C) Third (sub)plot" font Title2Font
plot '-' using ($0):2:(0xFF8080):xtic(1) notitle lc rgb variable
# Label; Value
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. Ccccc: Ccccccc(Ccc),\nCccccccc(CCC); 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
05. Eeee: EEEE,\nEEEEEEEEEEEE(EE); 50
end;

From gnuplot you cannot directly control the line height used for the labels. But there is a quite dirty workaround for your problem:
You can split your label at the new line character, change the font size of each line with the enhanced label syntax, but set a different font size for the new line character. So a label
set label at 0,0 "first line\nsecond line" font ",10"
is changed to
set label at 0,0 "{/=10 first line}\n{/=10 second line} font ",8"
And your script changes to:
# Requires gnuplot >= 4.6
set terminal pdf color solid font "Helvetica, 10" enhanced size 4, 3.72
set output 'mwe.pdf'
set border 3 lc rgb "#000000"
set bmargin 9
set ylabel offset -1
TickSize = 10
TicksFont = ", ".TickSize
Title2Font = ", 14"
set yrange[0:180 < * ]
set format y "%.0f%%"
set xtics out scale 0, 0 nomirror rotate by 90 right offset -0.1,0 font ",8"
set ytics out nomirror font TicksFont
unset key
set datafile separator ";"
set style fill transparent solid 1.0 border -1
set style data boxes
set boxwidth 1.0
#Bottom-left plot
set title "C) Third (sub)plot" font Title2Font
set_label_size(s) = sprintf('{/=%d %s}', TickSize, s)
label(s) = strstrt(s, "\n") ? set_label_size(s[:strstrt(s, "\n")-1])."\n".set_label_size(s[strstrt(s, "\n")+1:]) : set_label_size(s)
plot '-' using ($0):2:(0xFF8080):xtic(label(strcol(1))) notitle lc rgb variable
# Label; Value
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. Ccccc: Ccccccc(Ccc),\nCccccccc(CCC); 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
05. Eeee: EEEE,\nEEEEEEEEEEEE(EE); 50
end;
Before (left) and after (right):

You can switch to the epslatex terminal and use latex syntax on your gnuplot script directly. Then you can use the \vspace{} command with a negative argument to decrease the interline spacing, something like this:
\shortstack{01. Aaaaa: \vspace{-0.2em} \\ Aaaaaaaaa(AA)}
Although you'll need to escape backslashes:
\\shortstack{01. Aaaaa: \\vspace{-0.2em} \\\\ Aaaaaaaaa(AA)}

Would it be acceptable to just change the size of the whole plot, for example by changing the plot width from 4 inches to 6 and the height from 3.72 to 5.58?
This way the aspect ratio is preserved if you include it in a different document.
Before:
After:

Related

How to draw rowstacked histogram in gnuplot with value labels in each bar

I want to draw a rowstacked histogram. The following is what I have so far .
I have used the code below
reset
set bmargin 10
set lmargin 20
set rmargin 16
set tmargin 7
show margin
set title "" font "Verdana,27"
show title
set grid
set xlabel offset 0,-5 font "Verdana,25"
set xlabel "Number of Cores"
show xlabel
set xtics font "Verdana,18"
set ylabel offset -4,0 font "Verdana,25"
set ylabel "Time (%)"
show ylabel
set yrange [0:130]
set ytics font "Verdana,18"
set ytics (0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
set style data histogram
set style histogram rowstacked title offset 0,3
set style fill solid border lt -1
set boxwidth 0.9
set key right
set key font "Verdana,17"
set key autotitle columnheader horizontal
plot newhistogram '{/=22 1-Core}', \
for [i=2:5] 'base_vs_FHA_speedup_1core.dat' using i:xtic(1), \
newhistogram '{/=22 2-Cores}' lt 1, \
for [i=2:5] 'base_vs_FHA_speedup_2core.dat' using i:xtic(1) notitle, \
newhistogram '{/=22 4-Cores}' lt 1, \
for [i=2:5] 'base_vs_FHA_speedup_4core.dat' using i:xtic(1) notitle, \
newhistogram '{/=22 8-Cores}' lt 1, \
for [i=2:5] 'base_vs_FHA_speedup_8core.dat' using i:xtic(1) notitle
I have 4 different data files with sample data like this
base_vs_FHA_1core.dat
Configuration col1 col2 col3 col4 col5
Base 5 50 40.5 4.5
FHA 5 16 38 5 1.54x
base_vs_FHA_2core.dat
Base 9.3 47 37.3 6.3
FHA 9.3 15 34.3 6.3 1.54x
base_vs_FHA_4core.dat
Base 18 41 33.9 7.06
FHA 16.6 13.07 30.7 7.4 1.47x
base_vs_FHA_8core.dat
Base 27.7 34.3 28.3 9.6
FHA 28.3 11.45 25.3 9.6 1.34x
I want to have each bar in the histogram labeled with the corresponding value and the value of the last column on the top of the second histogram in each pair. How can I achieve this?
Can I merge all the 4 files and achieve the same goal?
You can merge all 4 files in one file. I would recommend a separation with two empty lines, because with this, you can easily address the (sub)datablocks via index, check help index.
For the labels you can make a double loop, one for the (sub)datablocks and one for the columns. You have to sum up the columns (check help sum) to calculate the y-position of the label (i.e. center of the histogram element). The x-position of the labels is special for the first dataset (since only this has a columnheader), this is taken into account with (i*3+$0-(i==0)), i.e. i==0 will return 1 if i equals 0, and 0 otherwise.
Instead of setting a large fontsize many times you might want to set it in the terminal, e.g. something like: set term pngcairo font ",17".
Find below a minimized example as a starting point for further optimization.
Script:
### rowstacked histogram with numbers in corresponding boxes
reset session
$Data <<EOD
# base_vs_FHA_1core.dat
col1 col2 col3 col4 col5
Base 5 50 40.5 4.5
FHA 5 16 38 5 1.54x
# base_vs_FHA_2core.dat
Base 9.3 47 37.3 6.3
FHA 9.3 15 34.3 6.3 1.54x
# base_vs_FHA_4core.dat
Base 18 41 33.9 7.06
FHA 16.6 13.07 30.7 7.4 1.47x
# base_vs_FHA_8core.dat
Base 27.7 34.3 28.3 9.6
FHA 28.3 11.45 25.3 9.6 1.34x
EOD
set xlabel "Number of Cores"
set ylabel "Time (%)"
set yrange [0:130]
set ytics 0, 10, 100
set grid
set style data histogram
set style histogram rowstacked title
set style fill solid 0.5 border lt -1
set boxwidth 0.9
set key top left noautotitle horizontal
N = 5 # number of last data column
plot newhistogram '1-Core' at 0, \
for [i=2:N] $Data index 0 u i:xtic(1) ti columnheader, \
newhistogram '2-Cores' lt 1 at 3, \
for [i=2:N] '' index 1 u i:xtic(1), \
newhistogram '4-Cores' lt 1 at 6, \
for [i=2:N] '' index 2 u i:xtic(1), \
newhistogram '8-Cores' lt 1 at 9, \
for [i=2:N] '' index 3 u i:xtic(1), \
for [i=0:3] for [j=2:N] '' index i u (i*3+$0-(i==0)): \
((sum [k=2:j] column(k))-column(j)*0.5):j w labels
### end of script
Result:

How can I create a GTIN14 bar code in ZPL?

Can I print a GTIN-14 barcode using ZPL?
I have searched, but can't find much information on weather or not it is possible to print this type of bar code, including the border around it.
Any help is greatly appreciated.
Thanks!
GTIN-14 is simply an interleaved 2 of 5 barcode with bearer bars and special text formatting. Assuming a 203dpi printer and an approximate x-dimension of 25 mils (.64mm), you can calculate all the dimensions of the barcode and its border box.
25mils is approximately 5 dots at 203dpi.
The barcode height should be 1.25 inches, which is ~250 dots.
The border around the barcode should be the width of the wide bar (3x narrow, or 15 dots).
Each character in the barcode is 45 dots (9 modules * 5 dots).
The start/stop also adds 45 dots (9 modules * 5 dots).
So the total length of the barcode is 15 * 45 dots = 675 dots.
Quite zone is 0.25 inches (50 dots) left and right of the barcode.
So the box around the barcode is 805 dots wide (15 border + 50 quiet + 675 barcode + 50 quiet + 15 border) and 280 dots high (15 border + 250 barcode + 15 border).
Here's how it looks in ZPL:
^XA
^FO100,100^BY5,3
^B2N,250,N,N,N
^FD00012345678905^FS
^FO35,85^GB805,280,15^FS
^FO115,380^AB,44,28^FD0 00 12345 67890 5^FS
^XZ
The top left corner of the barcode is at 100,100. The top left corner of the border box is at 35,85.
The example uses the built-in B font, you may want to change that...
And here it is in labelary.

gnuplot : setting line style in a for loop

I have to plot several curve on a same graph. I necessarly need to use a for loop to do this. I would like to plot the 2 first curves with lines and the others with points. I am able to plot all the curves with lines or all the curves with points but not to change in the same for loop.
Here is the concerned part of my code:
set style line 1 lw 1 lc rgb "green"
set style line 2 lw 1 lc rgb "purple"
set style line 3 pt 1 ps 1.0 lc rgb "red"
set style line 4 pt 2 ps 1.0 lc rgb "red"
set style line 5 pt 3 ps 1.0 lc rgb "red"
plot for [i=1:words(FILES)] myDataFile(i) u (column(1)):((word(UTAUS_ch,i))) ls i title myTitle(i)
I would like to preface "ls i" with "w l" for the 2 first curves and "ls i" for the others. I tried to use a if statement by replacing "ls i" by "if (i < 2) {w l ls i} else {ls i}" but Gnuplot does not expect to find a if statement at this location.
Can someone help me ?
Thank you,
Martin
As mentioned here you probably cannot switch plotting styles within a plot for loop.
So, either you do two separate loops, one with points and the other with lines or you do one loop with linespoints and define all necessary parameters for points and lines as functions (to keep the plot command readable).
As mentioned here, linewidth 0 is not zero but the thinnest possible line which is typically 1 pixel. To make the line disappear completely you have to use linetype -2.
Code:
### lines and points in the same plot for-loop
reset session
LINECOLORS = "red green blue magenta cyan"
LINEWIDTHS = '1.0 4.0 0.0 0.0 0.0'
POINTTYPES = '0 0 5 7 9'
POINTSIZES = '0 0 1.0 2.0 3.0'
TITLES = 'one two three four five'
myLinecolor(i) = word(LINECOLORS,i)
myLinewidth(i) = real(word(LINEWIDTHS,i))
myPointtype(i) = int(word(POINTTYPES,i))
myPointsize(i) = real(word(POINTSIZES,i))
myLinetype(i) = myLinewidth(i) == 0 ? -2 : 1
myTitle(i) = word(TITLES,i)
set samples 31
set key out
plot for [i=1:words(TITLES)] (sin(0.25*x-i)) w lp pt myPointtype(i) ps myPointsize(i) \
lt myLinetype(i) lw myLinewidth(i) lc rgb myLinecolor(i) title myTitle(i)
### end of code
Result:
Addition:
In order to keep the plot command as short and clear as possible you could also define line styles and use it in the plot for command via ls i, with the same result as above.
...
do for [i=1:words(TITLES)] {
set style line i pt myPointtype(i) ps myPointsize(i) \
lt myLinetype(i) lw myLinewidth(i) lc rgb myLinecolor(i)
}
plot for [i=1:words(TITLES)] (sin(0.25*x-i)) w lp ls i title myTitle(i)
Here a way using a macro:
set style line 1 lw 1 lc rgb "green"
set style line 2 lw 1 lc rgb "purple"
set style line 3 pt 1 ps 1.0 lc rgb "red"
set style line 4 pt 2 ps 1.0 lc rgb "red"
set style line 5 pt 3 ps 1.0 lc rgb "red"
set samp 100
set macro
cmd = ''
do for [i=1:10] {s = i<3? 'w l' : 'ls '.i;
cmd = cmd . '"+" us 1:(sin(x-'.i.'/10.)) '.s.' title "key '.i.'",'}
plot [0:2*pi] #cmd

Matching a Color Gradient to Data in Gnuplot

I have data showing a change over time, and I have used Gnuplot 4.6 to generate the following plot:
Here, I have manually defined eleven line styles to change the color gradually from red (#ff0000) to dark red (#5f0000) as `time' progresses. Here was my input:
# svg
set terminal svg size 600,600 fname 'CMU Sans Serif' fsize '10'
set output 'plot.svg'
# color definitions
set style line 1 lc rgb '#ff0000' lt 1 lw 2 pt 7 # starting light red
set style line 2 lc rgb '#ef0000' lt 1 lw 2 pt 7
set style line 3 lc rgb '#df0000' lt 1 lw 2 pt 7
set style line 4 lc rgb '#cf0000' lt 1 lw 2 pt 7
set style line 5 lc rgb '#bf0000' lt 1 lw 2 pt 7
set style line 6 lc rgb '#af0000' lt 1 lw 2 pt 7
set style line 7 lc rgb '#9f0000' lt 1 lw 2 pt 7
set style line 8 lc rgb '#8f0000' lt 1 lw 2 pt 7
set style line 9 lc rgb '#7f0000' lt 1 lw 2 pt 7
set style line 10 lc rgb '#6f0000' lt 1 lw 2 pt 7
set style line 11 lc rgb '#5f0000' lt 1 lw 2 pt 7
# visual elements
unset key
set xlabel 'x Axis'
set ylabel 'y Axis'
# plot data
plot for [n=2:11] 'data.dat' using 1:n with lines ls (n-1)
Is there a simpler way to get Gnuplot to gradually change the color as it plots multiple lines? I would ideally like to have a single script that can handle an arbitrary number of color gradations depending on the amount of data from the `data.dat' file.
You can e.g. use a function which calculates the colors depending on the iteration counter
# values r, g, b, x must be in the range [0:1]
rgb(r,g,b) = (255*r*256**2 + 255*g*256 + 255*b)
grad(x) = rgb(1 - x*0.7)
set style data lines
plot for [n=2:11] 'data.dat' using 1:2 lc rgb grad((n-2)/9.0)
Alternatively you can define a gradient and select a fractional position for each line
set palette defined (0 '#5f0000', 1 '#ff0000')
set style data lines
plot for [n=2:11] 'data.dat' using 1:2 lc palette frac (n-2)/9.0

MATLAB - How to plot x,y on an image and save?

File datafile.txt
code x y
23 22.1 33.11
23 110 22
23 11 200
24 111 321
24 222 111
24 10 22.1
10 88.3 99.3
10 110 32
10 121 143
10 190 200
In the above file, the first column represents the image code which is displayed on the screen, and the x and y columns represent the point where people look on the image. There were three different images displayed to the user. The problem with the code below is that I don't know how to save the image with the plotted x-y on with the same name of file as it opened.
fid = fopen(datafile.txt);
A = textscan(fid,'%f%f%f'); %Read data from the file
code = A{1};
xfix = A{2};
yfix = A{3};
for k=1:length(code)
imagefile=code(k)
I = imread([num2str(imagefile) '.jpg']); %# Load a sample image
imshow(I); %# Display it
[r,c,d] = size(I) %# Get the image size
set(gca,'Units','normalized','Position',[0 0 1 1]); %# Modify axes size
set(gcf,'Units','pixels','Position',[200 200 c r]); %# Modify figure size
hold on;
x = xfix2(k);
y = yfix2(k);
plot(x,y,'+ b');
f = getframe(gcf); %# Capture the current window
imwrite(f.cdata,([num2str(imagefile) '.jpg'])); %# Save the frame data
hold off
end
However, I have a little problem. The "cross plots" which I overlay on the image, were surrounded by gray shadow (like when we photocopy a paper, they will be a gray color on it). How did this happen?
There are numerous ways:
Superimposing line plots on images
Can I use a JPEG file as a background in a MATLAB plot?
How do I add a background image to my GUI or figure window?
And there's always some kind of problem with adjusting the axes afterwards.

Resources