Displaying long labels in Gnuplot - gnuplot

I am currently working for a company where I have to evaluate a survey. Now do I have to make a plot of the data.
They want a table with the questions in the left column and a point in the right column with a distance to the question which symbolizes the average grade[0:4]. My first attempt is using gnuplot because I have used it before and now ran into the problem that if the length of the question(I already shortened them to a max. of 50 characters) is too long gnuplot is having problem displaying the plot correctly. The whole plot gets pushed to the right and you can't read it anymore.
My dataset looks like this
Eltern können sich über unsere Schule vielfä..., 2.2
Eltern werden über wichtige Entscheidungen inf..., 2.4
Eltern wissen über Projekte der Schule Bescheid, 2.4
Eltern erfahren Planung und Durchführung von F..., 1.5
Die Schule hat einen guten Ruf, 2.8
And here is my gnuplot file
set terminal pdfcairo enhanced font "Droid Sans,9" linewidth 4 rounded fontscale 1.0
set output "template.pdf"
set style line 80 lt rgb "#808080"
set border 3 back linestyle 80 #remove top and right border
set xtics nomirror
set ytics nomirror
set datafile sep ','
set xtics 0,1,4.
set ytics font ",4"
set xlabel "Note"
set style line 1 lc rgb "#0060AD" lt 1 lw 2 pt 7 pi -1 ps 1.0
set pointintervalbox 3
set xrange [0:4]
plot 'datafiles/adler_organisation.dat' using 2:0:ytic(1) notitle w lp ls 1
The result I currently get is
Here is an image how the rest of the evaluation looks like(made in LaTeX with datatool, loongtable and LTXtable).

Basically it is enough to fix the left margin with the command set lmargin at screen 0.5. Gnuplot isn't very good at estimating the margins based on the font type and font size. The result I get with this little change is:
Gnuplot cannot wrap long line automatically. You could use the epslatex terminal an put the labels in a \parbox with fixed length to achieve this, like with
label(s) = sprintf('\parbox{4cm}{\raggedleft %s}', s)
set ytics right
plot 'datafiles/adler_organisation.dat' using 2:0:ytic(label(strcol(1))) notitle w lp ls 1
But that would possibly give you problems getting the fonts. I haven't tried using epslatex together with Droid Sans.
As an other option you can use a python script to preprocess your datafile and insert line breaks. With the python script wraplabels.py:
from __future__ import print_function
import sys
import textwrap
with open(sys.argv[1], 'r') as f:
for line in f:
l, v = line.split(',')
print('"{}",{}'.format('\\n'.join(textwrap.wrap(l.strip(), 30)), v), end='')
and the gnuplot script
set terminal pdfcairo enhanced font "Droid Sans,9" linewidth 4 rounded fontscale 1.0
set output "template.pdf"
set style line 80 lt rgb "#808080"
set border 3 back linestyle 80 #remove top and right border
set tics nomirror
set datafile sep ','
set xtics 0,1,4.
set ytics font ",4" right
set xlabel "Note"
set style line 1 lc rgb "#0060AD" lt 1 lw 2 pt 7 pi -1 ps 1.0
set pointintervalbox 3
set xrange [0:4]
set lmargin at screen 0.3
plot '< python wraplabels.py datafiles/adler_organisation.dat' using 2:0:ytic(1) notitle w lp ls 1
you get a nice result

Related

Gnuplot - How to make multiple plots and save each one to different files with cairolatex

