1
$\begingroup$

Say one had a function that was itself asymmetric about the abscissa, but one desired to plot it on a symmetric ordinate. How can this be achieved with Plot automatically (without separately computing the limits and providing them to Plot)?

Example The plot of 1/2 serves as a sufficient example.

Table[
  Plot[ 
    1/2, 
    { x, 0, 1 }, 
    Frame -> { False, True }[[i]], 
    Axes -> { True, False }[[i]] 
  ],
  { i, 1, 2 } 
 ]

asymmetric plots

But we might want the PlotRange to go from $+1$ to $-1$ instead, like:

symmetric plot

I did this with the option PlotRange, but short of finding the maximum of the absolute value of the function and setting PlotRange, I don't know how to automate this.

Specifically, is it possible to use Plot in a native manner to do this?

Solution (based on the answer by Szabolcs) Here's a little function that implements the answer.

plotSym[ plot_ ] := 
  plot //
    { 
      #, 
      First[PlotRange /. AbsoluteOptions[#, PlotRange]], 
      Max@Abs@Last[PlotRange /. AbsoluteOptions[#, PlotRange]] 
    } & //
      Show[ #[[1]], PlotRange -> {#[[2]], (1.2*{-#[[3]], #[[3]]})} ] &  

Note: I added the factor of 1.2 as a way to give the plot a little more room. It can be anything nonzero (including the likely more-desirable 1).

$\endgroup$
4
$\begingroup$

You can try extracting the PlotRange using AbsoluteOptions. You need to use AbsoluteOptions, not simply Options, to make sure All gets translated to a numerical value.

g = Plot[Sin[x]^2, {x, 0, Pi}, PlotRange -> All]

max = Max@Abs@Last[PlotRange /. AbsoluteOptions[g, PlotRange]]

Show[g, PlotRange -> {All, 1.1 {-max, max}}]
$\endgroup$
  • $\begingroup$ Nice, thank you Szabolcs. I wrote a little function based on your answer that I'll put in my question. $\endgroup$ – Rico Picone Jun 30 '14 at 20:11

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.