Plotting horizontal lines across histogram bars

2019-08-19 03:32发布

I have an experiment where I measured the performance of some algorithms relative to three baselines. I'd therefore like to plot histograms for the algorithms, with horizontal lines of various styles drawn through the histogram bars to show the baselines.

Below is an example which I produced by manually drawing horizontal lines on a graph produced by Gnuplot. The histograms "sentence" and "document" represent the algorithms I tested, and "mono", "random", and "MFS" are the baselines.

Is there some way I can do this within Gnuplot itself? If not, can anyone recommend another tool which can do this? Or perhaps there's a better visualization technique I should be using instead?

a histogram where the bars in each series are bisected by lines of various styles

1条回答
贼婆χ
2楼-- · 2019-08-19 04:13

This is definitely possible. Here's a little example that I cooked up:

First, the datafile "data.dat":

#histograms
1 3 stack1
2 2 stack2
3 1 stack3


#mono
 .6
 .6

 1.5
 1.5

 3.1
 3.1

Now the gnuplot script to plot it:

set yrange [0:*]
set style data histograms 
set style histogram cluster gap 1

IDX=-1
xpos(x)=(IDX=IDX+1, IDX%2==0)?(IDX/2-.5):(IDX/2+.5)

set style fill solid
plot 'data.dat' index "histograms" u 1:xtic(3) title "column1", \
     '' index "histograms" u 2 title "column2", \
     '' index "mono" u (xpos($1)):1 w lines ls -1 title "mono"

This is a little more tricky than my last version. When plotting a cluster of histograms, each cluster is centered on an integer starting at 0 and incrementing by 1 for each cluster (regardless of your setting for xtics and labels). What I've done is used that information to simplify the datafile. Now this plot command plots 2 different data sets as histograms (taken from each column in the "histogram" portion of the datafile), the first one adds the xtic labels. Then the tricky part: I write a function which has side-effects (gnuplot inline-functions are new in gnuplot 4.4 I think). Each time it is called, the value for the variable IDX is incremented -- So, the current position on the xrange is always IDX/2. This function alternates between returning IDX/2-.5 and IDX/2+.5. Note that to create another dataset random, you'll need another function xpos2 which is the same as xpos1 except it uses a separate iterator.

查看更多
登录 后发表回答