0
votes

I have a question about ploting graphics with gnuplot.

I have a .csv file, the first row has to be the y axis and the second row has to be the x axis

This is the file.csv

7.88, 7.26, 61.89, 7.00, 6.92, 6.96, 7.42
3,    4,     5,    6,    7,    8,    9

and this the code:

set datafile separator comma
plot 'file.csv' using 2:xtic(1) notitle with boxes

There are examples for plotting row-wise, but these examples do not cover this case or are using an external tool for reformatting of the data.

Using gnuplot matrix (cannot be used in this case):

Plot csv file with multiple rows using gnuplot

Plotting .csv data by row with GNUplot

Reformatting (awk and an unspecified tool):

how plot per rows in gnuplot

Gnuplot: Plot row-wise and named data as bundle of differently colored and titled lines

Can this maybe nevertheless be achieved with gnuplot only?

1
Please don't post data and code as picture. Please insert it as text. - theozh

1 Answers

0
votes

Plotting data which is arranged in rows is a recurring question or demand for gnuplot.

The short answer is: gnuplot doesn't like data in rows and prefers having it organized in columns. So, simply transpose your data. Unfortunately, gnuplot does not have a transpose function. What (gnuplot and Linux) people would say: use an external tool (e.g. awk, sed, ...) and prepare your data in such a way (here: transpose) that gnuplot can easily plot it.

If you want to do it with gnuplot only, the above links do not help since your "special" case with y-values in one row and x-values in another row is not covered.

That's why I want to suggest another "obscure" gnuplot-only solution. Admittedly, it's a bit cumbersome, but it's gnuplot only and therefore platform independent and independent of additional installations. There will be a warning during stats: Errors of slope and intercept are zero. There are as many data points as there are parameters., however, which can be ignored. This solution probably can be tuned to cover more than 2 rows.

Code:

### plotting data from two rows
reset session

$Data <<EOD
7.88, 7.26, 61.89, 7.00, 6.92, 6.96, 7.42
3.1,  4.2,   5.3,  6.4,  7.5,  8.6,  9.7
EOD

set datafile separator comma

# transposing data with 2 rows and N columns
stats $Data nooutput    # get the number of columns STATS_columns
set table $Dummy
    plot for [col=1:STATS_columns] a=0 $Data u \
        (int($0)?a=column(col):b=column(col),sprintf("%g,%g",a, b)) w table
unset table
set table $DataTransposed
    plot $Dummy u 1:(","):2 every 2::1 w table  # remove every second row
unset table
undef $Dummy

plot $DataTransposed u 1:2 w lp pt 7

### end of code

Result:

enter image description here