Gnuplot,y轴上的文本和图表区域内的值

时间:2014-02-24 11:49:43

标签: plot gnuplot

很容易绘制像这样的X-Y数据点

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

与Gnuplot。

但是我试图像这样绘制数据点

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

图表上有Y轴的文字

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

我该怎么做?

set xtics ("2010" 0, "2011" 1, "2012" 2, "2013" 3)
set ytics ("M1" 0, "M2" 1, "M3" 2)
plot 'data.txt' ??????

1 个答案:

答案 0 :(得分:1)

我的回答基于this suggestion how to mirror Matlab's spy function 但是,您必须将数据文件重新格式化为这样(保存为data.dat):

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

这将有助于区分应绘制的元素(数字)和不应显示的元素(NaN)。

以下代码将生成您在问题中勾画的情节:

# 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

你将得到的情节如下:

plot-demo