4

I have a plot of a cubic polynomial. I want to have tick marks along the x-axis to show that the root is between -1 and 0. With the default positioning of tick marks, the label -1 would interfere with the plot. I want to place the labels -2 and -1 above the x-axis and the 1 and 2 below the xaxis. In particular, I would like the spacing of -2 and -1 above the x-axis to be the same as the spacing of 1 and 2 below the x-axis.

\documentclass[10pt]{amsart}

\usepackage{tikz}
\usetikzlibrary{calc,intersections,}

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


\usepackage{mathtools,array}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=3.5in, clip=false,
    axis lines=middle,
    xmin=-2.5,xmax=2.5, domain=-2:2,
    ymin=-10,ymax=12,
    restrict y to domain=-10:12,
    xtick={-2,-1},
    ticklabel style={font=\tiny, anchor=south},
    xticklabels={\makebox[0pt][r]{$\tiny-$}2, \makebox[0pt][r]{$\tiny-$}1},
    extra x ticks={1,2},
    extra x tick labels={1,2},
    extra x tick style={font=\tiny},
    ytick={\empty},
    xlabel=$x$,ylabel=$y$,
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[samples=201, domain=-2:2] {x^3 + x + 1)} node[right,pos=1,font=\footnotesize]{$y = x^{3} + x + 1$};

\end{axis}
\end{tikzpicture}

\end{document}
Adelyn
  • 3,373

1 Answers1

4

Applying tick label style={below} will position the label below at the correct default distance from the x-axis:

enter image description here

Notes:

  • Font \tiny is not valid in math mode.
  • Not sure what you were doing with the \makebox in the xticklabel style so have removed that.
  • It appears that the default distance of the tick labels is different if the label is above, or below the axis. You can see this if you replace

    ticklabel style={font=\tiny, above}
    

    with

    ticklabel style={font=\tiny, below}
    

    I am not sure why these the two default settings are different. To tweak this, I added a yshift=0.5ex to the extra x tick style.

Code:

\documentclass[border=2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,intersections,}

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


\usepackage{mathtools,array}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=3.5in, clip=false,
    axis lines=middle,
    xmin=-2.5,xmax=2.5, domain=-2:2,
    ymin=-10,ymax=12,
    restrict y to domain=-10:12,
    xtick={-2,-1},
    ticklabel style={font=\tiny, above},
    xticklabels={$-2$, $-1$},% <--- corrected
    extra x ticks={1,2},
    extra x tick labels={1,2},
    extra x tick style={tick label style={red, below, yshift=0.5ex}},% <--- added "tick label style"
    ytick={\empty},
    xlabel=$x$,ylabel=$y$,
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[samples=201, domain=-2:2] {x^3 + x + 1)} 
        node[right,pos=1,font=\footnotesize]{$y = x^{3} + x + 1$};

\end{axis}
\end{tikzpicture}

\end{document}
Peter Grill
  • 223,288
  • There is one aspect of your display that is not appealing. The labels for the tick marks at -2 and -1 are right on the tick marks. How do I get them "the same distance" above the tick marks as 1 and 2 are below the tick marks? – Adelyn Aug 11 '16 at 11:48
  • @Adelyn: The labels are centered (the number includes the negative sign). You can use your \makebox trick to change that if you wish. – Peter Grill Aug 12 '16 at 00:50
  • I didn't say "right of" the tick marks. I said "right on" the tick marks. Look at the display that you get from your code. The labels -2 and -1 are much closer to the x-axis than the the labels 1 and 2. How do I get them "the same distance" from the x-axis? – Adelyn Aug 12 '16 at 12:44
  • 1
    @Adelyn: Sorry, did not read your comment carefully enough. Yes, it appears that the default distance from the x-axis is different if the label is below or above. Have provided a solution, so that you can adjust this. – Peter Grill Aug 13 '16 at 05:48
  • Thanks. I see that you put a value for the yshift option within the tick label style option. – Adelyn Aug 14 '16 at 19:27
  • 1
    @Adelyn: Yep, tried to highlight that in the Notes section. – Peter Grill Aug 15 '16 at 06:23
  • Thanks. I appreciate the additional comment. It is odd that the default distances using above and below are different. – Adelyn Aug 16 '16 at 14:27