Graph in Latex using Tikz and PGFplots

5k views Asked by At

How to draw this graph in LaTeX? I have used TikZ but I can't get it done exactly.

A graph to be drawn in Latex

This is what I have tried so far:

\begin{tikzpicture}
   \tkzInit[xmax=6,ymax=6,xmin=-6,ymin=-1]
   \tkzAxeXY
   \draw[thick] (6,3) -- (-4,5);
   \filldraw[black] (6,3) circle (2pt) node[anchor=west] {A(6,3)};
   \filldraw[black] (-4,5) circle (2pt) node[anchor=east] {B(-4,5)};  
\end{tikzpicture}

Output Pic

1

There are 1 answers

0
jf_ On

Using regular pgfplots you could do the following: Draw the line with the points you choose and label them with nodes. Then draw the additional distance markers using \draw in the axis coordinate system (The concrete positions of all elements may have to be adjusted).

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\tiny
    \begin{axis}[
        width  = 6cm,
        height = 4cm,
        ymajorgrids = true,
        ymin=-1,
        ymax=7,
        xmin=-6,
        xmax=6,
        xtick distance=1,
        ytick distance=1,
        axis y line*=center,
        axis x line*=center,        
        enlarge x limits=.12
    ]
\addplot+[sharp plot] coordinates {(6,3) (1,4) (-4,5) } node[below=1mm, pos=0] {A(6,3)}
    node[pos=0.45,below=1mm] {P(x,y)}
    node[pos=1,below=1mm] {B(-4,5)};

\draw[|-|] (axis cs:1.2,5) -- node [above, rotate=-10] {m=13} (axis cs:6.3,3.9);   
\draw[|-|] (axis cs:-3.8,6) -- node [above, rotate=-10, pos=.3] {n=2} (axis cs:1.2,5);   
   
\end{axis}
\end{tikzpicture}

\end{document}

Output:

out pic