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.

I'm trying to customize a bit the axis lines extension of pgfplots. Here is my code and what I get now.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{%
  compat = 1.7,
  every non boxed x axis/.style = {%
    enlarge x limits = true,
    x axis line style = {-stealth}
  },
  every non boxed y axis/.style = {%
    enlarge y limits = true,
    y axis line style = {-stealth}
  },
  axis x line = middle,
  axis y line = middle,
  every axis x label/.style = {%
    at = {(xticklabel cs:1)},
    anchor = north},
  every axis y label/.style = {%
    at = {(yticklabel cs:1)},
    anchor=east}
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xlabel = $x$, 
               ylabel = $\sin x$]
    \addplot+[domain=-10:.5,samples=40] {sin(deg(x))+2};
  \end{axis}
\end{tikzpicture}
\end{document}

I'd like to do four things automatically and I'm struggling with the doc...

  1. Both axis must intersect at (0,0) whatever the function ploted is.
  2. The xlabel must be below the xaxis at its end and the ylabel at left of the yaxis at its end (similar to the ticklabels).
  3. I want the ticklabel 0 to appear below left (0,0) once (say only as a xticklabel).
  4. Whatever the function ploted is, I'd like the axis to intersect at (0,0) and go at least a bit in the four directions (up, down, left and right, like a +). In the example above, I only get a _|_).
share|improve this question

1 Answer 1

up vote 5 down vote accepted
  1. Add before end axis/.code={\addplot [draw=none, forget plot] coordinates {(0,0)};} to your options. This will add an invisible plot with one coordinate at the origin that makes sure that the origin is always included.
  2. Use xlabel style={anchor=north}, ylabel style={anchor=east}. When using axis lines=middle, the labels are automatically positioned at the tips of the axes, only with a different alignment to the one you're requesting.
  3. You could add \node at (axis cs:0,0) [anchor=north east] {0}; to the before end axis/.code options. This will give you a slightly different alignment compared to the x tick labels, which may or may not be desirable.
  4. Add enlargelimits=true. In conjunction with point 1, this will make sure the axis always extends at least a little bit past the origin:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{%
  compat = 1.7,
  axis lines=middle,
  enlargelimits=true,
  before end axis/.code={
    \addplot [draw=none, forget plot] coordinates {(0,0)};
    \node at (axis cs:0,0) [anchor=north east] {0};
  },
  xlabel style={
    anchor=north
  },
  ylabel style={
    anchor=east
  }
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xlabel = $x$, 
               ylabel = $\sin x$]
    \addplot+[domain=-10:.5,samples=40] {sin(deg(x))+2};
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
1  
Many thanks! Works like a charm. Point 1 is very clever :). –  cjorssen Jan 10 '13 at 22:50

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.