3
$\begingroup$

Is it possible to plot y = x^2 and x = y^2 on the some graph? For some reason, I can't get it to plot the x = y^2 properly. Here is what I get:

Show[{ Plot[x == y^2, {y, -1, 1}], Plot[y = x^2, {x, -1, 1}]}]

enter image description here

Thank you for your time.

$\endgroup$

4 Answers 4

7
$\begingroup$

Yes, you just have to change how you think about plotting, a little. Specifically, you are looking for ParametricPlot.

ParametricPlot[{{t, t^2}, {t^2, t}}, {t, -1, 1}]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ This is good to see. Thank you! $\endgroup$ Commented Nov 22, 2012 at 5:26
  • $\begingroup$ Just wondering, is it possible to fill in the enclosed region? $\endgroup$ Commented Nov 22, 2012 at 5:28
  • 2
    $\begingroup$ @spryno724, that requires an entirely different approach... RegionPlot[x^2 < y && y^2 < x, {x, 0, 1}, {y, 0, 1}] $\endgroup$ Commented Nov 22, 2012 at 5:29
  • $\begingroup$ Ah... ok. Just thought I would check. $\endgroup$ Commented Nov 22, 2012 at 5:30
5
$\begingroup$

rcollyer's method is the best way of going about it. Here's an alternative:

g = Plot[x^2, {x, -1, 1}];
Show[g, g /. v_ /; VectorQ[v, NumericQ] && Length[v] == 2 :> Reverse[v], 
     PlotRange -> All]

parabolas

Here's a more conventional route, tho:

ContourPlot[{y == x^2, x == y^2}, {x, -1, 1}, {y, -1, 1}, Axes -> True, Frame -> False]

parabolas, again

To see the filled version being asked in the comments:

Show[ContourPlot[{y == x^2, x == y^2}, {x, -1, 1}, {y, -1, 1}], 
     RegionPlot[x^2 < y && y^2 < x, {x, 0, 1}, {y, 0, 1}],
     Axes -> True, Frame -> False]

parabolas with filling

$\endgroup$
5
$\begingroup$

For completeness we can mention finding all branches of inverse function. It won't always work, but it is conceptually instructive for simple cases.

f[x_] = x^2; g[x_] = InverseFunction[f][x]

-Sqrt[x]

Plot[{f[x], g[x], -g[x]}, {x, -1, 1}, AspectRatio -> 1, Filling -> {1 -> {3}}]

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ ParametricPlot needs Filling as an option! :) $\endgroup$
    – rcollyer
    Commented Nov 22, 2012 at 6:02
  • $\begingroup$ @rcollyer: orienting the filling would be a bit problematic, tho... $\endgroup$ Commented Nov 22, 2012 at 6:03
  • $\begingroup$ @rcollyer MeshShading as a remedy ;) $\endgroup$ Commented Nov 22, 2012 at 6:04
  • $\begingroup$ @J.M. maybe. But, filling between lines would be less difficult. $\endgroup$
    – rcollyer
    Commented Nov 22, 2012 at 6:10
  • $\begingroup$ @VitaliyKaurov why yes it is, but I'll have to play with that tomorrow. $\endgroup$
    – rcollyer
    Commented Nov 22, 2012 at 6:10
2
$\begingroup$

Using ReflectionTransform:

Clear["Global`*"];
ticks = {Range[-3/2, 3/2, 1/2], Range[-3/2, 3/2, 1/2]};
legend = LineLegend[{ColorData[97][1], 
    Red}, {"y = \!\(\*SuperscriptBox[\(x\), \(2\)]\)", 
    "x = \!\(\*SuperscriptBox[\(y\), \(2\)]\)"}];
g1 = Plot[x^2, {x, -2, 2}
   , PlotRange -> {{-3/2, 3/2}, {-3/2, 3/2}}
   , AspectRatio -> Automatic
   , Ticks -> ticks
   ];
g2 = RegionPlot[x^2 < y && y^2 < x
  , {x, 0, 1}, {y, 0, 1}
  , PlotStyle -> Yellow
  ]; (* Borrowed from J.M. 's answer *)

Legended[
 Show[g1
  , g1 /. L_Line :> {Red, GeometricTransformation[L
      , ReflectionTransform[{-1, 1}]]}
  , g2
  , Epilog -> {Dashed, InfiniteLine[{{-1, -1}, {1, 1}}]}
  ]
 , legend
 ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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