0

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?

2
  • 3
    Please don't post data and code as picture. Please insert it as text. Commented Jun 3, 2020 at 17:25
  • If your problem is solved then please check the acceptance mark at the answer. Any feedback would be appreciated. Commented Jul 26, 2022 at 7:33

1 Answer 1

0

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 links (I added to your question) 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 and therefore platform independent and independent of additional installations.

This solution probably can be tuned to cover more than 2 rows.

Script: (works with gnuplot>=5.0.0)

### 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
stats $Data u 0 nooutput    # get the number of columns in STATS_columns
set datafile missing "nan"
set datafile separator whitespace

set table $DataXY
    plot for [col=1:STATS_columns] v1=NaN $Data u (v0=v1,v1=column(col)):(v0) w table
unset table

plot $DataXY u 1:2 w lp pt 7
### end of script

Result:

enter image description here

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

2 Comments

Thank you very much for the reply, but I have the next errors: Line 9: Command not found Line 17: stats: Command not found Line 20: syntax error near unexpected token (' Line 9 is $Data <<EOD Line 17 is stats $Data nooutput Line 20 is (int($0)?a=column(col):b=column(col),sprintf("%g,%g",a, b)) w table Thank you
The above code is copy&paste code, it should work with gnuplot >=5.2. Which version of gnuplot are you running? Can you edit your question and post your actual code? Skip the $Data ... EOD section and replace $Data in line 12 with "file.csv". Eventually with the full path.

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.