I need to make a 2d plot and a 3d plot, which I want to save as .tex files, respectively labo2-mag/latex/position-x-y-over-time.tex and labo2-mag/latex/position-3d.tex. Is there anything wrong with my script ?
# General settings
set tics font ", 14"
set tmargin at screen 0.7
set key at screen 0.3, 0.8 font ",14" spacing 1.3 samplen 0.8 box opaque
# set key outside font ",14" spacing 1.3 box opaque
# Send the border to the background
set border lw 0.1 back
set xlabel '$t$ [s?]' offset 1.7;
set ylabel '$Position$ [m?]' offset -1;
# Plot once 'in the air'
# set terminal unknown
plot "labo2-mag/data/output.out" using 1:2 with points pointtype 6 lc rgb "red" title '$x(t)$', \
"" using 1:3 with points pointtype 8 lc rgb "green" title '$y(t)$', \
"" using 1:4 with points pointtype 6 lc rgb "blue" title '$z(t)$'
# pause 70
# reread
set terminal cairolatex pdf size 10cm, 10cm
set out 'labo2-mag/latex/position-x-y-over-time.tex'
MAX_Y=GPVAL_Y_MAX
MIN_Y=GPVAL_Y_MIN
MAX_X=GPVAL_X_MAX
MIN_X=GPVAL_X_MIN
set xrange [MIN_X-(MAX_X-MIN_X)*0.1:MAX_X+(MAX_X-MIN_X)*0.01]
set yrange [MIN_Y-(MAX_Y-MIN_Y)*0.05:MAX_Y+(MAX_Y-MIN_Y)*0.05]
replot
# reset terminal
splot "labo2-mag/data/output.out" using 2:3:4 with points pointtype 6 lc rgb "red" title '$pos(t)$'
set terminal cairolatex pdf size 10cm, 10cm
set out 'labo2-mag/latex/position-3d.tex'
replot
Your splot command is issued while the first terminal is still active and writing to the first output file. You need to change the terminal and output before doing a new plot. The minimum would be to issue unset output after each of your replot statements.
You might want or need to also set a new intermediate terminal type together with the unset output. I don't understand the "in the air" comment in the script. Do you mean you don't actually want to plot it? You want to view it interactively before plotting to a file? Otherwise maybe set terminal dumb? If it is relevant, please clarify.

How to italic text in Times new roman font in gnuplot

I am trying to produce a plot with font of Times new roman in Jpeg terminal.
Gnuplot 4.6 is installed in Windows. I have tried with JPEG terminal and ended with following result by code:
# this script for compare remaining ice area
reset
set term png large font "times new roman" 20 size 700,500 enhanced #500,400
set output 'Compare.png'
#
set xlabel "{/=15 {/Italic Time} (min)}"
set ylabel "{/=15 {/Italic Remaining ice area} (percent)}"
set xrange [0:75]
set yrange [0.5:1.01]
set key top right
plot 'ice_example.dat' u 1:2 w p pt 8 lc rgb 'red' lw 3 title 'Free surface LBM' ,\
'ice_mass.dat' u 1:2 w p pt 5 lc rgb 'black' lw 5 title 'Experiment'
unset multiplot
set term win
unset output
When I change term png to term jpeg, output file is not produced. Also It does not work with {/Times New Roman-Italic Time} (min) for label.
Thanks for advance.

How to make a plot of positions with palette in Gnuplot?

