Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

(related but unequal question: Using Macro Defined Lists in TikZ/PGFplots)

I'm having several axis-environments inside the same tikzpicture. I would like to have some (but not all) share the same xmin and xmax, and some (but not all) share the same ymin and ymax. I tried to do that by defining it in my own style, in the same way that I set xlabel, ylabel, and other axis-specific styles.

Below is a minimal, not-working example. When I run it with pgfplots 2011/07/29 v1.5 (git show 1.5-1-gcc2f2d2 ), it fails with the error message:

! Package pgfplots Error: Sorry, you can't change `/pgfplots/xmax' in this context. Maybe you need to provide it as \begin{axis}[/pgfplots/xmax=...] ?.

I have two closely related questions:

  1. Why can I set xlabel, ylabel, ytick, but not ymin? Is there a fundamental reason for this, or is it just a lack of implementation?
  2. How can I achieve the effect of applying a particular set of xmin, xmax, ymin, ymax, to more than one axis?

This related question ( Using Macro Defined Lists in TikZ/PGFplots) might provide a working solution (q. 2), but does not answer my understanding-related question (q. 1).

Minimal (not) Working Example

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}[
    spam/.style={%
        /pgfplots/ylabel={Spam (kg)},%
        /pgfplots/xlabel={Eggs (no.)},
        /pgfplots/ytick={0,10,...,200}, % this works
%        /pgfplots/xmax=3, this fails!
        }]
\begin{axis}[spam]
\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\end{axis}

\begin{axis}[spam, at={(5cm, 3cm)}]
\addplot coordinates {(-1, 0) (-1, 1) (1, 0)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this question
2  
The answer of cmhughes is good and should solve it. Here is just the missing piece of (technical) background for those who are wondering why it failed: Tikz styles are considered to be display styles. They are not evaluated for the survey phase of the axis, only for the display phase. Everything which might affect the survey phase (computing axis limits, for example), needs to be communicated to pgfplots - that is, by means of a style with the /pgfplots/ key prefix. Adding /pgfplots/spam/.style=... will solve the problem as good as \pgfplotsset{spam/.style...}. –  Christian Feuersänger Nov 26 '11 at 13:08
    
Thanks; it was the why that I was still wondering about :) –  gerrit Nov 26 '11 at 14:15
add comment

3 Answers

up vote 8 down vote accepted

Rather than defining your style in the first plot, you can define it in your pre-amble using \pgfplotsset; this allows you to put xmin, xmax, etc in your own style.

screenshot

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}

\pgfplotsset{mystyle/.style={%
        width=6cm,
        ylabel={mystyle (kg)},
        xlabel={Eggs (no.)},
        xmin=-2,xmax=3,
        xtick={-2,-1,...,3}}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[mystyle]
\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[mystyle]
\addplot coordinates {(-1, 0) (-1, 1) (1, 0)};
\end{axis}
\end{tikzpicture}

\end{document}

And if you want to inherit some of the properties from mystyle and overwrite others, then you can use, for example,

\begin{axis}[mystyle,xmin=-10,xmax=5,xtick={-5,-2,5}]
share|improve this answer
1  
pgfplots really is beautiful –  cmhughes Nov 25 '11 at 20:30
    
Side-note: you can get the same effect if you write \begin{tikzpicture}[/pgfplots/mystyle/.style=... . –  Christian Feuersänger Nov 26 '11 at 13:05
    
@ChristianFeuersänger Thanks, that's good to know! And thank you for the amazing package :) –  cmhughes Nov 26 '11 at 16:58
add comment

This partially answers your question but probably @Christian Feuersänger would answer this completely about why the limitation is in place.

I have used the following code :

\begin{tikzpicture}[every axis/.style={
xmax=3,%
xmin=-1,%
ylabel={Spam (kg)},%
xlabel={Eggs (no.)}%
ytick={0,1,...,2}
}]

\begin{axis}\addplot coordinates {(0, 0) (1, 1) (2, 2)};\end{axis}

\begin{axis}[at={(10cm, 0cm)}]
\addplot coordinates {(-1, 0) (-1, 1) (1, 0)};
\end{axis}
\end{tikzpicture}

which results with

enter image description here

I am definitely guessing here but most probably, tikzpicture has no complete external access to the inner settings of axis environment. That's why you need to supply it directly to the axis environment options.

share|improve this answer
add comment

You can use a \def to add this in after nearly minimally

\def\lims{xmax=<num>,ymax=<num>}

and then use it in an axis like

\begin{axis}[spam,\lims]

A better workaround is to use a macro

\def\spam{ylabel={Spam (kg)}, xlabel={Eggs (no.)}, ytick={0,10,...,20}, xmax={3}}

which can be used with the plot in almost the way you want

\begin{axis}[\spam]

These (I hope) answer your second question, but obviously not your first. I have no ideas on that front...

share|improve this answer
    
It's also messy in a way you obviously don't want... –  qubyte Nov 25 '11 at 15:34
    
actually, the idea is quite good. In fact, \pgfplotsset{lims/.style={xmax=<num>,ymax=<num>}} does the same: it defines a macro which sets the specified keys as soon as it is used. The only difference is that \pgfkeys (the backend for \pgfplotsset) only integrates the style definition better into the key setting stuff. see also tex.stackexchange.com/questions/2699/… –  Christian Feuersänger Nov 26 '11 at 22:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.