Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to export the data of a three dimensional histogram in the format

#   x     y     z  

with a blank line between different lines, so that one can plot the histogram in gnuplot simply using

splot 'data.dat' u 1:2:3 w l

What is the right way to obtain that?

share|improve this question
Can you use this?… –  Pickett Dec 4 '13 at 17:06
You could add some code to generate the data you refer to. It should be a fairly trivial task for Export - did you already try something along this line? –  Yves Klett Dec 4 '13 at 17:19

1 Answer 1

up vote 2 down vote accepted

this is how you get your data..

example = 
   Transpose@{RandomVariate[NormalDistribution[0, 1], 1000], 
              RandomVariate[NormalDistribution[0, 2], 1000]};
list = HistogramList[example];
x = list[[1, 1]];
y = list[[1, 2]];
data = Flatten[
      MapIndexed[ {x[[#2[[1]]]], y[[#2[[2]]]], #1} &, list[[2]] , {2}], 1];

now exporting to a gnuplot form..

Export["test.txt", data // N, "Table"]

I dont have gnuplot on this machine to test but it should be close

Edit.. generalization to higher dimensions:

dim = TensorRank[Last@list];
data = Flatten[MapIndexed[ 
         Append[Table[(First@list)[[k, #2[[k]]]], {k, dim}], #1] &, 
         Last@list ,{dim}], dim - 1];

Adding blanks, and a header comment:

      Prepend[Riffle[N@data, "  ", 
              1 + Last@Dimensions@Last@list], {"# x y z"}], "Table"]
share|improve this answer
yes, there is only the further need of a blank line between different blocks –  psmith Dec 4 '13 at 17:31
it doesn't work, it puts the blank line in the wrong place. –  psmith Dec 4 '13 at 18:14
Ok, Fixed that.. the automatic dimensions are not square and I used the wrong dimension in Riffle. –  george2079 Dec 4 '13 at 18:59
0k perfect, thanks –  psmith Dec 4 '13 at 19:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.