HackHerz
HackHerz

Reputation: 23

Displaying long labels in 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

enter image description here

Here is an image how the rest of the evaluation looks like(made in LaTeX with datatool, loongtable and LTXtable).

Upvotes: 2

Views: 1382

Answers (1)

Christoph
Christoph

Reputation: 48390

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:

enter image description here

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

enter image description here

Upvotes: 1

Related Questions