4

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

MarcoB
  • 67,153
  • 18
  • 91
  • 189
hana
  • 2,388
  • 5
  • 19
  • 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. – MarcoB Jun 23 '22 at 17:17
  • @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. – hana Jun 23 '22 at 17:21
  • After all of this, I decided to export the image and numbers, titles and add it in visio which is much easier to do! – hana Jun 24 '22 at 06:42

2 Answers2

6
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

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • 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. – hana Jun 23 '22 at 15:53
  • 3
    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. – Bob Hanlon Jun 23 '22 at 15:58
  • I just updated it. – hana Jun 23 '22 at 16:22
  • 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. – hana Jun 24 '22 at 05:35
4

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}]
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • Somehow this doesn't work on my PC, 12.1.1 for Microsoft Windows (64-bit). – hana Jun 24 '22 at 04:27
  • 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. – hana Jun 24 '22 at 05:59
  • There are no brackets around {c_, x_} and {-c, x} in Mr.Wizard original code, so omitting them might get it working in 12. – Chris Degnen Jun 24 '22 at 07:03