Take the 2-minute tour ×
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? stackoverflow.com/questions/8696159/… –  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 1 down vote accepted

this is how you get your data..

example = 
   Transpose@{RandomVariate[NormalDistribution[0, 1], 1000], 
              RandomVariate[NormalDistribution[0, 2], 1000]};
Histogram3D[example]
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];
ListPlot3D[data]

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:

Export["test.txt", 
      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 –  Fabrizio Dec 4 '13 at 17:31
    
it doesn't work, it puts the blank line in the wrong place. –  Fabrizio 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 –  Fabrizio Dec 4 '13 at 19:01

Your Answer

 
discard

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.