如何在x轴上的数据文件中使用列标题(字符串)作为带有框的图的xiticlabel? (Gnuplot) [英] How do I use the columnhead (string) in a data file on the x-axis as a xiticlabel of a plot with boxes? (Gnuplot)

查看:97
本文介绍了如何在x轴上的数据文件中使用列标题(字符串)作为带有框的图的xiticlabel? (Gnuplot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,这是我的第一个问题,希望我的问题能够按照此处的规定正确描述...

I´m new here and this is my first question, hope my Problem is described properly according to our rules in here...

我有一个数据文件(datafile.dat),该文件用于创建多个绘图(见下文):

I´ve got a data file (datafile.dat) which is used to create several plots (see below):

temp name1  name2
10   1000   1200
22   800    750
50   250    200
100  80     82
107  5      3

我想做的是用第二和第三列中的值用方框创建一个绘图.在x轴上,应显示这些值所指的名称.此外,还应为每个盒子赋予特定的颜色.另一个优点是该解决方案也可以在循环中使用(因为原始数据文件包含更多的列...). 最后,我希望图看起来像这样: 所需的地块布局.

What I want to do is to create a plot with the values in the second and third column plotted with boxes. On the x-axis the names these values refer to shall be displayed. In Addition it shall be possible to give each of the boxes a specific colour. An additional Advantage would be that the solution can also be used in a Loop (because the original data file contains a lot more columns...). In the end I want the graph to look something like this: Desired Layout of the plot.

为了得到这个,我尝试了各种搜索互联网的方法(见下文).我正在Windows上使用以下命令文件运行gnuplot 5:

In order to get this I tried different things I found searching the Internet (see below). I am running gnuplot 5 on Windows with the following command file:

xticlabels 如果我尝试例如对于第2列,此方法无效:

xticlabels If I try this e.g. for column 2 this doesn´t work:

plot 'datafile.dat' u 2:xticlabels(columnhead(2))

使用外部实用程序 根本没用,产生了失败消息

Using an external utility Didn´t work at all, failure message was produced

统计信息 如果将输出存储在变量中,则看起来是一个很好的解决方案.但是我无法使我的代码正常工作(见下文):

Stats Looks like a pretty good solution if I store the output in a variable. But I can´t get my code working (see below):

reset
set terminal postscript eps size 15 cm, 15 cm colour enhanced dashed "Times, 22"
set output "test.pdf"
stats 'datafile.dat' using 2
b = STATS_sum
plot 'datafile.dat' u 2:xticlabels(b) every ::1     
reset

我该怎么做才能从上面的数据文件中创建所需的输出?我以许多不同的组合尝试了上述要点. 建议1 建议3 是与主题相关的解决问题的新思路,但我一无所获这些工作.能否请任何人帮助我获得解决方案?任何提示将不胜感激!!!

What can I do to create the desired output from the data file above? I tried the Points mentioned above in many different combinations. Suggestion 1, Suggestion 2, Suggestion 3 are further Topic-related ideas to solve the Problem but I got none of these working. Can please anyone help me to get a solution? Any hints will be highly appreciated!!!

提前谢谢!

迈克尔

我发现三年前已经有人问过这个问题:问题

I found out that this question was already asked from someone else three years ago: Axis label and column header ...Is there maybe a solution today? Also: Question

推荐答案

我可以看到两种方法.第一种是自动的,但缺点是不能做颜色.

I can see two methods for doing this. The first is more automatic, but has the disadvantage of not being able to do the colors.

每列仅使用一个数据点(正如您的注释所建议的那样),我们几乎可以使用列堆叠的直方图样式来完成此操作.此时,我不确定如何获得不同的颜色,因为columnstacked样式会将颜色应用于堆栈的各个部分.

Using only one datapoint for each column (as your comment suggests you will be doing), we can almost accomplish this using the columnstacked histogram style. At this point, I'm not sure how to get different colors, as the columnstacked style applies colors to the sections of the stacks.

使用您的示例数据和第一行数据,我们可以做到

Using, your example data, and the first line of data, we can do

set style data histogram            # we could do w histograms in the plot command instead
set style histogram columnstacked
set boxwidth 0.9                    # so the boxes don't touch
set style fill solid
set key autotitle columnhead        # first row contains series name

plot for[i=2:3] "datafile.dat" every ::0::0 u i 

其中every ::0::0表示仅使用数据的第0行(第一行) .

where every ::0::0 means use the 0th (first) line of data only.

这产生

例如,要绘制第2至50列,只需将for[i=2:3]更改为for[i=2:50].

To plot columns 2 through 50, for example, just change the for[i=2:3] to for[i=2:50].

我们可以使用stats命令添加标签,然后执行标准绘图命令来完成此操作.

We can do this by using the stats command to add the labels, and then do a standard plot command.

要设置刻度线,我们可以做

To set the tic marks, we can do

set xtics 1,1 format ""
do for[i=2:3] {
    stats "datafile.dat" every ::0::0 u (a=strcol(i),1) nooutput
    set xtics add (a i-1)
}

这里的第一个命令将xtics设置为从1开始的每1个单位发生一次,但会取消标签(我们将设置自己的标签).

The first command here sets the xtics to occur every 1 unit starting at 1 but suppresses the labels (we will be setting our own labels).

然后,我们遍历每一列,并使用stats命令读取数据文件中的第0行.读取时,我们将列标题存储在变量a中.我们只返回1以供stats命令进行实际分析.实际上,我们并不关心此命令的结果,我们只需要它来读取列标题.最后,我们使用set xtics add将此标签添加为xtic.

We then loop over each column, reading the 0th line in the datafile with the stats command. When we read it, we store the columnheader in a variable a. We just return a 1 for the stats command to actually analyze. We actually don't care about the results of this command, we just need it to read the column headers. Finally, we use set xtics add to add this label as an xtic.

接下来,我们可以执行一些必要的设置命令

Next, we can do some necessary set up commands

set style fill solid
set boxwidth 0.9      # so the boxes don't touch
unset key
set yrange[0:*]       # by default, the smallest boxes may be cut off

最后,我们可以用

Finally, we can plot with

plot for[i=2:3] "datafile.dat" every ::1::1 u (i-1):i w boxes

结果是

同样,可以将for循环更改为使用任意数量的列.可以根据需要调整X范围,并且可以在plot命令中使用linetype命令设置颜色.

Again, the for loops can be changed to use any number of columns. X-ranges can be adjusted if desired, and linetype commands can be used in the plot command to set colors.


我们使用every ::0::0是因为set key autotitle命令会导致忽略具有列标题的第一行(在plot命令之前进行处理).因此,第一行(第0行)是实际数据的第一行.


We use every ::0::0 because the set key autotitle command causes the first line with the column headers to be ignored (processed before the plot command). Thus the first (0th) line is the first line of actual data.

请注意,此处使用every ::1::1是因为第0行是列标题行.如果没有set key autotitle命令,第一行不会自动被忽略.

Note that here we use every ::1::1 because the 0th line is the column header line. Without the set key autotitle command, the first line is not automatically ignored.

这篇关于如何在x轴上的数据文件中使用列标题(字符串)作为带有框的图的xiticlabel? (Gnuplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