I know how to make a histogram in gnuplot, but all the instructions I've seen are valid when the data is located in a single file. My problem is that my data spans several files. Is there any way to do this with gnuplot
?
3 Answers
Not really. Gnuplot is really only good at dealing with one file at a time. However, there are many useful external tools which could combine the files together for you:
plot "<magic_external_tool file1 file2 file3 file4 ..." ...
of course your choice of external tool is a question of how your files are formatted already. If the standbys (awk
, sed
, join
, cat
...) don't work, I often rely on python
to write a quick script to do the job, but I'm sure many other people would use something like perl
or java
. It really doesn't matter what tool you use, as long as you can coax it to write a properly formatted file to standard output, you're golden.

- 300,191
- 65
- 633
- 696
-
1Thanks, I saw this answer coming so I went ahead and used `Fortran` to merge the files. Cheers. – Gabriel Aug 23 '12 at 00:24
-
@Gabriel -- Fortran works :). I still use it frequently as well, but not for tasks like this... – mgilson Aug 23 '12 at 00:41
As mgilson says, this isn't really possible in gnuplot.
One thing you might want to consider (and I'm biased here as I'm an author of the project) is Pyxplot http://www.pyxplot.org.uk, which was written by a group of gnuplot users who were frustrated with problems like this. Like gnuplot, it's free and open source, and it's syntax is virtually identical.
For your needs, it has a histogram command, which produces a mathematical function representing a histogram computed from supplied data (see http://pyxplot.org.uk/current/doc/html/sec-histogram.html). You can do something along the lines of:
histogram f1() 'file1.dat'
histogram f2() 'file1.dat'
histogram f3() 'file1.dat'
plot f1(x) + f2(x) + f3(x)

- 224
- 3
- 2
-
Thank you Dominic! I just installed `Pyxplot` and let me tell you, it looks **very** interesting. You almost have me sold with the H-R diagram in the examples (I'm using **lots** of them in my Thesis), so I'll make you two questions to see if I should ditch `gnuplot` altogether: 1- How would I plot individual points with `Pyxplot`? and 2- Can I draw a _horizontal_ histogram with it? These are both big issues for me with `gnuplot` and if you guys managed to work them out, I might just be forced to make the switch :) – Gabriel Aug 30 '12 at 17:33
-
1Certainly, you can do both these things. 2) To make a horizontal histogram, just swap the axes with `axes yx`, e.g. `plot f(x) axes yx`. Not sure what you mean by 1), but you can do, e.g. `fsum(x)=f1(x)+f2(x)+f3(x)` followed by `print fsum(1)` or `set samples 10 ; plot fsum(x) with points`. – Dominic Ford Aug 30 '12 at 17:43
-
Great! I just tried the horizontal histogram and it works as described (except the axis that counts the number of elements in the bars appears to be multiplied by 10, bug?) By point 1- I mean this: http://www.paranoid-media.de/blog/?p=17 Anyway, you've helped me *a lot* and I'm digressing. I'll certainly keep `pyxplot` at hand from now on. Cheers and thank you again! – Gabriel Aug 30 '12 at 19:14
-
1I suspect the problem you're having with the histogram is that Pyxplot is giving you the number *density* of points, not the number of points (I think this is more meaningful as you can have variable-width bins without distorting the data). See the second-to-last paragraph of http://pyxplot.org.uk/current/doc/html/sec-histogram.html. In short, if you want the number of points, multiply your histogram by your bin width. – Dominic Ford Aug 30 '12 at 19:32
-
You are 100% correct Sir and now I wish I had found out about `pyxplot` a couple of months ago. – Gabriel Aug 30 '12 at 19:40
It seems to me that you can indeed do this using just Gnuplot, I just did it. The solution I used can be found here: https://stackoverflow.com/a/11092650/448700

- 1
- 1

- 603
- 7
- 13