2

How to remove extreme labels on each axis ?

Why does the shape of the curve is not limited to the upper edge of the grid ?

I find that the curve is not very smooth, especially at the apex of the parabola. I am a user Metapost, and I do not have this trouble if I draw the same curve.

   \documentclass[11pt,a4paper]{article} 
   \usepackage{geometry}
   \geometry{margin=1in} 
   \usepackage{tikz}
   \usepackage{tkz-euclide}
   \usetikzlibrary{calc,shapes.misc}
   \usepackage{pgfplots}
   \pgfplotsset{compat=1.8}

   \begin{document}
   \begin{center}
   \begin{tikzpicture}
   \begin{axis}[
     restrict x to domain=-3:3, xmax=3, xmin=-3,
     restrict y to domain=-3:5, ymax=5, ymin=-3,
     x=1cm,
     y=1cm,
     axis lines=middle,
     line width = 0.35mm,
     xtick={-3,-2,-1,0,1,2,3},
     ytick={-3,-2,-1,0,1,2,3,4,5},
     grid=major,
     >=stealth,
     compat=newest,
     x label style={at={(axis description cs:0.98,0.32)},anchor=south},
     y label style={at={(axis description cs:0.5,0.98)},rotate=-90,anchor=east},
     xlabel={$x$},
     ylabel={$y$}, 
      ]
     \addplot [red,line width=1,smooth,domain=-3:3] {2*x^2-x-2} ;
     \node[below right] at (1,-1){A}; 
     \node[below left] at (0,0){$0$}; 
     \node[fill=red,circle,scale=0.3] at (1,-1){};
     \end{axis}
     \end{tikzpicture} 
     \end{center}
     \end{document}

enter image description here

6
  • 1
    Which labels are the "extreme" ones? Could you also clarify what you mean by limited to the upper edge of the grid? Apr 3, 2015 at 18:04
  • @Christopher Thank you for your reply. Indicating samples=300, you solved a problem. In terms of extreme labels, I want to remove -3 and 3 on the x-axis and -3 and 5 and the y-axis.
    – Fabrice
    Apr 3, 2015 at 19:10
  • 1
    Just the labels? If so please see my updated answer. If you want to shrink the graph to not include these areas then you can change the domain. Apr 3, 2015 at 20:46
  • I see! I didn't realise you still wanted the ticks just without the labels. I've updated my answer accordingly. Apr 4, 2015 at 10:13
  • @Christopher Thank you, sorry for my english still be improved:-)
    – Fabrice
    Apr 4, 2015 at 10:16

1 Answer 1

3

Adding additional samples seems to make the curve go all the way to the top of the grid. This is achieved by adding samples=300, to your axis.

To remove the labels but not remove the ticks as you requested you can employ the xticklabelsand yticklabels options to specify where to place the labels. The \empty bits make the label blank and if you don't include them the labels will shift.

  \documentclass[11pt,a4paper]{article} 
   \usepackage{geometry}
   \geometry{margin=1in} 
   \usepackage{tikz}
   \usepackage{tkz-euclide}
   \usetikzlibrary{calc,shapes.misc}
   \usepackage{pgfplots}
   \pgfplotsset{compat=1.8}

   \begin{document}
   \begin{center}
   \begin{tikzpicture}
   \begin{axis}[
     restrict x to domain=-3:3, xmax=3, xmin=-3,
     restrict y to domain=-3:5, ymax=5, ymin=-3,
     x=1cm,
     y=1cm,
     axis lines=middle,
     line width = 0.35mm,
     xtick={-2,-1,0,1,2},
     xticklabels={\empty,-2,-1,0,1,2},
     ytick={-2,-1,0,1,2,3,4},
     yticklabels={\empty,-2,-1,0,1,2,3,4,\empty},
     grid=major,
     samples=300,
     >=stealth,
     compat=newest,
     x label style={at={(axis description cs:0.98,0.32)},anchor=south},
     y label style={at={(axis description cs:0.5,0.98)},rotate=-90,anchor=east},
     xlabel={$x$},
     ylabel={$y$}, 
      ]
     \addplot [red,line width=1,smooth,domain=-3:3] {2*x^2-x-2} ;
     \node[below right] at (1,-1){A}; 
     \node[below left] at (0,0){$0$}; 
     \node[fill=red,circle,scale=0.3] at (1,-1){};
     \end{axis}
     \end{tikzpicture} 
     \end{center}
     \end{document}

enter image description here

4
  • 1
    Instead of increasing the number of samples (which will increase compilation time), you can just use y domain=-3:5 instead of restrict y to domain=-3:5.
    – erik
    Apr 3, 2015 at 23:17
  • @erik This is true, but the curve is less smooth.
    – Fabrice
    Apr 4, 2015 at 8:31
  • 1
    Of course the curve will be more smooth with more than 10x the default number of samples. I just wanted to point out there is an alternative way to extend the curve.
    – erik
    Apr 4, 2015 at 19:21
  • @erik Indeed. The number of samples can be tuned of course to reduce compile time I just used it as an example and it's a fortunate double fix of OP's problem. Apr 4, 2015 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .