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 - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1....