How can I make a plot with some values of delta_x and delta_y positions; where I need to use palette (or colorbars) for each point showing their respective epochs (My current plot, and an example of the plot that I would like to make are shown below).
My current Gnuplot code is as follows:
set fontpath '/System/Library/Fonts'
set term post color enh eps font "Helvetica" 14
#Gnuplot script file for plotting data
#Set Line style
set style line 12 lw 1 ps 1 pt 8 # empty triangle
set style line 13 lw 1 ps 0.7 pt 81 #empty circle
#Add Legend
set key top right box
# set plot
set xrange[1.2:0]
set xlabel "Relative R.A. (mas)" # set xlabel
set mxtics 4
set xtics 0, 0.1, 1.2
set yrange[0:1]
set ylabel "Relative decl. (mas)"
set mytics 4
set ytics 0, 0.1, 1
plot "MOD1.dat" using 1:2 w points ls 13 lc rgb 'black' title "Data1", "MOD2.dat" using 1:2 w points ls 12 lc rgb 'gray' title "Data2"
Where my data are as follows:
Data1:
#Year RA(mas) DEC(mas)
1993-06-26 0.36315 0.23913
1993-12-16 0.33392 0.28443
1994-01-28 0.34606 0.30810
1994-12-23 0.37139 0.32989
1995-02-12 0.34050 0.29165
1995-08-17 0.55405 0.42913
1995-12-18 0.36928 0.2777
1996-04-07 0.49601 0.31533
1996-12-13 0.43557 0.34637
1997-11-14 0.36811 0.25562
1998-06-02 0.55603 0.36268
1998-12-07 0.52873 0.23110
2000-11-12 0.45839 0.22572
ieData2:
#Year RA(mas) DEC(mas)
1993-06-26 0.63633 0.44645
1993-09-18 0.63548 0.35586
1993-12-16 0.63161 0.41704
1994-01-28 0.27266 0.47256
1994-03-14 0.75819 0.65255
1994-04-21 0.69664 0.68481
1994-06-21 0.78735 0.72865
1994-08-29 0.91143 0.78274
1994-10-30 0.55326 0.43258
1994-12-23 0.67065 0.54423
1995-02-12 0.55778 0.51656
1995-08-17 1.01458 0.50502
Thank you.
try the palette linetype, like this:
plot 'MOD1.dat' u 2:3:1 w p pt 7 ps 2 lt palette

import font in Gnuplot: strange result

