11
$\begingroup$

I need to change the position of my axes labels such that the y-axis label is in the middle and parallel to the y-axis. I'd also like the x-axis label to be in the middle of the x-axis rather than pushed to the far right. I've read a few posts on this topic (related_post1, related_post2) but they all don't seem to help in this instance. This is slightly complicated by the fact that I deliberately want my x-axis at the top rather than at the bottom. I really don't want to frame the plot in this case but if I really have to, how do I move the x-axis to the top of the frame rather than at the bottom?

Below is my code:

Plot[-x^2 - 4, {x, 0, 5}, ImageSize -> 500, AxesOrigin -> {0, -1}, 
AxesLabel -> {"x-axis", "y-axis"} ]

Any assistance would be highly appreciated.

$\endgroup$
  • 1
    $\begingroup$ Labeled[Plot[-x^2 - 4, {x, 0, 5}, ImageSize -> 500, AxesOrigin -> {0, -1}], {"Y axis", "X Axis"}, {Left, Top}] ? $\endgroup$ – Dr. belisarius Jul 2 '15 at 16:42
  • $\begingroup$ Thanks for your post. The only little thing is that my y-axis label is actually meant to be parallel to the y-axis-I shall edit my post to include this. Any ideas? $\endgroup$ – John_dydx Jul 2 '15 at 16:49
  • $\begingroup$ I believe it's important to point out that there seems to be no way to place AxesLabel labels. The docs for AxesLabel teasingly say "By default, axes labels in two-dimensional graphics are placed at the ends of the axes ", implying that they can be moved. But I've not found that to be the case. Use FrameLabel labels, or Labeled labels, as the fine answers suggest. $\endgroup$ – Chris Nadovich Jul 8 '17 at 3:23
11
$\begingroup$

If your objection to framing the plot is that you don't want the full frame box, you can get around that by specifying specific directives for each side in Frame and the related options:

Plot[-x^2 - 4, {x, 0, 5},
 ImageSize -> 500,
 Axes -> False,
 Frame -> {{True, False}, {False, True}},
 FrameLabel -> {{"y-axis", None}, {None, "x-axis"}},
 FrameTicks -> All]

plot

$\endgroup$
  • $\begingroup$ This is great, thanks. $\endgroup$ – John_dydx Jul 2 '15 at 16:51
  • $\begingroup$ You can use the shorter FrameTicks -> All with the same effect. $\endgroup$ – Mr.Wizard Jul 3 '15 at 1:52
  • $\begingroup$ @Mr.Wizard, thanks for the tip! $\endgroup$ – Virgil Jul 3 '15 at 17:05
9
$\begingroup$
Labeled[Plot[-x^2 - 4, {x, 0, 5}, ImageSize -> 500,  AxesOrigin -> {0, -1}],
        {"Y axis", "X Axis"}, {Left, Top}, RotateLabel -> True]

Mathematica graphics

$\endgroup$
  • $\begingroup$ This answer is definitely a lot simpler to understand codewise. Thanks! $\endgroup$ – John_dydx Jul 2 '15 at 17:00
  • $\begingroup$ This a great answer. Is it possible to adjust the distance between the labels and the axes? For example, if I want the "X Axis" to be closer to the axis at the top in your picture, how to do that? $\endgroup$ – nanjun Jan 23 '17 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.