6

According to percusses answer in Argument to \input or \include statement? both height and width in the axis environment in PGFplots may be set externally (outside the axis environment) with a nested \pgfplotsset statement:

\pgfplotsset{execute at begin axis={\pgfplotsset{width=5cm}}} 

However, if I try to apply the same concept to xmin, xmax, ymin and ymax according to the answer suggested by Piotr, pgfplotsset doesn't seem to override the settings given within the axis environment.

Consider the following MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
{ % begin outer pgfplotsset scope
\pgfplotsset{ymax=5, ymin=1, execute at begin axis={\pgfplotsset{width=5cm}}}
\begin{tikzpicture}
\begin{axis}[width=10cm, ymin=0]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
} % end outer pgfplotsset scope
\end{document}

which produces the following graph, where ymin clearly is 0, though I just set it ymin=1 in pgfplotsset.

enter image description here


What I want to achieve

Set xmin, xmax, ymin and ymax from outside the axis environment (in a similar way as I do with width applying execute at begin axis in pgsplotsset), even though they also will be specified in \begin{axis}[]. The external call to xmin etc. should override the values set in \begin{axis}[].


4
  • execute at begin axis is probably executed after the option you provide. This does not happen if you use the every axis (which is applied before any local options) style: every axis/.append style={width=5cm}. The ymin=0 overwrites the ymin=1 setting from before. May 16, 2013 at 9:57
  • So I'll still have my overriding problem... :p
    – Holene
    May 16, 2013 at 11:12
  • Could you re-phrase your question? What is your problem? What do you want to achieve? — The execute at begin axis code is executed after the [ ] axis options. Of course ymin=0 overwrites a previous set ymin=1. May 16, 2013 at 11:42
  • @Qrrbrbirlbel Ah, damn, it's quite obvious I haven't clarified what I really want to achieve. I'm sorry. Reformulated question coming up!
    – Holene
    May 16, 2013 at 11:48

3 Answers 3

3

Well, depending on what they option actually does, we can simply deactivate it.

The overwrite option key takes its argument in the form of <key> with <value> (the spaces are needed), sets the key to that value and than disables the key altogether by assigning an empty /.code.

Code

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{overwrite option/.style args={#1 with #2}{#1=#2,#1/.code=}}
\begin{document}
{ % begin outer pgfplotsset scope
\pgfplotsset{ymax=5, overwrite option=ymin with 1, execute at begin axis={\pgfplotsset{width=5cm}}}% the ymin key doesn't work anymore
\begin{tikzpicture}
\begin{axis}[width=10cm, ymin=0]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
} % end outer pgfplotsset scope
% the ymin key works as usual
\end{document}
0
5

Use \pgfplotsset{xmin=...} within desired scope:

\documentclass[landscape]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
{ % begin pgfplotsset scope
\pgfplotsset{xmin=0, xmax=3, ymin=1, ymax=5}
\begin{tikzpicture}
\begin{axis}[width=10cm]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
} % end pgfplotsset scope
\begin{tikzpicture}
\begin{axis}[width=10cm]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

This will result with: result

2
  • This is the kind of behaviour I'm looking for! However: with width, pgfplotsset overrides the setting in the axis environment. Somehow, this doesn't happen with xmin, xmax (and so on). Do you know why?
    – Holene
    May 16, 2013 at 8:54
  • I'll reformulate the question.
    – Holene
    May 16, 2013 at 9:26
0

What you are looking for ist the every axis post/.append style key, which is - to my knowledge - executed after the previous axis styles, and thus overwrites them. Thus the solution is very easy:

\documentclass[border=2pt,convert={ghostscript, density={500}}]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
{ % begin outer pgfplotsset scope
\pgfplotsset{ymax=5, execute at begin axis={\pgfplotsset{width=5cm}},
every axis post/.append style={ymin=1}}
\begin{tikzpicture}
\begin{axis}[width=10cm, ymin=0]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
} % end outer pgfplotsset scope
\end{document}

plot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .