1

It is easy to plot X-Y data points like this

# X   Y
  1   2
  2   4
  4   1
  ...

with Gnuplot.

However I am trying to plot data points like this

# Model     2010 2011 2012 2013
  M1              10        15
  M2         5    20  
  M3         10   10   20

The chart has texts on Y-axis

   ^
   |
 M1|          10           15
 M2|     5    20
 M3|     10   10    20
   +-----+-----+-----+-----+----->
      2010  2011   2012   2013

How can I do that?

set xtics ("2010" 0, "2011" 1, "2012" 2, "2013" 3)
set ytics ("M1" 0, "M2" 1, "M3" 2)
plot 'data.txt' ??????
2
  • Yes, you can set tics like that - have you tried it and it didn't work? Please clarify. Commented Feb 24, 2014 at 13:22
  • I don't know what I put in front of plot. What is the correct usage of using here? Commented Feb 24, 2014 at 13:47

1 Answer 1

2

My answer is based on this suggestion how to mirror Matlab's spy function.
However, you'll have to reformat your data file to look like this (saved as data.dat):

NaN  10   NaN  15  
5    20   NaN  NaN  
10   10   20   NaN  

This will help distinguish between the elements that should be plotted (the numbers) and those that should not appear (NaN).

Here's the code which will produce the plot you sketched in your question:

# the code will plot the numbers as labels:
set style data labels

# set up x- and y-coordinates based on the rows and columns
xcoord(N) = (N) 
ycoord(N) = (column(0)+1)

# define a symbol to be used as label:
# every NaN will be empty (" "), 
# and everything not equal to NaN (ne), will be a string 
# of the actual element
symbol(N) = strcol(N) ne "NaN" ? strcol(N) : " "

# define the ranges, use reverse with the yrange
set xrange [0:5]
set yrange [0:4] reverse

# set the tics (this is something you already had)
set xtics ("2010" 1, "2011" 2, "2012" 3, "2013" 4)
set ytics ("M1" 1, "M2" 2, "M3" 3)

# the actual plot command
# N will count through the number of columns

plot for [N=1:4] 'data.dat' using (xcoord(N)):(ycoord(N)):(symbol(N)) w labels

The plot you will get looks like this:

plot-demo

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it fully works. I have modified to add more data points and it is perfect. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.