# Adding text and labels to a pgfplot

I am very new to LaTeX and pgfplots. I would like to reproduce this image:

Is this possible using pgfplots? All I have so far is a straight line!

\begin{tikzpicture}[scale=0.70]
\begin{axis}[ymin=0, ymax=12,xmax=10, xlabel=independent variable, ylabel=dependent variable]
\end{axis}
\end{tikzpicture}
• Hi Francesca, welcome! That kind of diagram is probably better done with plain tikz, no need to use pfgplots (although it can be used, obviously). Have you seen the first tutorial chapter of the tikz manual? It's a very similar diagram... Oct 1, 2018 at 17:58

Welcome to TeX.SE!

\documentclass[border=3.14mm,standalone]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[scale=0.70]
\begin{axis}[ymin=0, ymax=12,xmax=10,
xlabel=independent variable, ylabel=dependent variable,enlargelimits=0.7]
\addplot coordinates{(2,4) (8, 10)} node[midway,above left] {$d$}
node[pos=0,left] {$A(x_1,y_1)$} node[pos=1,above] {$B(x_2,y_2)$};
\addplot[black,no marks] coordinates{(2,4) (8,4) (8, 10)}
node[pos=0.25,below] {$x_2-x_1$}
node[pos=0.75,right] {$y_2-y_1$};
\end{axis}
\end{tikzpicture}
\end{document}

Of course, one could also reproduce your screen shot more closely.

\documentclass[border=3.14mm,standalone]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=2, ymax=10,xmin=0,xmax=10,unit vector ratio=1 1,
xlabel=independent variable, ylabel=dependent
variable,enlargelimits=0.7,grid=major,
xtick={-10,-8,...,20},xticklabel=\empty,
ytick={-10,-8,...,20},yticklabel=\empty,
xlabel={$x$},xlabel style={at=(xticklabel cs:1)},
ylabel={$y$},ylabel style={at=(yticklabel cs:1),rotate=-90}]
\addplot[draw=red,mark=*,mark options=blue] coordinates{(2,4) (8, 10)} node[midway,above left] {$d$}
node[pos=0,left] {$A(x_1,y_1)$} node[pos=1,above] {$B(x_2,y_2)$};
\addplot[black,no marks] coordinates{(2,4) (8,4) (8, 10)}
node[pos=0.25,below] {$x_2-x_1$}
node[pos=0.75,right] {$y_2-y_1$};
\end{axis}
\end{tikzpicture}
\end{document}

Welcome to TeX.SE !

As Rmano said, you can set your picture with tikz. Here is your exact picture in tikz with the corresponding commands :

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
%%Set grid and axis
\draw[step=0.5, help lines] (-0.5,-0.5) grid (4.5,4.5);
\draw[->] (-0.5,0) -- (4.5,0) node[above]{$x$};
\draw[->] (0,-0.5) -- (0,4.5) node[right]{$y$};

%%Set scale on the axis
\foreach \x in {0.5,1,...,4}{
\draw (\x,0.1) -- (\x,-0.1);}
\foreach \y in {0.5,1,...,4}{
\draw (0.1,\y) -- (-0.1,\y);}

\node[fill=blue, circle, inner sep=1pt, minimum size=1pt] (A) at (1.5,1){};
\node[fill=blue, circle, inner sep=1pt, minimum size=1pt] (B) at (3,3){};
\draw[red] (A)  -- (B) node [midway, above left, black] {$d$};
\draw (A) node[left]{$A(x_1,y_1)$} -- (3,1) node[midway, below] {$x_2-x_1$};
\draw (B) node[left]{$B(x_2,y_2)$} -- (3,1)node[midway, right] {$y_2-y_1$};
\end{tikzpicture}
\end{document}

With the result :

EDIT : I guess the point C is on the third corner. I keep the notation with $C(x_3,y_3)$. In my opinion, it's better if you labeled the coordinates with the label of the letter : $A(x_a,y_a)$. Tips : Try to modify some stuff on the code to understand the tikz code ;)

\begin{tikzpicture}
%%Set grid and axis
\draw[step=0.5, help lines] (-0.5,-0.5) grid (4.5,4.5);
\draw[->] (-0.5,0) -- (4.5,0) node[above]{$x$};
\draw[->] (0,-0.5) -- (0,4.5) node[right]{$y$};

%%Set scale on the axis
\foreach \x in {0.5,1,...,4}{
\draw (\x,0.1) -- (\x,-0.1);}
\foreach \y in {0.5,1,...,4}{
\draw (0.1,\y) -- (-0.1,\y);}

%%Set point A, B, C
\node[fill=blue, circle, inner sep=1pt, minimum size=1pt] (A) at (1.5,1){};
\node[fill=blue, circle, inner sep=1pt, minimum size=1pt] (B) at (3,3){};
\node[fill=blue, circle, inner sep=1pt, minimum size=1pt] (C) at (3,1){};

%%Draw lines and labels on the node
\draw[red] (A)  -- (B) node [midway, above left, black] {$d$};
\draw (A) node[left]{$A(x_1,y_1)$} -- (C) node[midway, below] {$x_2-x_1$};
\draw (B) node[left]{$B(x_2,y_2)$} -- (C) node[midway, right] {$y_2-y_1$} node[right]{$C(x_3,y_3)$};
\end{tikzpicture}
• This is fantastic! How would I add the point C at the corner? Oct 2, 2018 at 0:26
• @Francesca could you add an image where the point C appear, please? In your image it is not there. Oct 2, 2018 at 4:18
• Thank you very much! The code you shared is much more advanced than my experience so far. I played around with it before and then weird things started to happen. I appreciate your help! Oct 2, 2018 at 23:06