3

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}
3
  • Does your earlier questions not answer this: Moving a tick mark or Moving a tick mark on each axis but keeping the default tick marks? – Peter Grill Aug 10 '16 at 22:54
  • @Peter Grill Oh, you remember that. Hmmm. Good memory. – Adelyn Aug 10 '16 at 23:55
  • @Peter Grill With my code, all the tick marks are above the x-axis. I want 1 and 2 below the x-axis in their default positions. Are these default positions 0.6ex below the x-axis? In the second post that you referenced, you suggested yshift=0.6ex to move the labels above x-axis. – Adelyn Aug 10 '16 at 23:55
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}
7
  • 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 0: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 5:48
  • 1
    @Adelyn: Yep, tried to highlight that in the Notes section. – Peter Grill Aug 15 '16 at 6:23

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.