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[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...