Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a 2D parametric plot like the following:

ParametricPlot[
  {Sin[t], Cos[t]}, {t, 0, 2 \[Pi]},
  Frame -> True, AxesLabel -> {x, y}, AxesStyle -> Arrowheads[0.04], PlotRangePadding -> 0.2
]

wrong labels

But I would like the axes labels x, y to be positioned besides the arrowheads (i.e. y left of the vertical arrowhead and x just below the horizontal one).

Since I can't use the frame as a replacement for the axes (as the axes are in the middle, not at the edge), I believe I cannot use the approach suggested in this question.

How could I achieve this?

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

You could "abuse" Arrowheads for this purpose; it has the advantage of tracking the axes wherever they may roam:

toArrowheads[{x_, y_}, head_, size_: 0.015, xos_: {-3, -3}, yos_: {-3, 3}] := 
  {Arrowheads[{{size, 1, Graphics[{head, Text[x, xos]}]}}], 
   Arrowheads[{{size, 1, Graphics[{head, Text[y ~Rotate~ (-90 °), yos]}]}}]}

head = Polygon[{{-3, 1}, {0, 0}, {-3, -1}, {-2.2, 0}}];

ParametricPlot[{Sin[t], Cos[t]}, {t, 0, 2 π},
  Frame -> True,
  AxesStyle -> toArrowheads[Style[#, 20] & /@ {"x", "y"}, head],
  PlotRangePadding -> 0.2,
  PlotRange -> {-0.8, 1.2},
  AxesOrigin -> {-0.1, 0.3}
]

enter image description here

share|improve this answer
    
Nice, I had the same idea but I didn't know how to attach the label to Arrowhead. This time I've missed "Details and Options" :) –  Kuba Sep 6 '13 at 9:53
    
works great, thanks a lot for this! –  Bernd Sep 6 '13 at 10:24
add comment

You could use Epilog to put the labels there manually:

ParametricPlot[{Sin[t], Cos[t]}, {t, 0, 2 \[Pi]}, Frame -> True, 
    AxesStyle -> Arrowheads[0.04], PlotRangePadding -> 0.2, 
    Epilog -> {Inset["x", Scaled[{0.95, 0.48}]], Inset["y", Scaled[{0.48, 0.95}]]}]

I used Scaled so it doesn't matter how large the circle is (and works well with PlotRangePadding).

enter image description here

Asymmetrical Version - not yet profoundly tested

Based on Mr. Wizards comments, I thought about asymmetrical ideas. He showed how to "abuse" the arrowheads - I'll try something different here (note that the code can be optimized, but I keep it rather extensive to show what I was thinking):

What I do: I get the PlotRange, AxesOrigin and PlotRangePadding and calculate the position:

doPP[pp_] :=
   Module[{plotRange, plotRangePadding, axes, offset},
     plotRange = Extract[pp, Most /@ Position[pp, PlotRange]][[1, 2]];
     plotRangePadding = {-Abs@#, Abs@#} &@
        Extract[pp, Most /@ Position[pp,   PlotRangePadding]][[1, 2]];
     axes = Reverse@Extract[pp, Most /@ Position[pp, AxesOrigin]][[1, 2]];
     offset = 
        Rescale[#1, #2] & @@@ Transpose[{axes, plotRangePadding + # & /@ plotRange}];
     Show[pp, 
        Epilog -> {Inset["x", Scaled[{ 0.95, offset[[1]] - 0.02}]], 
                   Inset["y", Scaled[{offset[[2]] - 0.02, 0.95}]]}]]

where you can customize 0.02 and 0.95 to your liking (or set as options, parameters).

We can execute now:

ParametricPlot[{Sin[t], Cos[t]}, {t, 0, 2 \[Pi]}, Frame -> True, 
    AxesStyle -> Arrowheads[0.04], PlotRangePadding -> 0.2, 
    AxesOrigin -> {0.3, 0.5}, PlotRange -> {-0.2, 1.3}] // doPP

enter image description here

share|improve this answer
2  
Could you perhaps address positioning with non-centered axes from either e.g. AxesOrigin -> {-0.3, 0.5} or PlotRange -> {-0.2, 1.3}? (+1 of course) –  Mr.Wizard Sep 6 '13 at 9:20
    
3D variation mathematica.stackexchange.com/a/27004/5478 –  Kuba Sep 6 '13 at 9:25
    
thanks for the input, @Mr.Wizard! You make it sound like there is an obvious/easy way to do it... I tried something, see my edited answer. If there is something smarter, feel free to add/edit/new-answer :) –  Pinguin Dirk Sep 6 '13 at 9:57
    
@Pinguin No, nothing easy or obvious that I could think of; I gave my own method below. –  Mr.Wizard Sep 6 '13 at 9:57
    
@Mr.Wizard: ok cool, I like your approach - my idea (should it prove to be stable) works without arrowheads, at least one advantage... –  Pinguin Dirk Sep 6 '13 at 9:59
add comment

Here's another way, based on this answer by Alexey Popkov. It is a little slower than the others due to Rasterize, but it handles asymmetrical axes positions. One could also use Offset instead of {3.5, 2.} to handle offsetting the labels, or make the offsets arguments to labelaxes.

labelaxes[plot_, {xlabel_, ylabel_}] := 
 Module[{xlim, ylim, orig}, 
  Rasterize[
   Show[plot, Frame -> False, Ticks -> {(xlim = {##}) &, (ylim = {##}) &}], 
   ImageResolution -> 1];
  orig = AxesOrigin /. AbsoluteOptions[plot, AxesOrigin];
  Show[plot,
   Graphics[{
     Text[TraditionalForm[xlabel], {xlim[[2]], orig[[2]]}, {3.5, 2.}], 
     Text[TraditionalForm[ylabel], {orig[[1]], ylim[[2]]}, {3.5, 2.}]}]
   ]
  ]


labelaxes[
 ParametricPlot[{Sin[t], Cos[t]} + {0.1, 0.2}, {t, 0, 2 Pi},
  Frame -> True, AxesStyle -> Arrowheads[0.04], PlotRangePadding -> 0.2], {x, y}]

Mathematica graphics

I suppress the frame so that the axes will be shown:

labelaxes[
 ParametricPlot[{Sin[t], Cos[t]} + {2, 4}, {t, 0, 2 Pi},
  (*Frame -> True,*) AxesStyle -> Arrowheads[0.04], PlotRangePadding -> 0.2], {x, y}]

Mathematica graphics

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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