I am using the fontfile option to import a font in gnuplot. The font I selected is similar to a computer modern font, but I obtain a very different font.
Here is my code
reset
set term postscript enhanced eps fontfile "/usr/share/texmf/fonts/type1/public/lm/lmr8.pfb" "lmr8"
set output "comparison_inside_fields.eps"
set key spacing 1.5
normTemp=2*pi*750*1E+12
set xlabel "{/lmr8=20 t} ({/lmr8=18 s})"
set ylabel "Re({/lmr8=18 E}_{/lmr8=12 1}) ({/lmr8=18 V/m})" font "lmr8,17"
set logscale y
set yrange [50:1000]
set size 0.65
set tics front
set autoscale xfixmax
plot "Analytic_EField_linear_PW1000.dat" u ($1/normTemp):($2) w l lw 3.0 lc rgb "red" title "E_{1} equa-diff", "EField_linear_PW1000.txt" u ($1):($2) w l lw 3.0 lc rgb "blue" title "E_{1} CST", "exponential_linear.dat" u ($1/normTemp):($2) w l lt 2 lw 1.0 lc rgb "black" notitle`
and here is the image:
The font name is LMRoman8, and not lmr8. Use e.g. kfontview or head -1 .../lmr8.pfb to see the font name.
So a rather minimal example taken from your script would be:
reset
set term postscript enhanced eps fontfile "/usr/share/texmf/fonts/type1/public/lm/lmr8.pfb" "LMRoman8"
set output "comparison_inside_fields.eps"
set xlabel "{/LMRoman8=20 t} ({/LMRoman8=18 s})"
set ylabel "Re({/LMRoman8=18 E}_{/LMRoman8=12 1}) ({/LMRoman8=18 V/m})" font "LMRoman8,17"
plot x title "E_{1} equa-diff"
Result with 4.6.5 is:
The result is ok, but I would recommend you to use the epslatex terminal, which provides much nicer typesetting. Here an examplary document similar to yours:
basename = 'epslatex-test'
set terminal epslatex standalone header '\usepackage{lmodern}'
set output basename.'.tex'
set xlabel '$t$ (s)'
set ylabel 'Re$(E_1)$ (V/m)' offset 1
plot x
set output
system(sprintf('latex %s.tex && dvips %s.dvi && ps2pdf %s.ps && pdfcrop --margins 1 %s.pdf %s.pdf',\
basename, basename, basename, basename, basename))
Instead of using the header option you can also write all the packages and settings you want for all image into a file gnuplot.cfg, which will be included automatically, if available.

Can I use only certain numbers from data file in gnuplot title?

I have a data file with many columns of data and the first two lines look like this:
#time a1 b1 c1 d1 a2 b2 c2 d2
1 2 3 4 5 6 7 8 9
I would like to put a title on the graph which reads like this:
a1=2, a2=6, b1=3, b2=7, c1=4, c2=8, d1=5, d2=9
So, basically just taking the data from the first line and adding some text with it. Is this possible?
Thanks mgilson! Here is the script (the first part.)
#filename = "a6_a.txt"
//defining variables called ofac and residual
unset multiplot
set term aqua enhanced font "Times-Roman,18"
set lmargin 1
set bmargin 1
set tmargin 1
set rmargin 1
set multiplot
set size 0.8,0.37
set origin 0.1,0.08 #bottom
set xlabel "time" offset 0,1 #bottom
set ylabel "{/Symbol w}_i - {/Symbol w}_{i+1}"
set yrange [-pi:pi]
plot filename using 1:(residual($7 -$14)) ti "" pt 1 lc 2, \
"" using 1:(residual($14-$21)) ti "" pt 1 lc 3, \
"" using 1:(residual($21-$28)) ti "" pt 1 lc 4
set origin 0.1,0.36 #mid
set format x "" #mid, turn off x labeling
set xlabel "" #mid
set ylabel "P_{i+1}/P_i" offset 2,0
set yrange [*:*]
plot filename using 1:(($10/ $3)**1.5) ti "P_2/P_1" pt 1 lc 2, \
"" using 1:(($17/$10)**1.5) ti "P_3/P_2" pt 1 lc 3, \
"" using 1:(($24/$17)**1.5) ti "P_4/P_3" pt 1 lc 4
set origin 0.1,0.64 #top
set ylabel "semi-major axes"
set yrange [*:*]
plot filename using 1:($3):($4*$3) with errorbars ti "" pt 1 lc 1, \
"" using 1:($10):($10*$11) with errorbars ti "" pt 1 lc 2, \
"" using 1:($17):($17*$18) with errorbars ti "" pt 1 lc 3, \
"" using 1:($24):($24*$25) with errorbars ti "" pt 1 lc 4
unset multiplot
unset format
unset lmargin
unset bmargin
unset rmargin
unset tmargin
You probably could do this using gnuplot only (I think), but it would be painful. The easiest solution is to write an external script which will format the title for you -- e.g. in python (untested):
#python script formatTitle.py
import sys
fname=sys.argv[1]
with open(fname,'r') as f:
line1=f.readline().split()
line2=f.readline().split()
pairs=sorted(zip(line1,line2)[1:])
print (", ".join("%s=%s"%(p[0],p[1]) for p in pairs))
and then in gnuplot:
set title "`python formatTitle.py datafile.dat`"
EDIT
Your script above has a few problems. First, // is not a comment character in gnuplot -- so that will raise some sort of syntax error right away. Next, you're using filename as a string variable (which is OK) but you've not defined it anywhere. (the first line of your script would probably work except that it has been commented out). Finally, I can't check since I don't have your datafile, but your use of lmargin etc + set size + set origin is a little hard (for me) to track. Someone smarter would probably have no problem with it. When I want to explicitly align my plots however, I use the second form of margin.
#make plots go from 0.1 to 0.9 on the screen (screen range is 0-1)
# (0,0) is the lower left corner, (1,1) is the upper right.
set lmargin at screen 0.1
set rmargin at screen 0.9
And then for each of your sub-plots, you can do:
NPLOTS=3
SCREENSIZE=0.8 #leave .1 for the top and bottom border
DY=SCREENSIZE/NPLOTS #height of each plot
#bottom
set tmargin at screen 0.9-2*DY
set bmargin at screen 0.9-3*DY
#other commands
plot ...
#middle
set tmargin at screen 0.9-DY
set bmargin at screen 0.9-2*DY
#other commands ...
plot ...
#top
set tmargin at screen 0.9
set bmargin at screen 0.9-DY
#other commands ...
plot ...
Of course, you can play around with the positions as you see fit.

Resources