2

I would like to define common values with \pgfplotsset for most plots in a document and overwrite individual settings for some plots.

In the case of restrict y to domain this fails horribly, since any later usage of that command seems to be completely ignored.

Minimal working example of the problem:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.11}
\pgfplotsset{restrict y to domain=-5:5}
\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=-10, xmax=10, ymin=-10, ymax=10]
\addplot gnuplot [samples=500, mark=none, domain=-10:10, thick, blue] {x};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[xmin=-10, xmax=10, ymin=-10, ymax=10, restrict y to domain=-10:10]
\addplot gnuplot [samples=500, mark=none, domain=-10:10, thick, blue] {x};
\end{axis}
\end{tikzpicture}

\end{document}

Both plots produce exactly the same result, even though I set a different y domain restriction in the second one.

  • 1
    Welcome to TeX.SX. I don't think that that is possible. In fact both restrict y to domains are evaluated, and you can even add more. You can test that by changing values given at the second axis to -10:3 and also add another one, e.g. restrict y to domain=-3:10. Then the plot will only be drawn in the interval -3:3 ... A workaround would be defining a style containing the restrict y to domain and apply it only to the plots where you need the "global" setting. – Stefan Pinnow Oct 9 '16 at 10:59
  • I see. Is this the only instance of a definition that cannot be undone later in the document or are there more to be wary of? Might there even be a list hidden somewhere in the package manual? – phil Oct 9 '16 at 11:10
  • I just found that behavior by trying. No hint is given in the manual at the description of that key that they "accumulate". So I am not aware of other keys behaving like that nor do I think that there is a list in a manual for that. – Stefan Pinnow Oct 9 '16 at 11:20
  • Good point. Most of the coordinate filters "append to" the existing filters, allowing them to be combined (for example combine each nth point with restrict y to domain). The manual does not define that "append to the filter" means that whatever was active previously stays active. The manual uses "append" whenever such combinations are active and claims that it "sets" values if it overwrites items. – Christian Feuersänger Oct 9 '16 at 18:39
  • The solutions are as outlined by @Stefan : either you define a style which sets the global restriction and use it where appropriate or you have to reset it as outlined by Percusse in his answer. A best-practice is probably to use a style. That will also simplify copy-and-paste to other documents as it documents the dependencies better than a global config. – Christian Feuersänger Oct 9 '16 at 18:42
3

You can add y filter/.code={} key to the axis to reset everything in that filter accumulated so far before setting a new restrict y to domain.

It is not as elegant as reverting only the last bit but you might be better off without the global setting anyways.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\pgfplotsset{restrict y to domain=-5:5,domain=-20:20}
\begin{document}
\begin{tikzpicture}
\begin{axis}[y filter/.code={},restrict y to domain=-10:10]
\addplot {x};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.