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.

Question

I add the origin to my graph with

after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}

(code found here). This works fine is the origin is on my axis. But, there are problems if 0 is not in the values taken by y (see the pictures below).

Is there a way to fix this issue?

By this, I mean an option that would display the origin only when needed.

Pictures

first picture

enter image description here

second picture

I cannot put it here, but it prints two pages!!! enter image description here

Code

first code

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major, 
clip = true, 
clip mode=individual, 
axis x line = middle, 
axis y line = middle, 
xlabel={$x$}, 
xlabel style={at=(current axis.right of origin), anchor=west}, 
ylabel={$y$}, 
ylabel style={at=(current axis.above origin), anchor=south}, 
domain = 2:5, 
xmin = 2, 
xmax = 5, 
enlarge y limits={rel=0.13}, 
enlarge x limits={rel=0.07}, 
restrict y to domain=1:2,
ymin = 1, 
ymax = 2, 
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}

second code

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major, 
clip = true, 
clip mode=individual, 
axis x line = middle, 
axis y line = middle, 
xlabel={$x$}, 
xlabel style={at=(current axis.right of origin), anchor=west}, 
ylabel={$y$}, 
ylabel style={at=(current axis.above origin), anchor=south}, 
domain = 2:5, 
xmin = 2, 
xmax = 5, 
enlarge y limits={rel=0.13}, 
enlarge x limits={rel=0.07}, 
%restrict y to domain=1:2,
%ymin = 1, 
%ymax = 2, 
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
    
You most likely want to use rel axis cs instead of axis cs to set the origin. –  Henri Menke Apr 8 at 17:26
    
You can use the axis description coordinate system instead: after end axis/.code={ \node[left=2pt] at (axis description cs:0,0) {0}; \node[below=2pt] at (axis description cs:0,0) {0}; } –  Gonzalo Medina Apr 8 at 17:29
    
Hi! Both solution work and don't work, in a sense. The origin is displayed in a good way, but since 0 is out the range, the origin in this case should not be displayed. –  Colas Apr 8 at 17:33
    
Some remarks: I don't know why (and what is the loss in this trick), but the behavior is better with before than with after. More precisely, before end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0} ;} is better. –  Colas Apr 8 at 18:06
1  
@Colas: Probably better due to the fact that with before end axis you are still within the axis environment so that pgf can determine if that text is within the bounds of the plot, but after end axis pgf is unable to make that determination. –  Peter Grill Apr 8 at 18:12

1 Answer 1

You can use axis direction cs as in

\path (axis direction cs:0,0) node [anchor=north west,yshift=-0.65cm,xshift=-0.7cm] {0} node [anchor=south east,xshift=-0.5cm,yshift=-0.7cm] {0};}]

Code:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 2:5,
xmin = 2,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
restrict y to domain=1:2,
ymin = 1,
ymax = 2,
after end axis/.code={\path (axis direction cs:0,0) node [anchor=north west,yshift=-0.65cm,xshift=-0.7cm] {0} node [anchor=south east,xshift=-0.5cm,yshift=-0.7cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

there is also axis description cs which you may like to exploit for this purpose.

after end axis/.code={\path (axis description cs:0,0) node [anchor=north west,yshift=-0.075cm,xshift=-0.2cm] {0} node [anchor=south east,xshift=-0.075cm,yshift=-0.2cm] {0};}]

However for this particular graph, (0,0) position is completely wrong as seen in the picture.

share|improve this answer

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.