8

Is there any way in pgfplots describe the positive part of the x-axis labels above the axis and the negative part of the x-axis label below the axis? The point is that if the graph of the function is partly above and partly below the axis, thus I am not able to do that to me at least one part of labels on the axis not overwrite graph of function.

\documentclass{minimal}
\usepackage{pgfplots}
\def\AddLabel(#1,#2)#3{\node \[align = center\] at (axis cs: #1,#2) {#3};}%

\begin{document}
\begin{tikzpicture}
\def\function{rad(atan((1.73205*\x)/(1-(\x)^2)))}
\def\functionlabel{\arctan\frac{\sqrt{3}\cdot x}{1-x^2}}

\begin{axis}\[
        xmin=-10,
        xmax=10, 
        domain=-9.5:9.5,
        ymin=-3,
        ymax=3,
        y domain=-3:3,
        width=\textwidth,
    height=0.4\textwidth,
        axis x line=middle,
        axis y line=middle, 
        axis equal=true,
        xlabel=$x$, 
        ylabel=$y$,          
        samples=600,
        clip=true,
        xticklabel style={anchor=north,yshift=5mm},
        yticklabel style={anchor=west,xshift=0.5mm},
\] 


\addplot\[line width=1.5pt,domain=-9.5:-1.01,y domain=-3:3\]{\function};
\addplot\[line width=1.5pt,domain=-0.99:0.99, y domain=-3:3\]{\function};
\addplot\[line width=1.5pt,domain=1.01:9.5,y domain=-3:3\]{\function};
\AddLabel(4,-1.5){$f\!:\ y=\functionlabel$}

\end{axis}
\end{tikzpicture}
\end{document}

sample graph

7

You can use normal ticks for one half of the axis, and extra x ticks for the other half, and then set every extra x tick/.style={xticklabel style={anchor=north}}:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\def\function{rad(atan((1.73205*\x)/(1-(\x)^2)))}

\begin{axis}[
        xmin=-10,
        xmax=10, 
        domain=-9.5:9.5,
        ymin=-3,
        ymax=3,
        y domain=-3:3,
        axis x line=middle,
        axis y line=middle, 
        axis equal image,        
        samples=50,
        clip=true,
        xticklabel style={anchor=south},
        yticklabel style={anchor=west,xshift=0.5mm},
        xtick={5,10},
        extra x ticks={-10,-5},
        every extra x tick/.style={
            xticklabel style={anchor=north}        
        }
] 


\addplot[line width=1.5pt,domain=-9.5:-1.01,y domain=-3:3]{\function};
\addplot[line width=1.5pt,domain=-0.99:0.99, y domain=-3:3]{\function};
\addplot[line width=1.5pt,domain=1.01:9.5,y domain=-3:3]{\function};

\end{axis}
\end{tikzpicture}
\end{document}
  • Thanks very much for the nice trick (at least for me) and inspiration for next work. It works as I need. Thanks again. Jaroslav – Jaroslav Hajtmar Mar 9 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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