Skip to main content

plotting - Axis label shifted in 3D plot


Does anybody know why the axis label is shifted in this plot? Probably because of the different lengths of the labels of the ticks? How can I have the axis label in the middle of the axis?


The code that generates the plot is


Plot3D[master, {l2, 0, 15}, {l3, 0, 15}, PlotRange -> {0, 0.02}, AxesLabel -> {Style[Subscript[\[Lambda], 2], "Text", Background -> White], Style[Subscript[\[Lambda], 3], "Text", Background -> White], Rotate[Text[Style["I(0,1,1,1)\n", 14]], \[Pi]/2]}, Ticks -> {Automatic, Automatic, {0, 0.01, 0.02}}]


The answer given below works for the mentioned plot. However, another problem occurs in another plot when plotting the range PlotRange -> {-11000000, 11000000} and where the labels on the axes ticks should be $−1⋅10^7$ and $1⋅10^7$ such as Ticks -> {Automatic, Automatic, {{-10000000, "-1*\!\(\*SuperscriptBox[\(10\), \(7\)]\)"}, 0, {10000000, "1*\!\(\*SuperscriptBox[\(10\), \(7\)]\)"}}}]. This does not work with the stated answer below.


Moreover, my label on the vertical axis includes subscripts, which is also not compatible with the suggestions in the answer: Text[ Style["B(\[Gamma])\!\(\*SubscriptBox[\(\[Lambda]\), \ \(2\)]\)\!\(\*SubscriptBox[\(\[Lambda]\), \(3\)]\)", 14]]}



enter image description here



Answer



I assume you are referring to the z-axis label, which is badly behaved (it tends to jump to the top or bottom of the axis on my example plot), especially at certain viewpoints:


label1


Although, by the nature of perspective in 3D plots, the axes labels will not always appear to be in the middle the the axis, here the position does seem to move out from the midpoint. My first guess was that this had to do with the interaction of the viewpoint and the Rotate applied to the label, but that doesn't seem to be the case, as an unrotated label does the same thing, so my current thought is that Mathematica's built-in label-placement function is struggling to find a clear view with the long numbers needed for the ticks.


Existing Answers


Positioning the label of the z-axis on 3D plots is a topic that has beed addressed here previously in several ways. As long as all you want is a good image of the plot to export, you could just specify the position of the label manually as a new graphic (as in this question) or via Inset with Epilog (here) or with Framed (here). However, these solutions are "static" and do not play well if you want to view your plot interactively by rotating and zooming, etc. This question shows several methods of dealing with axes labels overlapping crowded axes, but I think that the placement of the label in this instance should be addressed.


Workaround using Ticks


There is a "dynamic" work-around, but it is a bit dirty. Essentially, we can abuse Ticks to manually place a new axis label as a tick mark at the axis midpoint. Going through Ticks has the advantage that the label moves with the axis as it is repositioned when the plot is interacted with. We can define a new function for the ticks on the z-axis that includes a label (thanks to this question for the IntegerChop function):


zTicksLabel[ticks__, label_, style_: Null] := Module[

{IntegerChop, nform, nlength, zmin, zmax, t, longest},

IntegerChop = With[{r = Round[#]}, r + Chop[# - r]] &;
nform[n_] := Which[
n == 0, n,
10^-5 <= Abs[n] <= 10^5, n,
IntegerQ[Log10[Abs[n]]],
ScientificForm[N@n, NumberFormat -> (Row[{If[n < 0, "-"], 10^#3}] &)],
Divisible[n, 10^Floor@Log10@Abs[n]], ScientificForm[N@n, NumberPoint -> ""],
True, ScientificForm[n]

];
nlength = First[ImageSize /. Options[Rasterize@Invisible@nform@#]] &;

If[Length@{ticks} == 1,
t = IntegerChop@ticks;
{zmin, zmax} = {Min@t, Max@t},
{zmin, zmax} = First@{ticks};
t = IntegerChop[N@FindDivisions[{zmin, zmax}, Last@{ticks}]]
];


longest = First@MaximalBy[t, nlength];

Join[
{#, nform@#} & /@ t,
{{
Mean[{zmax, zmin}],
Row[
{Invisible@nform@longest,
If[style === Null, l, Style[l, style]] /.
{l -> Rotate[Row[{Invisible["M"], label, Invisible["M"]}], Pi/2]},

Invisible@nform@longest}, Invisible["M"]
]
}}
]
];

In response to the comments, I have made this function more sophisticated. The argument ticks, takes either:



  1. an explicit list of tick positions like {tick1, tick2, ...}, in which case only those ticks will be drawn;

  2. or the sequence {zmin, zmax}, n, in which case the function draws about n z-axis ticks between zmin and zmax according to the function FindDivisions (rather than the mysterious Mathematica default).



The argument label takes an expression (which can be anything that can be rotated and given a Style, but in most cases should be a string) and places it somewhat outside a tick in the middle. However, because the label is made with Ticks, it inherits the TickStyle options if they are specified, rather than LabelStyle, so I have left room for style options to be specified explicitly with the style argument. Also, this just rotates the label into a vertical position, rather than aligning it to the z-axis, as has been noted in the comments.


Apply it as so:


Plot3D[master, {l2, 0, 15}, {l3, 0, 15},
AxesLabel -> {Subscript[\[Lambda], 2], Subscript[\[Lambda], 3], Null},
LabelStyle -> Blue,
TicksStyle -> Red,
PlotRange -> {0, 0.02},
Ticks -> {Automatic, Automatic, zTicksLabel[{#1, #2}, 3, "I(0,1,1,1)", Orange] &}
]


label3


As a further example, to make all axes labels a larger size, we just pass FontSize -> 14 to LabelStyle and the style argument of zTicksLabel (keeping TicksStyle at size 10):


Plot3D[master, {l2, 0, 15}, {l3, 0, 15},
AxesLabel -> {Subscript[\[Lambda], 2], Subscript[\[Lambda], 3], Null},
LabelStyle -> 14,
TicksStyle -> 10,
PlotRange -> {0, 0.02},
Ticks -> {Automatic, Automatic, zTicksLabel[{#1, #2}, 3, "I(0,1,1,1)", 14] &}
]


label2


And lastly, if you only want two z-axis ticks at particular locations, do something like


Plot3D[master, {l2, 0, 15}, {l3, 0, 15},
AxesLabel -> {Subscript[\[Lambda], 2], Subscript[\[Lambda], 3], Null},
LabelStyle -> 14,
TicksStyle -> 10,
PlotRange -> {-1.1*^7, 1.1*^7},
Ticks -> {
Automatic,

Automatic,
zTicksLabel[{-10^7, 10^7},
"B(\[Gamma])\!\(\*SubscriptBox[\(\[Lambda]\), \(2\)]\)\!(\*SubscriptBox[\(\[Lambda]\), \(3\)]\)", 14] &
}
]

label4


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