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'm trying to make a DensityHistogram in Mathematica 8 that overlays the histogram values for each location. What I want is for the value of each square to be displayed over it always (i.e. not in the StatusArea or as a Tooltip). For instance, if you run this code in Mathematica, one of the squares will have a value of 23, and I'd like that value overlaid on that square, and then the same for all the other non-zero values (I don't need a bunch of 0's).

a = {{1.6, 314.1592}, {0.8, 63.6172}, {0.4, 113.0973}, {1, 19.6349}, {1, 19.6349}, 
     {1, 50.2654}, {1, 490.8738}, {1, 19.6349}, {0.045, 17.5}, {0.3, 80}, {0.06, 120}, 
     {1, 100}, {1,78.5398}, {0.06, 120}, {1, 7.0685}, {0.33, 126.6768}, {1, 369.8361}, 
     {0.0008, 0.09}, {1, 1134.1149}, {0.06, 64}, {0.08, 120}, {1, 113.0973}, {0.19, 600},
     {1, 283.5287}, {0.00217,14}, {1, 201.0619}, {0.64, 283.5287}, {2, 1520.5308}, {60, 31.4159},
     {0.3, 10}, {1, 201.0619}, {1, 283.5287}, {2, 12.5663}, {1, 506.7074}, {0.35, 176.7145},
     {0.05, 1.1309}, {2, 1520.5308}, {0.7, 28.2743}, {1, 49}, {1, 100}, {1, 706.8583},
     {1, 706.8583}, {8, 3848.451}, {1, 50.2654}, {1, 50.2654}, {1, 314.1592}, {1, 314.1592},
     {1, 314.1592}, {1, 314.1592}, {1, 1140.0918}, {2, 314.1592}, {2, 314.1592}, 
     {1.5, 314.1592}, {1, 23.7582}, {1, 15.2053}, {0.02, 0.415}, {1.5, 314.1592}, 
     {1, 254.469}, {0.02, 0.415}, {1, 346.3605}, {0.5, 254.469}, {1, 95.0331}}

DensityHistogram[ a, {{{.0001, .001, .01, .1, 1, 10, 100}}, 
                      {{.01, .1, 1, 10, 100, 1000, 10000}}}, ScalingFunctions -> {"Log", "Log"},
                  ColorFunction -> (ColorData["SolarColors"][1 - #] &), 
                  ChartBaseStyle -> EdgeForm[Thin], BaseStyle -> {FontFamily -> "Arial"},
                  Method -> {"DistributionAxes" -> "Histogram"},
                  Ticks -> {
                    Table[{10^k, Superscript[10, k]}, {k, -4, 4}], 
                    Table[{10^k, Superscript[10, k]}, {k, -2, 4}]}, 
                  Frame -> {True, True, False, False}, Mesh -> True, ImageSize -> 600, 
                  LabelingFunction -> (Placed[#1, Above] &)]

I'm not sure if I'm using LabelingFunction correctly, probably not. I'd also like to see the relevant values on the axes histograms, if possible.

share|improve this question
    
There is a comment at the docs that says "Use automatic labeling by values through Tooltip and StatusArea:". So probably LabelingFunction[] does not support what you want –  belisarius Dec 28 '13 at 0:47
    
Ok, if that is the case, what is the best way to go about displaying the values? –  iwantmyphd81 Dec 28 '13 at 0:49
add comment

1 Answer

up vote 6 down vote accepted

You can use ChartElementFunction:

For Version 9:

f[{{xmin_, xmax_}, {ymin_, ymax_}}, a_ , b_] := 
                 {Rectangle[{xmin, ymin}, {xmax, ymax}, RoundingRadius -> 0.2], 
                 Text[Style[Length@a, Large, Bold, Black], Mean /@ {{xmin, xmax}, {ymin, ymax}}]}

DensityHistogram[a, {{{.0001, .001, .01, .1, 1, 10, 100}}, {{.01, .1, 1, 10, 100, 1000, 10000}}},
 ChartElementFunction -> f,
 ScalingFunctions -> {"Log", "Log"}, 
 ColorFunction -> (ColorData["SolarColors"][1 - #] &), 
 ChartBaseStyle -> EdgeForm[Thin],
 BaseStyle -> {FontFamily -> "Arial"}, 
 Method -> {"DistributionAxes" -> "Histogram"}, 
 Ticks -> {Table[{10^k, Superscript[10, k]}, {k, -4, 4}], 
           Table[{10^k, Superscript[10, k]}, {k, -2, 4}]}, 
 Frame -> {True, True, False, False},
 Mesh -> True, ImageSize -> 600]  

Mathematica graphics

For Version 8 (but greater than 8.0):

d= DensityHistogram[ a, {{{.0001, .001, .01, .1, 1, 10, 100}}, {{.01, .1, 1, 10, 100, 1000, 10000}}}, 
    ScalingFunctions -> {"Log", "Log"}, 
    ColorFunction -> (ColorData["SolarColors"][1 - #] &), 
    ChartBaseStyle -> EdgeForm[Thin], 
    BaseStyle -> {FontFamily -> "Arial"}, 
    Ticks -> {Table[{10^k, Superscript[10, k]}, {k, -4, 4}], 
              Table[{10^k, Superscript[10, k]}, {k, -2, 4}]}, 
    Frame -> {True, True, False, False}, Mesh -> True, 
    ImageSize -> 600] // FullForm;
makeText[{a_, l_List}] := Text[Style[a, Large, Bold, Black], Mean /@ Transpose[#]] & /@ l
epilog = makeText /@ Cases[d, Tooltip[a_, b_, ___] :> 
         {b, Cases[a, RectangleBox[c__] :> {c}, Infinity]}, Infinity];

DensityHistogram[a, {{{.0001, .001, .01, .1, 1, 10, 100}}, {{.01, .1, 1, 10, 100, 1000, 10000}}}, 
 ScalingFunctions -> {"Log", "Log"}, 
 ColorFunction -> (ColorData["SolarColors"][1 - #] &), 
 ChartBaseStyle -> EdgeForm[Thin], 
 BaseStyle -> {FontFamily -> "Arial"}, 
 Method -> {"DistributionAxes" -> "Histogram"}, 
 Ticks -> {Table[{10^k, Superscript[10, k]}, {k, -4, 4}], 
           Table[{10^k, Superscript[10, k]}, {k, -2, 4}]}, 
 Frame -> {True, True, False, False}, Mesh -> True, ImageSize -> 600, 
 Epilog -> epilog, LabelingFunction -> None]
share|improve this answer
    
Looks cool! I copied your code but must be doing something wrong. I just get 0's in the fields. I'll keep messing with it, but if there's something I'm doing wrong, please let me know. Thanks! –  iwantmyphd81 Dec 28 '13 at 4:18
    
@iwantmyphd81 Probably you forgot to define your data (list a = ...)` –  belisarius Dec 28 '13 at 4:20
    
Just checked, my list is there and defined properly. –  iwantmyphd81 Dec 28 '13 at 4:23
    
@iwantmyphd81 ChartElementFunction was first introduced in Version 7, but it seems that they modified it in V9 to pass the data points in each region. In version 8 this feature isn't present, so it isn't able to calculate the results. sorry. –  belisarius Dec 28 '13 at 4:44
1  
Nevermind, I used IntegerPart inside Text[Style[a, Large, Bold, Black] to get rid of the decimal points, so it now reads: Text[Style[IntegerPart[a], Large, Bold, Black]. Thanks! –  iwantmyphd81 Dec 28 '13 at 23:46
show 2 more comments

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.