LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-07-2011, 10:26 PM   #1
pravas
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
GNUPLOT help needed


Hello linux users,

I have a problem in plotting a file in gnuplot. The part of the file "abc.dat" looks like this.


1 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
2 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
3 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
5 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
6 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
7 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
8 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
9 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1
10 5 3 5 3 5 3 1 3 1 3 1 5 3 5 3 5 3 1 3 1 3 1

I want to assign a color to number 5, 3 and 1 and plot them from a file which has more than 500 columns. The yscale is [1:10] and the xscale is very large [0:500]. The figure should be filled with colored boxes like the table.

Please help me in finding a solution to this problem.
Thanks a lot.

Pravas
 
Old 12-08-2011, 11:29 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
First, convert your data from table form, to a list of points describing the centerpoint of each box. In your case, this means one list to describe the column and row for each '1' value, another list to describe the column and row for each '3', and another for each '5'.

The following awk snippet will use the first column as Y coordinates, and the column number (less one) as the X coordinate, and output the coordinates for each value into a separate file named out.value (but only if value starts with a sign, digit, or letter, followed by dots, digits, or letters, but no other characters, so that there is no danger in creating a file by that name):
Code:
awk '{ for (i = 2; i <= NF; i++) if ($i ~ /^[-+0-9A-Za-z][.0-9A-Za-z]*$/) printf("%d %d\n", i-1, $1) >> ("out." $i) }' abc.dat
In your case, this should create out.1, out.2, and out.3 files in the current directory. If you had only data fields in the file, and wanted to use the column and row numbers and X and Y coordinates, you can use
Code:
awk '{ for (i = 1; i <= NF; i++) if ($i ~ /^[-+0-9A-Za-z][.0-9A-Za-z]*$/) printf("%d %d\n", i, NR) >> ("out." $i) }' abc.dat
After you have those files generated, use the boxxyerrorbars style in Gnuplot to plot these point lists. You'll want to set the error bars at 0.5 for all samples, so the boxes will butt each other -- or maybe a bit less, if you want space between the boxes. Remember that we used integer coordinates, therefore each box should extend 0.5 units along each axis to fill their "grid cell". Here is an example using 0.4 (and therefore 2*(0.5-0.4) = 0.2 = 20% space between boxes), in Gnuplot:
Code:
plot 'out.1' u 1:2:(0.4):(0.4) w boxxyerrorbars t '1' lc rgbcolor "#3366ff" fillstyle solid, \
     'out.3' u 1:2:(0.4):(0.4) w boxxyerrorbars t '3' lc rgbcolor "#66ff33" fillstyle solid, \
     'out.5' u 1:2:(0.4):(0.4) w boxxyerrorbars t '5' lc rgbcolor "#ff6633" fillstyle solid
If you don't want the key (legend), just use '' instead of '1', '3' and '5' . I recommend specifying the colors using #RRGGBB (just like in HTML and CSS files), but you can also use some names instead; use the show colornames command in Gnuplot to see them.

If you want each box to have an outline, use empty fillstyle to draw the outer box on top:
Code:
plot 'out.1' u 1:2:(0.4):(0.4) w boxxyerrorbars t '1' lc rgbcolor "#3366ff" fillstyle solid, \
     'out.1' u 1:2:(0.4):(0.4) w boxxyerrorbars t ''  lc rgbcolor "#000066" fillstyle empty, \
     'out.3' u 1:2:(0.4):(0.4) w boxxyerrorbars t '3' lc rgbcolor "#66ff33" fillstyle solid, \
     'out.3' u 1:2:(0.4):(0.4) w boxxyerrorbars t ''  lc rgbcolor "#006600" fillstyle empty, \
     'out.5' u 1:2:(0.4):(0.4) w boxxyerrorbars t '5' lc rgbcolor "#ff6633" fillstyle solid, \
     'out.5' u 1:2:(0.4):(0.4) w boxxyerrorbars t ''  lc rgbcolor "#660000" fillstyle empty
 
Old 12-08-2011, 06:06 PM   #3
pravas
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks a lot for the help. Now it works fine.
I also found another solution

unset key
unset colorbox
set xrange [0:20]
set yrange [0:20]
set palette defined (0 "black", 1 "green",2 "yellow", 3 "red", 4 "blue", 5 "brown")
set view map
splot "abc.dat" matrix using ($1+1)$2+1):3 with image

I need another help in making the array of columns from a single continuous column.
A 1
B 3
C 4
A 2
B 5
C 6
A 2
B 4
C 8
>
1 2 2
3 5 4
4 6 8
The input file is very long and consists of 500 repeats of a column size of 100.
Any help will be highly appreciated.
Thanks.
 
Old 12-09-2011, 01:24 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Right, I forgot about the matrix input flag. The histograms I've done have needed further visual cues (extra outlines on certain values, labels and so on), so the boxxyerrorbars using vector data (as opposed to matrix data as you used) was more intuitive for me.

Note that the matrix input flag uses line numbers, not the first field of your input. Your example data skips row 4. The method I outlined uses the first fields, and thus the row 4 is missing from my data plot. Using the matrix input flag means the entire file is used as a matrix; the first fields are just one more data value to be plotted.

Quote:
Originally Posted by pravas View Post
A 1
B 3
C 4
A 2
B 5
C 6
A 2
B 4
C 8
>
1 2 2
3 5 4
4 6 8
I recommend using awk:
Code:
awk '{ if ($1 in row) {
          row[$1] = row[$1] " " $2
       } else {
          key[++rows] = $1 ;
          row[$1] = $2
       }
     }
 END {
       for (i = 1; i <= rows; i++)
          print row[key[i]]
     }' input-file > output-file
The rows are output in the same order as the first fields first occurrences in the file. The script assumes that all elements are defined; it does not add "empty" values if one is skipped in the file.

I thought about sorting the data, and using a simple shell loop or sed to remove the duplicate row identifiers, too. The problem is in sorting: You'd need a stable sort (sort that keeps rows with equivalent keys in the same order that they occur in the input) that only considers the first field. The sort command considers the entire input line, so you'd need do some tricks to do such a sort. (Namely, swap the fields, then sort by second field.) Unfortunately, your input happens to be one that sorts correctly (does not need a stable sort), but data such as
Code:
A 9
B 8
C 7
A 2
B 1
C 6
A 3
B 4
C 5
shows the problem if you try a sort-based solution. The awk snippet handles this correctly, of course.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About gnuplot 3.7 DaisyTZA Red Hat 1 01-24-2011 12:17 PM
gnuplot 4.0.0 ines Linux - Software 1 04-28-2005 03:26 PM
Gnuplot allycavs Linux - Software 5 02-04-2005 08:31 AM
need help for gnuplot eph Linux - Newbie 1 08-23-2004 09:41 AM
about gnuplot Rex_chaos Linux - Software 0 09-25-2003 09:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration