在Gnuplot中显示长标签

时间:2014-07-01 15:28:20

标签: gnuplot

我目前正在为一家我必须评估调查的公司工作。现在我必须绘制数据图。 他们想要一个包含左栏中问题的表格和右栏中的一个点,与问题的距离代表平均等级[0:4]。我的第一次尝试是使用gnuplot,因为我之前使用过它,现在遇到的问题是,如果问题的长度(我已经将它们缩短到最多50个字符)太长,gnuplot在正确显示绘图时遇到问题。整个情节被推到右边,你再也看不懂了。

我的数据集看起来像这样

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

这是我的gnuplot文件

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

我目前得到的结果是

enter image description here

Here是评估其余部分的图像(使用datatool,loongtable和LTXtable在LaTeX中制作)。

1 个答案:

答案 0 :(得分:1)

基本上用命令set lmargin at screen 0.5修复左边距就足够了。 Gnuplot并不擅长根据字体类型和字体大小估算边距。我得到的这个小改动的结果是:

enter image description here

Gnuplot无法自动换行。您可以使用epslatex终端将标签放在\parbox中,并使用固定长度来实现此目的,例如

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

但这可能会让你在获取字体方面遇到问题。我没有尝试将epslatex与Droid Sans一起使用。

作为另一种选择,您可以使用python脚本预处理数据文件并插入换行符。使用python脚本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='')

和gnuplot脚本

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

你得到了一个不错的结果

enter image description here