12
$\begingroup$

Some of my 2-dimensional data are displayed with a code similar to this:

Needs["PlotLegends`"]
sampleData = 
Transpose[{RandomVariate[NormalDistribution[0, 3*10^-6], 20000], 
RandomVariate [NormalDistribution[0, 3*10^-6], 20000]}]; 
binning = {{-1*10^-5, 1*10^-5, 1*10^-7}, {-1*10^-5, 1*10^-5,1*10^-7}};
(*binning of my sampleData*)
maxBinnedData=Max[HistogramList[sampleData,binning][[2]]];
(*seachring for the maximum of the binned data*)
ShowLegend[DensityHistogram[sampleData,binning,LabelingFunction->None,
PerformanceGoal->"Speed", 
ColorFunction->(If[1-#1===0,White,ColorData["Rainbow"][#1]]&),
ColorFunctionScaling->True,PlotRange->{{-1*10^-5, 1*10^-5},{-1*10^-5, 1*10^-5}}, 
FrameLabel->{"x", "y"},
LabelStyle->Directive[Black,FontSize->12,FontFamily->"Arial"],ImageSize->{500,500}],{(If[1-#1===0,White,ColorData["Rainbow"][1-#1]]&),11,
ToString[maxBinnedData],ToString[0],LegendTextOffset->{-2, 0},
LegendPosition->{1.1,-0.4},LegendShadow->None,
LegendLabel->Style["Counts",Black,FontSize->12,FontFamily->"Arial"]}]

The output is quite nice for my taste (see figure).Output of my code

Yet, I want to improve two things, but all my attempts have been unsuccessfully so far.

First: How can I manage that the ticks are all in scientific form (e.g. $1.0\times10^{-5}$ instead of 0.00001)? Somehow I was not able to adapt the tricks of e.g. this post to my problem. I could do it perhaps by hand, but the scale is often different (e.g. from $-2.4 \times 10^{-5}$ to $4.0 \times 10^{-6}$) so that this will be unhandy.

Second: As perhaps can be seen, I try to use for all names and numbers the FontSize->12 and the FontFamily->“Arial”. That works fine except for the numbers in my legend (0 and 20). How do I have to change the font setting there? I tried things like Style[ToString[maxBinnedData], Black, FontSize -> 12, FontFamily -> "Arial"] instead of ToString[maxBinnedData] but nothing worked.

I would be happy if some people here could give me some hints!

$\endgroup$

3 Answers 3

8
$\begingroup$

For the first part of your question, you can specify a function for the FrameTicks or Ticks option. This function will then be applied to xmin and xmax (or ymin and ymax for the tick marks along the vertical axis). This means that you can let this function take care of the placement of the tick marks automatically without having to worry about different plot ranges. For example, you could do something like

ticks[ndiv_Integer: 5, nsubdiv_Integer: 5, 
   label_: (ScientificForm[N[#], 3] &)][xmin_, xmax_] := 
 With[{div = FindDivisions[{xmin, xmax}, {ndiv, nsubdiv}], 
   labelf = If[label === None, ("" &), label]},
  Join[{#, labelf[#], {0.01, 0}} & /@ div[[1]], 
    {#, "", {.00625, 0}} & /@ Flatten[div[[2, All, 2 ;; -2]]]]]

Here, ndiv is the (approximate) number of major tick marks, nsubdiv the number of subdivisions between the the major tick marks, and label a function for specifying the formatting the tick labels. I've chosen label to be equal to ScientificForm[N[#], 3] & by default. You can also specify None if you don't want tick labels.

Usage

For the plot above, you would get something like

pl = DensityHistogram[sampleData, binning, LabelingFunction -> None, 
  PerformanceGoal -> "Speed", 
  ColorFunction -> (If[1 - #1 === 0, White, 
      ColorData["Rainbow"][#1]] &), ColorFunctionScaling -> True, 
  PlotRange -> {{-1.024*10^-5, 1*10^-5}, {-1*10^-5, 1*10^-5}}, 
  FrameLabel -> {"x", "y"},
  LabelStyle -> Directive[Black, FontSize -> 12, FontFamily -> "Arial"], 
  ImageSize -> {500, 500}];

Show[pl, FrameTicks -> {{ticks[], ticks[None]}, {ticks[], ticks[None]}}]

Mathematica graphics

Edit

To change the style of the numbering in the legend, you could use the BaseStyle option (in ShowLegend), for example

With[{label = ticks[(If[# === 0, 0, ScientificForm[N[#], 3]] &)]},
 ShowLegend[
  Show[pl, FrameTicks -> {{label, ticks[None]}, {label, ticks[None]}}], 
   {(If[1 - #1 === 0, White, ColorData["Rainbow"][1 - #1]] &), 11,
   ToString[maxBinnedData], 
   ToString[0], LegendTextOffset -> {-1.3, 0}, 
   LegendPosition -> {1.1, -0.4},
   LegendShadow -> None,
   LegendLabel -> Style["Counts", Black, FontSize -> 12, FontFamily -> "Arial"],
   BaseStyle -> {FontFamily -> "Arial", FontSize -> 12}
   }]]

Mathematica graphics

Note that I also got rid of the dot at 0. by using a slightly different function for typesetting the tick labels in ticks.

$\endgroup$
3
  • $\begingroup$ Thanks a lot for both answers (Heike and @belisarius). Both are working fine. If I use e.g. ShowLegend[Show[hist, FrameTicks -> s1],{rest of my code}], I have even the legend again. Now I hope that somebody can help me with the second question. By the way, although it is not really important for me, how could I get rid of the dot at 0.? $\endgroup$
    – partial81
    Commented Jun 11, 2012 at 15:57
  • $\begingroup$ @partial81 I've updated my answer $\endgroup$
    – Heike
    Commented Jun 11, 2012 at 18:47
  • $\begingroup$ Dear @Heike, Thanks for editing your post! Now I have the plot I wanted to have. With your solution and with a bit of work, it is even easy to have e.g. for the x-axis scientific notation and for the y-axis the number form. Additionally, I can change the ticks lengths! That is really a nice addition! Not to forget that you got rid of the dot at the 0.! Thanks for this great help! $\endgroup$
    – partial81
    Commented Jun 12, 2012 at 7:27
5
$\begingroup$

For your first question:

sampleData = Transpose[{RandomVariate[NormalDistribution[0, 3*10^-6], 200], 
                        RandomVariate[NormalDistribution[0, 3*10^-6], 200]}]; 
hist = 
 DensityHistogram[sampleData, binning, LabelingFunction -> None, 
  PerformanceGoal -> "Speed", ColorFunction -> (If[1 - #1 === 0, White, 
      ColorData["Rainbow"][#1]] &), ColorFunctionScaling -> True, 
  PlotRange -> {{-1*10^-5, 1*10^-5}, {-1*10^-5, 1*10^-5}},    FrameLabel -> {"x", "y"}, 
  LabelStyle -> Directive[Black, FontSize -> 12, FontFamily -> "Arial"], 
  ImageSize -> {500, 500}];

binning = {{-1*10^-5, 1*10^-5, 1*10^-6}, {-1*10^-5, 1*10^-5, 1*10^-6}};
(*binning of my sampleData*)
maxBinnedData = Max[HistogramList[sampleData, binning][[2]]];

(*Here comes the trick*)
s = AbsoluteOptions[hist, FrameTicks] /. {o_ -> s_List} :> s;
s1 = s /. {v__, w__, h__, j__} :> {(MapAt[ScientificForm, #, {2}] & /@  v), 
                                   (MapAt[ScientificForm, #, {2}] & /@ w), h, j};
Show[hist, FrameTicks -> s1]

enter image description here

$\endgroup$
1
  • $\begingroup$ Dear @belisarius, Thanks for solving the first part of my question. The second part was well answered from Heike, and halirutan has given a nice solution too. $\endgroup$
    – partial81
    Commented Jun 12, 2012 at 7:32
4
$\begingroup$

You could do everything in post-processing by replacing parts of your output graphics. Your "Arial"-problem seems to come from some strange behavior inside the PlotLegends` package which does not let you give Style-ized text. Here you can simply replace every Text-directive which does not use style. The rule for that looks lik

rule1 = Text[str_String, rest___] :> 
        Text[Style[str, FontSize -> 12, FontFamily -> "Arial"], rest]

rest (and all the rests I'll use later) are always some parameters/options which may or may not follow and which I simply append again.

For the replacement-rule of the FrameTicks you have to inspect the final graphics a bit, because ShowLegend includes your original plot as Inset in the final Graphics. Therefore, I replace the graphics in the first Inset by the same graphics with altered FrameTicks. For this I extract with AbsoluteOptions the values for the ticks and wrap ScientificForm around. With this information even the slightly complex rule should be readable

rule2 = Graphics[{Inset[densGr_, rest1___], rest2___}, rest3___] :> 
 Graphics[{Inset[
    densGr /. (FrameTicks -> _) -> 
      (FrameTicks -> Last@Last[(AbsoluteOptions[densGr, FrameTicks] /. 
        {a_?NumericQ, b_?NumericQ, c_List, d_List} :> 
        {a, ScientificForm[b], c, d})]), 
     rest1], 
   rest2}, rest3]

Finally, you just have to use these replacement rules with your graphics, which I called gr here

gr /. rule1 /. rule2

enter image description here

$\endgroup$
1
  • $\begingroup$ Dear @halirutan, Thanks for this easy solution. It is almost as good as the one from Heike! I really appreciate that you showed a totally different way to get almost the same desired plot! $\endgroup$
    – partial81
    Commented Jun 12, 2012 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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