Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnuplot rotate key

Tags:

gnuplot

I made a horizontal histogram by turning the diagram by 270° using the rotate package in LaTex. It is no problem to adjust all the labels and tics, but I did not find a way to rotate the key.

enter image description here

Here is my gnuplot code:

set terminal epslatex  size 16cm, 32cm
set output "stackexchange.tex"
set xrange [-1:8]
set key
set boxwidth 0.95
set style data histograms
set style histogram errorbars
set style fill solid 0.8
set style line 1 lt 1 lc rgb "#0080B0" lw 3
set yrange[600:1100]
unset ytics
set y2tics rotate
set y2label rotate "xyz / abc"
set xtics nomirror rotate scale 0 
plot 'stackexchange.dat' using 2:3:xticlabels(1) ls 1 title 'A', '' using 4:5 ls 1 fill pattern 6 title 'B'

That is how I implement it in LaTex:

\begin{figure}
        \begin{turn}{270}
        \resizebox{!}{0.9\textwidth}{\input{stackexchange}}
        \end{turn}
\end{figure}

This is my datafile:

A     890.1  3.2  789.9 11.7 
B    626.97      20.467      862.8 12.3
C   923.9   5.89  963.8 3.7
D    785.233     15.921     627   2.3
E    903.167     7.94 880.9 1.9
F    863.43      25.237     778.2 4.2
G     909.6 5.370     941   13
H     895.633     40.401    813   11.3
like image 294
cps Avatar asked Dec 02 '25 10:12

cps


1 Answers

I guess this is a classical xy-problem. Actually, you want to have a horizontal bar chart and since it doesn't exist as direct plotting style in gnuplot you think the solution would be to make a vertical bar chart and rotate it, and now you have the problem of how to rotate the key labels.

I know it would be convenient if there was a horizontal bar chart, but as far as I know gnuplot 5.4 still doesn't have it. Actually, it's not really needed, because you can do it "manually" with the plotting style boxxyerror (available at least since gnuplot 4.0). The following script works with gnuplot>=5.0 (version at the time of OP's question) and with some small modifications even with older versions. The result below is from wxt terminal, but is should work the same with epslatex terminal.

The script is kept rather general. So, I added two columns to OP's data and simply by setting N=3 you can have additional data in the graph (see second image).

Script: (works for gnuplot>=5.0.0, Jan. 2015)

### Horizontal grouped bar chart
reset session

$Data <<EOD
A    890.1      3.2     789.9   11.7      627      2.3 
B    626.97    20.467   862.8   12.3      778.2    4.2
C    923.9      5.89    963.8    3.7      789.9   11.7
D    785.233   15.921   627      2.3      813     11.3
E    903.16     7.94    880.9    1.9      862.8   12.3
F    863.      25.237   778.2    4.2      880.9    1.9
G    909.6      5.37    941     13.0      941     13.0
H    895.6     40.41    813     11.3      963.8    3.7
EOD

N        = 2                              # number of boxes in group
gap      = 0.2                            # relative gap between groups
width    = 0.8                            # relative boxwidth in group
grid     = (1.0 - gap)/N                  # raster
y(i)     = column(0) + (i-N/2.-0.5)*grid  # y-position
dy       = grid*width/2.                  # half relative boxwidth 
title(i) = sprintf("column %d",2*i)       # key/legend entry

set style fill transparent solid 0.5 
set offset 0,0,0.5,0.5
set xrange [600:1100]
set yrange [:] reverse
set ytics out
set key noautotitle

plot for[i=1:N] $Data u (0):0:(0):i*2:(y(i)-dy):(y(i)+dy):ytic(1) w boxxy lc i ti title(i), \
     for[i=1:N]    '' u i*2:(y(i)):i*2+1 w xerr lc i
### end of script

Result:

N = 2

enter image description here

N = 3

enter image description here

like image 97
theozh Avatar answered Dec 06 '25 09:12

theozh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!