4
$\begingroup$

How can I move these numbers to the right side of the y axis?
(preferably not using frame)

Plot[1 + x^2, {x, -5, 0}]

enter image description here

Here is my orginal problem. I tried my best to create a simple minimal working example for this.
It would be nice if there is a way to move the numbers with minimal changes as I did use a lot of style settings for axes and numbers.

polygon = {{0, 0}, {0, 1.5}, {-2, 1}, {-2, 0}, {0, 0}};
myfunc[myfunctions_, polygon_, {xmin_, xmax_, ymin_, ymax_}] := 
 Module[{plot1, plot2, points},
  
  plot1 = 
   ListLinePlot[polygon, 
     GridLines -> {Range[xmin, xmax, 1/2], Range[ymin, ymax, 1/2]}, 
     PlotRange -> {{xmin, xmax}, {ymin, ymax}}, 
     Filling -> {1 -> Axis}, 
     FillingStyle -> Directive[Opacity[0.2`], Pink],
     AxesLabel -> {MaTeX["X", Magnification -> 3], 
       MaTeX["Y", Magnification -> 3]}, 
     Ticks -> {({#1, 
           MaTeX[#1, "DisplayStyle" -> False, 
            Magnification -> 3]} &) /@ 
        Range[xmin, 
         xmax], ({#1, 
           MaTeX[#1, "DisplayStyle" -> False, 
            Magnification -> 3]} &) /@ Range[ymin, ymax, 1]}, 
     TicksStyle -> Directive[Black, Bold, 20], 
     GridLines -> {Range[xmin, xmax, 1], Range[xmin, xmax, 1]}, 
     ImageSize -> 800] /. _Line -> Sequence[];
  plot2 = 
   Plot[myfunctions, {x, xmin, xmax}, 
    PlotRange -> {{xmin, xmax}, {ymin, ymax}}, 
    PlotStyle -> Directive[Thickness[0.01], CapForm["Round"]], 
    AxesLabel -> {MaTeX["X", Magnification -> 2], 
      MaTeX["Y", Magnification -> 2]}, 
    TicksStyle -> Directive[Black, Bold, 20], ImageSize -> 800];
  Show[plot1, plot2, AspectRatio -> 1]
  ]

Now if you excute this:

myfunc[{-x, -((1 + x^2)/(x - 3)), 2 - x}, polygon, {-3, 0, 0, 5}]

enter image description here

$\endgroup$
3
  • $\begingroup$ You need to use a frame rather than axes. This should be as simple as changing all axis references to "frame". For instance, FrameTicks instead of Ticks, FrameLabel instead of AxesLabel, etc. $\endgroup$
    – MarcoB
    Jun 23 at 17:17
  • $\begingroup$ @MarcoB I just tried but the numbers are much smallers and look bad. I can increase the size manually but I don't know how to keep it exactly the same as before. I want to keep everything same, just moving the numbers to the right. $\endgroup$
    – hana
    Jun 23 at 17:21
  • $\begingroup$ After all of this, I decided to export the image and numbers, titles and add it in visio which is much easier to do! $\endgroup$
    – hana
    Jun 24 at 6:42

2 Answers 2

6
$\begingroup$
Clear["Global`*"]

Assuming that your objection to Frame is just to avoid frames to the left and above:

Plot[1 + x^2, {x, -5, 0}, 
 Frame -> {{None, Automatic}, {Automatic, None}},
 FrameTicks -> All]

enter image description here

EDIT: The optional argument type with any value other then "Axes" will switch to a Frame

Needs["MaTeX`"];

EDIT 2: Placing labels on the axes instead of frames

myfunc[myfunctions_, polygon_, {xmin_, xmax_, ymin_, ymax_}, 
 type_ : "Axes"] := Module[
  {plot1, plot2, points, xticks, yticks},
  {xticks, yticks} = {
    ({#1, MaTeX[#1, "DisplayStyle" -> False,
         Magnification -> 3]} &) /@ Range[xmin, xmax],
    ({#1, MaTeX[#1, "DisplayStyle" -> False,
         Magnification -> 3]} &) /@ Range[ymin, ymax]};
  plot1 = ListLinePlot[polygon,
     GridLines ->
      {Range[xmin, xmax, 1/2], Range[ymin, ymax, 1/2]},
     PlotRange -> {{xmin, xmax}, {ymin, ymax}},
     PlotRangePadding -> Scaled[.01],
     Filling -> {1 -> Axis},
     FillingStyle -> Directive[Opacity[0.2`], Pink],
     AxesLabel -> {
       MaTeX["X", Magnification -> 3],
       MaTeX["Y", Magnification -> 3]},
     FrameLabel -> None,
     Ticks -> {xticks, yticks},
     FrameTicks -> {{None, yticks}, {xticks, None}},
     TicksStyle -> Directive[Black, Bold, 20],
     GridLines ->
      {Range[xmin, xmax, 1], Range[ymin, ymax, 1]},
     ImageSize -> 500,
     If[type === "Axes",
      {Axes -> True, Frame -> False},
      {Axes -> False, Frame -> {{False, True}, {True, False}}}]] /. 
    _Line -> Sequence[];
  plot2 = 
   Plot[myfunctions, {x, xmin, xmax}, 
    PlotStyle -> Directive[Thickness[0.01], CapForm["Round"]]];
  axesLabels = Graphics[{
     Text[MaTeX["X", Magnification -> 3], Scaled[{1.1, 0}]],
     Text[MaTeX["Y", Magnification -> 3], Scaled[{1, 1.07}]]}];
  Show[plot1, plot2,
   If[type === "Axes", {}, axesLabels], AspectRatio -> 1,
   ImagePadding -> {{25, 70}, {50, 50}},
   PlotRangeClipping -> False]]

Example,

myfunc[{-x, -((1 + x^2)/(x - 3)), 2 - x}, polygon, 
 {-3, 0, 0, 5}, "Frame"]

enter image description here

$\endgroup$
4
  • $\begingroup$ I did set a lot of styles for axies and these numbers so I don't want to use frame so I'll have to do it again for frame. $\endgroup$
    – hana
    Jun 23 at 15:53
  • 3
    $\begingroup$ Edit your question to reflect the actual problem you are trying to resolve. In is not clear why the solution involves more than additionally changing one word. $\endgroup$
    – Bob Hanlon
    Jun 23 at 15:58
  • $\begingroup$ I just updated it. $\endgroup$
    – hana
    Jun 23 at 16:22
  • $\begingroup$ How can I keep "X" and "Y" on the corners as the orginal? This is very nice but I just want to keep it consistent with many images I did previously. $\endgroup$
    – hana
    Jun 24 at 5:35
4
$\begingroup$

Applying Mr.Wizard's method from here

p = Plot[1 + x^2, {x, -5, 0}];
ticks = Ticks /. AbsoluteOptions[p];
{yticks, labels} = Replace[ticks[[2]], {a_, b_, {c_, x_}} :>
     {a, Sow@Text[b, {0.2, a}];, {-c, x}}, 1] // Reap;
Show[p, Graphics[labels /. "0" -> ""], Ticks -> {ticks[[1]], yticks},
 PlotRangePadding -> {{Automatic, 0.3}, Automatic}]
$\endgroup$
3
  • $\begingroup$ Somehow this doesn't work on my PC, 12.1.1 for Microsoft Windows (64-bit). $\endgroup$
    – hana
    Jun 24 at 4:27
  • $\begingroup$ It works on my another PC with 13.0.1 but how would you apply it to my orginal code? I tried but couldn't make it work. $\endgroup$
    – hana
    Jun 24 at 5:59
  • $\begingroup$ There are no brackets around {c_, x_} and {-c, x} in Mr.Wizard original code, so omitting them might get it working in 12. $\endgroup$ Jun 24 at 7: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.