18
$\begingroup$

I have a relatively simple operation I'd like to perform on a plot: I would like to reflect the plot across the x-axis. I do not have any tick labels, so making sure that the labels are not themselves reflected is not a concern.

Rotating the plot works as expected:

Rotate[ListPlot[{{0,0},{1,1}},Joined->True],90\[Degree]]

...but there does not appear to be a "Reflect" command or equivalent that performs a reflection operation. I've tried using GeometricTransformation and Scale, but both of those don't seem to work on plot objects. How should I go about doing this?

$\endgroup$
  • 1
    $\begingroup$ Can't you just change all y values into -y? Just do {#[[1]],-#[[2]]}& /@ pointList, with pointList the set of points to be plotted. $\endgroup$ – Sjoerd C. de Vries Jul 3 '12 at 22:21
  • $\begingroup$ I have filling in my plot, and I'm also doing a rotation (essentially I'm flipping about the y = x axis), so while what you suggest would work for the simple example I provided, it doesn't work in general for more complicated operations. $\endgroup$ – Guillochon Jul 3 '12 at 22:24
  • $\begingroup$ If you only need to flip a 2D image, then you can use ImageReflect $\endgroup$ – rm -rf Jul 4 '12 at 2:36
14
$\begingroup$

How about something like this (example shamelessly stolen from the docs)

f[n_, x_] := 
 Abs[((1/Pi)^(1/4) HermiteH[n, x])/(E^(x^2/2) Sqrt[2^n n!])]^2
lp = Plot[Evaluate@
     Append[Table[f[n, x] + n + 1/2, {n, 0, 7}], x^2/2], {x, 0, 4}, 
  Filling -> Table[n -> n - 1/2, {n, 1, 8}]]

Graphics[FullGraphics[
    lp][[1]] /. {x_Real, 
    y_Real} :> {-x, y}, 
    AspectRatio -> .42*2.380952380952381]

Mathematica graphics

(you said ticks aren't important so I ignored them). This is similar to Sjoerd's suggestion in the comments but for the whole plot.

$\endgroup$
22
$\begingroup$

Few examples from the docs on ReflectionTransform used in combination with GeometricTransformation

  gr = Plot[ E^x, {x, -3, 2}]; 
  Row[{Show[gr, Plot[x, {x, -3, 7}, PlotStyle -> Black], 
   gr /. L_Line :> {Red, GeometricTransformation[L, ReflectionTransform[{-1, 1}]]}, 
   PlotRange -> All, ImageSize -> 200], 
   Show[gr, gr /. L_Line :> {Red, GeometricTransformation[L, ReflectionTransform[{-1, 0}]]}, PlotRange -> All, ImageSize -> 200], 
  Show[gr, gr /. L_Line :> {Red, GeometricTransformation[L, ReflectionTransform[{0, -1}]]}, PlotRange -> All, ImageSize -> 200]}, Spacer[15]]

enter image description here

 cow = ExampleData[{"Geometry3D", "Cow"}, "GraphicsComplex"];
 p1 = {0, 0, -0.25161901116371155`};
 p2 = {0, 0, 0.25161901116371155`};
 Row[{Graphics3D[{EdgeForm[None], Opacity[0.5], 
 Lighter[ColorData[1, 1], 0.5], cow, Lighter[ColorData[1, 2], 0.5], 
 GeometricTransformation[cow, #]}, Lighting -> "Neutral", 
 ImageSize -> Small, Boxed -> False] & /@
{ReflectionTransform[{0, 0, 1}, p1], 
 ReflectionTransform[{0, 0, 1}, p2],
 ReflectionTransform[{1, 0, 0}, p1], 
 ReflectionTransform[{1, 1, 0}, p1]}}, Spacer[15]]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ wish I could upvote again for the cows $\endgroup$ – acl Jul 3 '12 at 23:19
  • $\begingroup$ Holy Flying Cows, Batman! $\endgroup$ – Dr. belisarius Jul 4 '12 at 4:48
  • $\begingroup$ can this be made to flip the axes? $\endgroup$ – acl Jul 4 '12 at 10:13
  • 1
    $\begingroup$ +1 There should be more answers on this site involving semi-transparent cows. $\endgroup$ – Simon Woods Jul 4 '12 at 11:21
  • $\begingroup$ Do you use this `GeometricTransform* in making Matlab's data equivalent for Mathematica? - - There is 90 degree rotation to the left required. $\endgroup$ – Léo Léopold Hertz 준영 Sep 23 '16 at 10:05
10
$\begingroup$

You can actually use Scale for this by doing something like

MapAt[Scale[#, {-1, 1}] &, Plot[Sin[x], {x, 0, 2 Pi}] , {1}]

Mathematica graphics

$\endgroup$
0
$\begingroup$

There is a very simple way to "mirror" a function in the y-axis.

Just write $f[-x]$, where you need.

Example:

f[x_] = 2 x - 3;
Plot[Which[x > 0, f[x], x < 0, f[-x]], {x, -5, 5}, PlotRange -> All]

mirror in y-axis

To mirror a function in the x-axis you can place $-f[x]$, where you need.

Example:

f[x_] = 2 x - 3;
P1 =  Plot[f[x], {x, -5, 5}, PlotRange -> All];
P2 =  Plot[-f[x], {x, -5, 5}, PlotRange -> All, PlotStyle -> Red];
Show[P1, P2]

mirror in x-axis

$\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.