简体   繁体   English

GnuPlot:根据 CSV 线画线

[英]GnuPlot: Draw line per CSV line

I just started to work with Gnu Plot and i created a few simple plots.我刚开始使用 Gnu Plot 并创建了一些简单的图。 But now i have a new problem.但现在我有一个新问题。

The input is a csv-file like this:输入是这样的 csv 文件:

name;n0;n1;n2
Benj;1;3;2
Silv;6;1;2
Steffi;3;2;2

The count of the columns and the count of the rows is dynamic.列数和行数是动态的。 So this is also a valid input file:所以这也是一个有效的输入文件:

name;n0;n1;n2;n3;n4
Benj;2;2;3;2;1

I like to have a graph like this (input is the first csv):我喜欢这样的图表(输入是第一个 csv):在此处输入图像描述

I played around with some Gnu Plot files which i created before, but i always just got something totally different.我玩过一些我之前创建的 Gnu Plot 文件,但我总是得到一些完全不同的东西。

Maybe someone has a hint or something to help me?也许有人有提示或可以帮助我的东西?

Thank you very much,非常感谢,

Best regards Kevin最好的问候凯文

As noted by Christoph, you need to transpose the data before plotting, the answers here should be helpful with that. 正如Christoph所说,你需要在绘图之前转置数据, 这里答案应该对此有所帮助。 To determine the number of rows in the data, recent versions of Gnuplot can use the stats command (see comment by Christoph below), otherwise the number of lines needs to be counted by other means. 要确定数据中的行数,最新版本的Gnuplot可以使用stats命令(请参阅下面的Christoph评论),否则需要通过其他方式计算行数。

Here is how you could handle both issues with external programs: 以下是如何处理外部程序的两个问题:

dfile = "data.csv"
nrec  = system("wc -l < " . dfile)
set datafile separator ';'
plot for [i=2:nrec] '<transpose --fsep \; -t '.dfile using i:xtic(1) with lines title columnhead

I used wc from coreutils to count the number of lines and transpose as suggested by flyingsheep to handle the transposition. 我使用coreutils中的wc来计算行数和transposeflyingsheep所建议的那样来处理转置。

Result: 结果:

Running the same script on data with more plots and data points: 在包含更多绘图和数据点的数据上运行相同的脚本:

data2.csv data2.csv

name;n0;n1;n2;n3;n4
Benj;1;3;2;5;3
Silv;6;1;2;3;4
Steffi;3;2;2;4;2
Carl;2;4;5;3;2

That's the wrong data file format for gnuplot. 这是gnuplot的错误数据文件格式。 Usually, for a single plot, gnuplot uses all rows for a single column. 通常,对于单个图,gnuplot将所有行用于单个列。 With a data file like 用像这样的数据文件

name;Benj;Silv;Steffi
n0;1;6;3
n1;3;1;2
n2;2;2;2

the plot becomes as easy as 情节变得如此简单

set datafile separator ";"
set style data lines
set key autotitle columnhead
plot 'data.csv' using 2:xtic(1), '' using 3, '' using 4

with the result (using 4.6.3): 结果(使用4.6.3):

在此输入图像描述

As mentioned already in the other answers, the most efficient way is certainly to transpose the data and use simple standard gnuplot plotting commands.正如其他答案中已经提到的,最有效的方法当然是转置数据并使用简单的标准 gnuplot 绘图命令。

If you want a platform-independent gnuplot-only solution you could do the following: If all rows have the same number of columns you can plot the data using the option matrix (check help matrix ).如果您想要一个独立于平台的仅 gnuplot解决方案,您可以执行以下操作:如果所有行的列数相同,您可以使用选项matrix plot 数据(检查help matrix )。 There are a few examples here on SO.这里有一些关于 SO 的例子。 However, here, the additional difficulty is to get the xticlabels and the legend as well.但是,在这里,额外的困难是获取 xticlabels 和图例。

I tried a few script variations, but the one below was the only one I found which seems to work unchanged for both gnuplot4.6.0 (March, 2012) and gnuplot5.5.0 (July 2021).我尝试了一些脚本变体,但下面的一个是我发现的唯一一个对于 gnuplot4.6.0(2012 年 3 月)gnuplot5.5.0(2021 年 7 月)似乎都没有变化的脚本。 I don't want to say the solution is beautiful, but it works with gnuplot-only .我不想说这个解决方案很漂亮,但它适用于gnuplot-only

Data: SO22438313.dat (taken from Thor's answer)数据: SO22438313.dat (取自 Thor 的回答)

name;    n0; n1; n2; n3; n4
Benj;    1;  3;  2;  5;  3
Silv;    6;  1;  2;  3;  4
Steffi;  3;  2;  2;  4;  2
Carl;    2;  4;  5;  3;  2

Script: (works for gnuplot>=4.6.0, March 2012)脚本:(适用于 gnuplot>=4.6.0,2012 年 3 月)

### plotting rows with xticlabels and legend
reset

FILE = "SO22438318.dat"

set datafile separator ';'
set yrange[0:]
set key noautotitle
# set colorsequence classic   # optional for gnuplot>=5.0

myTitles = ''

plot FILE matrix u (c=$1):3:(r=$2) every ::1:1 w l lw 2 lc var, \
        for [i=1:c]'' u (i):(i==1?myTitles=myTitles.' '.strcol(1):0,NaN): \
            xtic($0==0?xt=strcol(i+1):xt) w l lw 2, \
        for [i=1:r] '+' u 1:(NaN) every ::0::0 w l lc i lw 2 ti word(myTitles,i+1)
### end of script

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM