3

I have a function ploted with pgfplots:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{helvet}
\usepackage{sansmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
    xlabel=$X$,
    ylabel=$Y$,
    axis x line=center,
    axis y line=center,
    grid,
    grid style={green},
    major tick style={draw=black, thick},
    xticklabels={,,},
    yticklabels={,,},
    xmin=-5,
    xmax=5,
    ymin=-5,
    ymax=5,
    xtick={-5,-4,...,5},
    ytick={-5,-4,...,5},
    axis line style={thick},
    samples=250,
    unit vector ratio*=1 1,
    tick label style = {font=\sansmath\sffamily},
    every axis label = {font=\sansmath\sffamily},
    legend style = {font=\sansmath\sffamily},
    label style = {font=\sansmath\sffamily},
    ]

    \addplot [ultra thick, red]{42/27 *((x*x*x)/3 - (x*x)/2 - 2*x) +59/27} node [pos=0.75,left,black,{font=\sansmath\sffamily}] {$f(x)$};

    \draw[very thick, blue] (axis cs: -2.5,4)--(axis cs: 0.5,4);
    \draw[very thick, blue] (axis cs: 3.5,-3)--(axis cs: 0.5,-3);
    \node[above] at (axis cs: -1,4) {\sffamily\textit P};
    \node[below] at (axis cs: 2,-3) {\sffamily\textit Q};

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

The graphic generated by that code is the first one. The second one is the one that I want to achieve.

Then, I want to do severals changes that can be seen in both graphics:

  • Enlarge axis to put "X" and "Y" outside the grid, without putting a tick in x = −6 and y = −6. (I mean, I want to get the axis of the second figure, but not to finish the X axis in the left part by: "|---", but yes by "---"... and the same to the Y axis).
  • Only put labels in x = 1 and in y = 1.
  • (If it's possible) Remove of the font=\sansmath\sffamily of the f(x) node and include it in the axis options to affect to all nodes.
  • (If it's possible). Isn't there any other method to indicate tangent lines in points like P or Q, instead of doing it manually in TikZ as I did?

Graphics

4
  1. You could use axis line style={shorten > = -0.5cm, shorten < = -0.5cm} to extend the axis lines. Note that this doesn't increase the bounding box, so you might have to adjust this manually.

  2. You can use extra x ticks={1}, extra x tick label=\pgfmathprintnumber{\tick} to print the tick labels.

  3. You can put font=\sansmath\sffamily in the axis options to apply it to all text in the axis.
  4. You should probably ask a separate question for this.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{helvet}
\usepackage{sansmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
    xlabel=$X$,
    ylabel=$Y$,
    axis x line=center, xlabel style={anchor=south west},
    axis y line=center, ylabel style={anchor=south west},
    grid,
    grid style={green},
    major tick style={draw=black, thick},
    xmin=-5,
    xmax=5,
    ymin=-5,
    ymax=5,
    xtick={-5,-4,...,5},
    xticklabels={},
    extra x ticks={1},
    extra x tick label=\pgfmathprintnumber{\tick},
    ytick={-5,-4,...,5},
    yticklabels={},
    extra y ticks={1},
    extra y tick labels=1,
    axis line style={thick, shorten > = -0.5cm, shorten < = -0.5cm},
    samples=50,
    unit vector ratio*=1 1,
    font=\sansmath\sffamily,
    ]

    \addplot [ultra thick, red, smooth]{42/27 *((x*x*x)/3 - (x*x)/2 - 2*x) +59/27} node [pos=0.75,left,black] {$f(x)$};

    \draw[very thick, blue] (axis cs: -2.5,4)--(axis cs: 0.5,4);
    \draw[very thick, blue] (axis cs: 3.5,-3)--(axis cs: 0.5,-3);
    \node[above] at (axis cs: -1,4) {$P$};
    \node[below] at (axis cs: 2,-3) {$Q$};
\end{axis};

\path (current axis.south west) +(-0.5cm,-0.5cm) (current axis.north east) +(0.5cm,0.5cm);
\end{tikzpicture}
\end{document}

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.