3
$\begingroup$

I have made a figure with this code, where multiple plots are shown together:

Show[{
  Plot[-x, {x, 0, 1}, PlotStyle -> Directive[Thick]],
  Plot[-x + 1, {x, 1, 2}, PlotStyle -> Directive[Thick]],
  Plot[-x + 2, {x, 2, 3}, PlotStyle -> Directive[Thick]]
},
 PlotRange -> All, 
 AxesLabel -> {l, v[x]}, 
 LabelStyle -> Directive[FontSize -> 12], 
 GridLines -> Automatic, 
 GridLinesStyle -> Directive[Gray, Dashed], 
 ImageSize -> {500}, 
 PlotRangePadding -> 0]

Now, the result looks like:

plot 1

As you can see, the plot shows the x-axis ticks labels in the middle of gridlines and plot curves. I would rather prefer, instead, that my labels for the x axis were above the axis. How can I achieve that? Is there a general command to move the labels "at will"?

I had previously a look to this post: Is it possible to position ticklabels on the negative y axis on its right side?

but with no results.

Edit: For sake of explaining myself better, I am adding some more info.. I am looking for general (and possibly simple) way to solve the problem of ticks label positioning.

In fact, for another case as

Show[{Plot[-6 x + 3, {x, 0, 1}, PlotStyle -> Directive[Thick]],
      Plot[-6 x + 9, {x, 1, 2}, PlotStyle -> Directive[Thick]],
      Plot[-6 x + 15, {x, 2, 3}, PlotStyle -> Directive[Thick]]},
      PlotRange -> All, AxesLabel -> {l, T[x]}, 
      LabelStyle -> Directive[FontSize -> 12], GridLines -> Automatic, 
      GridLinesStyle -> Directive[Gray, Dotted], ImageSize -> {500}]

it gives:

Plot 2

In that case, using frame instead of axes gives the undesired effect of having the x-axis offset from the (0,0). Simply rewriting labels will have the drawback of not having ticks.

I understand that in some way one should extract the ticks and the text separately from AbsoluteOptions, and then maintaining ticks and moving the labels (all together or one by one) in another position with respect to the standard one. I can extract ticks from:

p = Show[{Plot[-6 x + 3, {x, 0, 1}, PlotStyle -> Directive[Thick]], 
          Plot[-6 x + 9, {x, 1, 2}, PlotStyle -> Directive[Thick]], 
          Plot[-6 x + 15, {x, 2, 3}, PlotStyle -> Directive[Thick]]}];

myticks = Ticks /. AbsoluteOptions[p]

However I do not know how to do exactly the repositioning.

$\endgroup$
3
$\begingroup$

