Why does pgfplots plot functions only until x = 5 and y = 5, but not any further?

enter image description here





\addplot [thick] plot  (\x,  {1/(1 + exp(-0.6*(\x - 12)))});
\addplot plot (\x, {\x});

  • 6
    Because that's the default domain. You can define over what range to plot the function by setting domain=0:20, for example. xmin and xmax only set the axis range (what you can see) but not the calculation range (what is computed). By the way, instead of saying \addplot plot (\x, {\x});, you can also say \addplot {x};. – Jake Apr 9 '13 at 15:03

There are a lot of "domain" options in pgfplots. The one which you are asking about is simply domain, which specifies what values of x (you don't need the backslash if you're using pgfplots) are used in plotting; by default, we have domain = -5:5, which is what the author apparently thinks is reasonable for typical graphs. There is a corresponding y domain for two-variable functions.

This domain is quite different from the limits established by xmin and xmax. While domain is set per-plot, these keys are set per-axis and just confine the actual drawing to these limits. There are corresponding ymin and ymax. These will be computed automatically by pgfplots if they are not given, but it is necessary for a really polished picture to set them yourself. Note that y domain does not have anything to do with ymin and ymax in a plot of one-variable functions, because it determines the inputs of the non-existent variable y. Instead, ymin and ymax, if they were determined automatically, would be calculated from the values output by your plotted functions across the domain.

There are more! My favorites are restrict x to domain and restrict y to domain, which are filters with the same input syntax as domain. These don't determine what numbers are used in the variables; they determine what values are used in the plot. They are enormously helpful with parametric or uncontrollable functions; i.e. \addplot {1/x}; will, with the default domain = -5:5, produce a rather hideous asymptote at x = 0 as well as (with the default ymin and ymax) a badly distorted view of the axes. But setting restrict y to domain = -5:5 in this plot will simply eliminate the large values, removing the asymptote and also scaling the picture back to a proportional square.

Or, alternatively, a parametric plot such as \addplot ({exp(x)},{exp(-x)}); (a funny way of drawing the same thing in the first quadrant alone), which is hard to tune directly because of the logarithmic connection between values on the plot and values of the variable. For this, both the default domain and the default axis sizes are inappropriate; I usually leave domain as it is (which gives numbers that are too huge in both coordinates) and then set restrict x to domain and restrict y to domain accordingly to trim the picture nicely. This is not to say it's a good idea to be completely oblivious to domain, since those points are computed...just not used.

These filter keys are different from the min and max keys because they actually ignore the filtered-out values, rather than simply cutting them out of the picture. This is essential if these values are larger than TeX is capable of computing with.

Finally, there are samples or sample at, which latter exists mutually exclusively with domain and say how many, or even exactly at which values of x to compute values. This can be an alternative to the restrict to domain keys, if you choose the samples carefully to avoid exceptional inputs. They are also useful for tuning the plot around rapidly-varying places in the graph, which would otherwise look rather choppy. These also interact with the restrict to domain keys in the sense that with, say,

  domain = -5:5,
  samples = 11,
  restrict x to domain = -1:1,
  restrict y to domain = -1:1
] ({exp(x)},{exp(-x)});

there will be exactly 11 points evaluated, namely ({exp(-5)},{exp(5)}) through ({exp(5)},{exp(-5)}), but only those with both coordinates in the interval [-1,1] will be plotted. Unfortunately, the only point with that property is ({exp(0)},{exp(0)}) = (1,1), so your plot will be rather vacant. The un-plotted points are not even used to anchor interpolating curves! So the filter keys are not a panacea.

My pictures tend to set all of these keys, since they each affect the drawing differently.

  • 1
    Nice overview! Two things: 1. While sample at and domain are mutually exclusive, samples and domain are not (I guess you didn't actually mean that, but that's how I read it). 2. Discarding points using restrict x to domain is probably okay in most situations, but it does introduce unnecessary computations. Setting the domain (or samples at) to appropriate values can sometimes be required if you have functions that are very expensive to evaluate. – Jake Apr 9 '13 at 15:29

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.