mbbce
mbbce

Reputation: 2303

Gnuplot: String xticlabels with duplications

I have a data file with two columns with column 1 having string labels for x-axis such as:

label1 20
label2 30
label1 30
label3 20

I am using the following the command in gnuplot

gnuplot> plot 'datafile' 2:xticlabels(1)

The problem is that this command produces a graph with label1 in two separate ticks. What I want to do is to put two y-axis values corresponding to the same x-tic labelled 'label1'. Is there a way to do that in gnuplot?

Upvotes: 1

Views: 601

Answers (3)

Faced with same issue and I solved it by changing string labels to nums. For data file from question it would be like that

1 20
2 30
1 30
3 20

Then in gnuplot script file I add command, which assigns nums to string labels

# set xtics for x axis
set xtics ("label1" 1, "label2" 2, "label3" 3)

What we'll get as result

enter image description here

Upvotes: 0

theozh
theozh

Reputation: 25749

There is and was no need to use external tools or modify the original data. If your "prefix" is just label, you also don't need to create a list of unique elements but you can simply extract the index by defining a function. Check help strcol, help strlen. The following script works even with gnuplot 4.6.0 dated from April 2012.

Data: ('myData.dat')

label1 20
label2 30
label1 30
label3 20

Code:

### plot data with duplicate xlabel
reset

myIdx(col) = int(strcol(col)[6:strlen(strcol(col))])
set offset 1,1,1,1

plot 'myData.dat' u (myIdx(1)):2:xtic(1) w p pt 7 lc rgb "red"
### end of code

Result:

enter image description here

Upvotes: 0

Marco Tompitak
Marco Tompitak

Reputation: 658

I don't think gnuplot has any native support for this. What you can do is add another column to your data that associates a number with every label. In the case of your example label names, you could simply use the last character in the label name for that. You could achieve the whole thing using awk. As an example, see the following command and output:

plot "< awk '{print $0, substr($1, length($1), 1)}' gnuplotstringlabels.dat" u 3:2:xticlabels(1) t 'Grouped Labels' ps 2 pt 7

enter image description here

What the awk command produces is this:

label1 20 1
label2 30 2
label1 30 1
label3 20 3

Of course, your labels are probably not called label1 etc. You would have to make whatever produces your data supply such unique numbers for your labels, or write a more complicated script to add this third column.

Upvotes: 1

Related Questions