Skip to main content

graphics - Adapt ticks in plot and font in legend


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!



Answer



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.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

list manipulation - Selecting multiple columns from a matrix?

Sample data: data = { {{2013, 1, 1}, 24.13, 167.67, 231.82}, {{2013, 1, 2}, 32.15, 170.92, 225.99}, {{2013, 1, 3}, 35.43, 172.68, 221.67}, {{2013, 1, 4}, 36.73, 173.05, 218.32}, {{2013, 1, 5}, 58.19, 165.96, 197.05}, {{2013, 1, 6}, 69.99, 163.50, 187.52}, {{2013, 1, 7}, 71.37, 154.21, 175.58}, {{2013, 1, 8}, 72.51, 149.66, 163.25}}; I want a DateListPlot with three graphs, so for a matrix formed by columns 1 and 2, one for columns 1 and 3, and 1 for columns 1 and 4. At the moment I'm using this code: data2 = Transpose[{data[[All, 1]], data[[All, 2]]}]; data3 = Transpose[{data[[All, 1]], data[[All, 3]]}]; data4 = Transpose[{data[[All, 1]], data[[All, 4]]}]; DateListPlot[{data2, data3, data4}, Joined -> True, Filling -> {3 -> {1}}] but I have a hunch that this can be done more efficiently. I don't like the Transpose s in particular. Any ideas? edit (for extra credit) What if I need to multiply the second column by 2, which in my solution is simp...

calculus and analysis - Integral of the Sinc product

Let us consider the following integral $$ B_n = \int_0^\infty \prod_{k=1,3,5,\dots}^n\frac{\sin (x/k)}{x/k}dx $$ By definition, Sinc[x] == Sin[x]/x , therefore B[n_?OddQ] := Integrate[Product[Sinc[x/k], {k, 1, n, 2}], {x, 0, Infinity}] B /@ Range[1, 13, 2] {Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2} OK, everything is fine. But... B[15] 467807924713440738696537864469 Pi/935615849440640907310521750000 What's going on? Answer As Eckhard wrote in comments B[n] is the n-th Borwein integral . (The letter B was not accidental :) ) This funny properties of Borwein integrals is related to the Fourier transform of Sinc function FourierTransform[Sinc[x], x, k] 1/2 Sqrt[Pi/2] (Sign[1 - k] + Sign[1 + k]) Plot[%, {k, -2, 2}, Filling -> 0] which is the box function. The result is $\pi/2$ while the sum $$ 1/3+1/5+\dots+1/n $$ B_n = \frac{\pi}{2} - \pi \bigg(\sum_{k=3,5,\ldots}^n\frac{1}{k} -1\biggr)^\frac{n-1}{2}\prod_{k=3,5,\dots}^n\frac{k}{k-1}. $$ For $n=15$ it is equal to $$ \frac{46780...