What about this approach:

    Show[{Plot[-x, {x, 0, 1}, PlotStyle -> Directive[Thick], 
   Ticks -> {None, Automatic}, PlotRange -> {{0, 3.1}, {-1, 0.07}}, 
   AxesLabel -> {l, v[x]}, LabelStyle -> Directive[FontSize -> 12], 
   GridLines -> Automatic, GridLinesStyle -> Directive[Gray, Dashed], 
   ImageSize -> {500}, PlotRangePadding -> 0, AxesOrigin -> {0, 0}], 
  Plot[-x + 1, {x, 1, 2}, PlotStyle -> Directive[Thick]], 
  Plot[-x + 2, {x, 2, 3}, 
   PlotStyle -> Directive[Thick]],(*Here are the ticks labels*)
  Graphics[{Text[Style[#, 12], {#, 0.03}] & /@ {0.5, 1., 1.5, 2., 2.5,
       3.}}]

enter image description here

?

Have fun!

$\endgroup$
  • $\begingroup$ thank you very much! your hint has been very useful for solving this problem! Probably you do not need so many Text[Style[#, 12], {#, 0.03}] but just one evaluated through the list of labels... $\endgroup$ – L. B. Aug 13 '15 at 21:22
  • $\begingroup$ @ L. B. Yes, this is a miswriting. I edited a corrected version. $\endgroup$ – Alexei Boulbitch Aug 14 '15 at 7:13
7
$\begingroup$

You can simply use Frame and FrameTicks, add:

Frame -> {{True, False}, {False, True}},
FrameTicks -> All]

At the end. So that:

Show[{Plot[-x, {x, 0, 1}, PlotStyle -> Directive[Thick]], 
Plot[-x + 1, {x, 1, 2}, PlotStyle -> Directive[Thick]], 
Plot[-x + 2, {x, 2, 3}, PlotStyle -> Directive[Thick]]}, 
PlotRange -> All, AxesLabel -> {l, v[x]}, 
LabelStyle -> Directive[FontSize -> 12], GridLines -> Automatic, 
GridLinesStyle -> Directive[Gray, Dashed], ImageSize -> {500}, 
PlotRangePadding -> 0,
Frame -> {{True, False}, {False, True}},
FrameTicks -> All]

enter image description here

EDIT: To keep the label, change AxesLabel -> {l, v[x]} to FrameLabel -> {{v[x], None}, {None, l}}

$\endgroup$
  • $\begingroup$ Using Frame is a very clever idea, but it is not general and in the long term you will need to find another method again. $\endgroup$ – L. B. Aug 13 '15 at 21:23
  • $\begingroup$ Actually, this method is general and works in all situations, whereas the other two solutions are very situation-dependent. I use frames instead of axes in all situations. $\endgroup$ – Jason B. Aug 14 '15 at 7:23
2
$\begingroup$

Thanks to the reply of @Alexei Boulbitch, I was able to come up with a general answer of the question and I am writing it down part by part in order to let people with my same problem to get a general answer with some explanation..

First of all, we want to get rid of the actual x-axis labels. In the plot options we want to set Directive[FontOpacity -> 0, FontSize -> 0] in the x component:

TicksStyle -> {Directive[FontOpacity -> 0, FontSize -> 0],
               Automatic}

In my case the plot styles are:

myplotStyle = {PlotStyle -> Directive[Thick],
               PlotRange -> {{0, 3}, {-1, 0}},
               AxesLabel -> {l, v[x]},
               LabelStyle -> Directive[FontSize -> 12],
               GridLines -> Automatic,
               GridLinesStyle -> Directive[Gray, Dashed],
               ImageSize -> {500},
               PlotRangePadding -> 0,
               PlotRangeClipping -> False,
               AxesOrigin -> {0, 0},
               TicksStyle -> {
                       Directive[FontOpacity -> 0, FontSize -> 0],
                       Automatic
                              }
               };

The options

PlotRangePadding -> 0,
PlotRangeClipping -> False,

are necessary respectively to avoid properly fill the plot and to avoid clipping of the text that we are going to insert. Then, we prepare the curves on the plot:

myplot = Show[{Plot[-x, {x, 0, 1}, Evaluate[myplotStyle]],
               Plot[-x + 1, {x, 1, 2}, Evaluate[myplotStyle]],
               Plot[-x + 2, {x, 2, 3}, Evaluate[myplotStyle]]}];

and ticks options are extracted from the plot:

 ticks = Ticks /. AbsoluteOptions[myplot];

Now, I use the following substitution for the ticks to move to the other side of the x-axis:

 {xticks, xlabels} = Replace[ticks[[1]], {a_, b_, c_, y__} :> {a, b, -c, y}, 1] // Reap;

I tried with the last line to extract labels, but I was not able to do that. Instead, I still can recontruct the text in the labels by extracting it from Ticks:

xlabels = DeleteCases[DeleteCases[ticks[[1, All, 2]], ""], 0.];

In the last expression, Null value and 0. were eliminated from the label list. Now the final plot is the combination of the previous curves and the text that is positioned at 0.03 over the x line. Ticks are the xticks for x-axis and automatic ticks for the y-axis:

 Show[{myplot,
       Graphics[{Text[Style[#, 12], {#, 0.03}]}] & /@ xlabels},
       Ticks -> {xticks, Automatic}
     ]

Result:

Resulting Plot

For the other plot, same procedure is used but last step is just a little bit different:

 Show[{myplot,
       Graphics[{Text[Style[#, 12], {# - 0.05, -0.2}]}] & /@ xlabels},
       Ticks -> {xticks, Automatic}
     ]

or use Background -> White to have a white frame behind the number. Result:

Second Result

$\endgroup$

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.