[Tex/LaTex] Axis label positioning in pgfplots

pgfplotstikz-pgf

Can someone please help me explain why label positioning in the following code does not work? It is not clear what I am doing wrong. I also tried axis description cs. That does not change the label position either.

Thank you.

\documentclass{article}
\usepackage{tikz}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}



\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=4in,
        height=4in,
        xlabel={Quantity},
        % the following x label positioning does not work.
        every axis x label/.style=
            {at={(ticklabel cs: 0.5)}, anchor=north},
        ylabel={Price},
        axis x line=middle,
        axis y line=middle,
        ytick={0,10,...,90},
        xtick={0,10,...,90},
        ymin=0,
        ymax=100,
        xmin=0,
        xmax=100,
        xlabel shift = -1in,
    ]
        \addplot coordinates {(0,90) (90,0)};
        \addplot[color=red,domain=0:40]{2*x} node [pos=1,above]{MC};

        % why is this node not visible.
        \node at (axis description cs:0.5,-0.02) {(0,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

Best Answer

axis x line redefines the label style, move your definition to after that. Note that you seem to need specify a y-coordinate of zero as well, i.e. (ticklabel cs:0.5,0).

Read section 4.9.1 Placement of axis descriptions in the pgfplots manual for more discussion about the ticklabel coordinate system(s).

As for the invisible node, everything outside the axis limits is by default clipped away. If you set clip mode=individual, plots will be clipped, but \nodes and similar will not be clipped.

Finally, pgfplots loads tikz, so you don't actually have to load the latter explicitly.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=4in,
        height=4in,
        xlabel={Quantity},
        ylabel={Price},
        axis x line=middle,
        axis y line=middle,
        % the following x label positioning does work here.
        every axis x label/.style=
            {at={(ticklabel cs: 0.5,0)}, anchor=north},
        ytick={0,10,...,90},
        xtick={0,10,...,90},
        ymin=0,
        ymax=100,
        xmin=0,
        xmax=100,
        xlabel shift = -1in,
        clip mode=individual
    ]
        \addplot coordinates {(0,90) (90,0)};
        \addplot[color=red,domain=0:40]{2*x} node [pos=1,above]{MC};

        % why is this node not visible.
        \node at (axis description cs:0.5,-0.02) {(0,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here