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?

  • $\begingroup$ Can you use this? stackoverflow.com/questions/8696159/… $\endgroup$ – C. E. Dec 4 '13 at 17:06
  • $\begingroup$ 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? $\endgroup$ – Yves Klett Dec 4 '13 at 17:19

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"]
  • $\begingroup$ yes, there is only the further need of a blank line between different blocks $\endgroup$ – psmith Dec 4 '13 at 17:31
  • $\begingroup$ it doesn't work, it puts the blank line in the wrong place. $\endgroup$ – psmith Dec 4 '13 at 18:14
  • $\begingroup$ Ok, Fixed that.. the automatic dimensions are not square and I used the wrong dimension in Riffle. $\endgroup$ – george2079 Dec 4 '13 at 18:59
  • $\begingroup$ 0k perfect, thanks $\endgroup$ – psmith Dec 4 '13 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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