BlackShadow
BlackShadow

Reputation: 1015

plotting on the y-axis in Mathematica

I have another question about Wolfram Mathematica. Is there someone that knows how I can plot a graphic on the y axis?

I hope that the figure helps.

enter image description here

Upvotes: 9

Views: 13657

Answers (5)

Mr.Wizard
Mr.Wizard

Reputation: 24336

You can flip the axes after plotting with Reverse:

g = Plot[Sin[x], {x, 0, 9}];

Show[g /. x_Line :> Reverse[x, 3], PlotRange -> Automatic]

enter image description here

With a minor change this works for plots using Filling as well:

g1 = Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]}];
g2 = Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]}, Filling -> {1 -> {2}}];

Show[# /. x_Line | x_GraphicsComplex :> x~Reverse~3,
     PlotRange -> Automatic] & /@ {g1, g2}

enter image description here

(It may be more robust to replace the RHS of :> with MapAt[#~Reverse~2 &, x, 1])


As a Function

Here is the form I recommend one use. It includes flipping of the original PlotRange rather than forcing PlotRange -> All:

axisFlip = # /. {
   x_Line | x_GraphicsComplex :> 
      MapAt[#~Reverse~2 &, x, 1], 
   x : (PlotRange -> _) :>
      x~Reverse~2 } &;

To be used like: axisFlip @ g1 or axisFlip @ {g1, g2}


A different effect can be had with Rotate:

Show[g /. x_Line :> Rotate[x, Pi/2, {0,0}], PlotRange -> Automatic]

enter image description here

Upvotes: 9

Dr. belisarius
Dr. belisarius

Reputation: 61016

Just for fun:

ContourPlot is another alternative. Using Thies function:

ContourPlot[-y*Exp[-y^2/2] - x == 0, 
            {x, -2, 2}, {y, 0, 4}, 
            Axes -> True, Frame -> None]

enter image description here

RegionPlot is another

RegionPlot[-y*Exp[-y^2/2] > x,
           {x, -2.1, 2.1}, {y, -.1, 4.1}, 
           Axes -> True, Frame -> None, PlotStyle -> White, 
           PlotRange -> {{-2, 2}, {0, 4}}]

enter image description here

And finally, a REALLY convoluted way using ListCurvePathPlot and Solve:

Off[Solve::ifun, FindMaxValue::fmgz];

ListCurvePathPlot[
 Join @@
  Table[
        {x, y} /. Solve[-y*Exp[-y^2/2] == x, y],
   {x, FindMaxValue[-y*Exp[-y^2/2], y], 0, .01}],
 PlotRange -> {{-2, 2}, {0, 4}}]

On[Solve::ifun, FindMaxValue::fmgz];

enter image description here

Off Topic

Answer to Sjoerd's None of the answers given thus far can work with Plot's Filling option.

Reply: Not necessary

f={.5 Sin[2 y],Sin[y]};
RegionPlot[Min@f<=x<=Max@f,{x,-1,1},{y,-0.1,2.1 Pi},
  Axes->True,Frame->None,
  PlotRange->{{-2,2},{0,2 Pi}},
  PlotPoints->500] 

enter image description here

Upvotes: 7

Sjoerd C. de Vries
Sjoerd C. de Vries

Reputation: 16232

ParametricPlot[{5 Sin[y], y}, {y, -2 \[Pi], 2 \[Pi]}, 
                Frame -> True,  AxesLabel -> {"x", "y"}]

enter image description here


EDIT

None of the answers given thus far can work with Plot's Filling option. Plot's output contains a GraphicsComplex in that case (which, incidentally, breaks Mr.Wizard's replacements). To get the filling capability (it doesn't work for a standard plot without filling) you could use the following:

Plot[Sin[x], {x, 0, 2 \[Pi]}, Filling -> Axis] /.  List[x_, y_] -> List[y, x]

enter image description here

Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]}, Filling -> {1 -> {2}}] 
   /. List[x_, y_] -> List[y, x]

enter image description here

Upvotes: 9

Thies Heidecke
Thies Heidecke

Reputation: 2537

One possibility is to use a ParametricPlot like this:

ParametricPlot[
  {-y*Exp[-y^2], y}, {y, -0.3, 4},
  PlotRange -> {{-2, 2}, All},
  AxesLabel -> {"x", "y"},
  AspectRatio -> 1/4
]

Example for plot with flipped axes

Upvotes: 8

Verbeia
Verbeia

Reputation: 4420

Depending on how you wanted the axis labels to show, you could just wrap the code for the original Plot in the Rotate function.

Upvotes: 5

Related Questions