2
votes

I'm trying to plot a histogram for the following data:

<text>,<percentage> 
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%

I used the set datafile separator "," to obtain the corresponding columns. The plot should have percentage on the X-axis and the statements on the Y-axis (full character string). So each histogram is horizontal.

How can I do this in gnuplot? Or is there other tools for plotting good vector images?

2

2 Answers

5
votes

The gnuplot histogram and boxes plotting styles are for vertical boxes. To get horizontal boxes, you can use boxxyerrorbars.

For the strings as y-labels, I use yticlabels and place the boxes at the y-values 0, 1 and 2 (according to the row in the data file, which is accessed with $0).

I let gnuplot treat the second column as numerical value, which strips the % off. It is added later in the formatting of the xtics:

set datafile separator ','
set format x '%g%%'
set style fill solid
plot 'data.txt' using ($2*0.5):0:($2*0.5):(0.4):yticlabels(1) with boxxyerrorbars t ''

The result with version 4.6.4 is:

enter image description here

0
votes

@Christoph Thank you. Your answer helped me.

@Slayer Regarding your question to add labels using gnuplot v5.2 patchlevel 6 and using @Christoph's provided sample.

Sample Code:

# set the data file delimiter
set datafile separator ','

# set the x-axiz labels to show percentage
set format x '%g%%'

# set the x-axis min and max range
set xrange [ 0 : 100]

# set the style of the bars
set style fill solid

# set the textbox style with a blue line colour
set style textbox opaque border lc "blue"

# plot the data graph and place the labels on the bars
plot 'plotv.txt' using ($2*0.5):0:($2*0.5):(0.3):yticlabels(1) with boxxyerrorbars t '', \
     ''          using 2:0:2 with labels center boxed notitle column

Sample Data Provided:(plotv.txt)

<text>,<percentage> 
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%

Reference(s):

  1. gnuplot 5.2 demo sample - textbox and the related sample data