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 1107 and 1107 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

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...