I have four given points, for example

C(-3, 4)
D(-3, 0)

Using TikZ/PgfPlots, how can I draw them in the Cartesian coordinate system, show the points, each accompanied by its name (A,B,C,D) and then draw the sides of the quadrilateral ABCD?

I'm new to TikZ :)


Below I include two options: the first one uses "pure" TikZ and the second one uses the great tkz-euclide package.

One possibility (the code has explanatory comments):


  inner sep=1.5pt


% The x-axis
  (-3.5,0) -- (5.5,0);
% The y-axis
  (0,-0.5) -- (0,4.5);
% Ticks for x-axis
\foreach \Valor in {-3,...,5}
  \draw (\Valor,2pt) -- (\Valor,-2pt) node[below] {\Valor};
% Ticks for y-axis
\foreach \Valor in {1,...,4}
  \draw (-2pt,\Valor) node[left] {\Valor} -- (2pt,\Valor);
% define coordinates at the desired points
  coordinate (A) at (3,2)
  coordinate (B) at (5,4)
  coordinate (C) at (-3,4)
  coordinate (D) at (-3,0);
% draw lines between the coordinates  
\draw (A) -- (B) -- (C) -- (D) -- cycle;
% Label the points
\foreach \Nombre/\Pos in {A/right,B/right,C/left,D/left}
  \node[\Pos] at (\Nombre) {$\Nombre$};
  \node[smalldot] at (\Nombre) {};


enter image description here

If you are starting with TikZ, I'd like to suggest you the tkz-euclide package which is built on top of TikZ and has a more intuitive series of commands and some predefined features which could help you to easily draw euclidean figures (compare the code below with the one using "pure" TikZ):



\tkzDrawSegments[color=red!70!black,line width=1pt](A,B B,C C,D D,A)


enter image description here

  • The solution is great and very intuitive. But in my real example, I need a much bigger range of x, y variables. If I do \tkzInit[xmax=10,xmin=-10,ymax=10,ymin=-10] then the produced diagram is too big, so it goes out of the page range. How can I scale it to the page size?
    – marmistrz
    Mar 7 '15 at 15:56
  • 1
    @marmistrz add scale=<factor> and possibly also transform shape to the tikzpicture options, as in \begin{tikzpicture}[scale=0.5,transform shape]...\end{tikzpicture} (to scale it down 50%). By the way, don't forget that now that you have several answers, you can accept the one you consider best solved your problem by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. Mar 7 '15 at 16:00

With pgfplots


\begin{axis}[axis lines=middle,enlargelimits,
    xlabel=$x$,ylabel=$y$,nodes near coords]
\addplot+[point meta=explicit symbolic] coordinates{
(3,2)  [A]
(5,4)  [B]
(-3, 4)[C]
(-3, 0)[D]
}; % Add "-- cycle" if you want to close the drawing

enter image description here


Now with MetaPost (and LuaLaTeX).

\documentclass[12pt, border = 5bp]{standalone}
    u = 1.5cm; len := 3bp;
    xmin := -3.5u; xmax := 5.75u; ymin := -.5u; ymax := 4.75u;
    pair A, B, C, D; A = u*(3,2); B = u*(5,4); C = u*(-3, 4); D = u*(-3,0);
        labeloffset := 5bp;
        for i = -3 upto 5:
            if i <> 0: draw (i*u, -len) -- (i*u, len); 
                label.bot("$" & decimal i & "$", (i*u, 0)); fi
        for j = 1 upto 4:
            draw (-len, j*u) -- (len, j*u);
            label.lft("$" & decimal j & "$", (0, j*u));
        drawarrow (xmin, 0) -- (xmax, 0); drawarrow (0, ymin) -- (0, ymax);
        draw A -- B -- C -- D -- cycle; 
        labeloffset := 3bp;
        dotlabel.rt("$A$", A); dotlabel.rt("$B$", B);
        dotlabel.lft("$C$", C); dotlabel.ulft("$D$", D);
        label.llft("$O$", origin); label.bot("$x$", (xmax, 0)); label.lft("$y$", (0, ymax));

enter image description here

While I'm at it: another, with the mfpic package:

    \pointdef{A}(3, 2)
    \pointdef{B}(5, 4)
    \pointdef{C}(-3, 4)
    \pointdef{D}(-3, 0)
    \polygon{\A, \B, \C, \D}
    \point{\A, \B, \C, \D}
    \xmarks{-3 upto -1}\xmarks{1 upto 5}
    \ymarks{1 upto 4}
    \tlabels{[tr](0, 0){$O$} [tc](\xmax, 0){$x$} [cr](0, \ymax){$y$}
        [cl](\Ax, \Ay){$A$} [cl](\Bx, \By){$B$} [cr](\Cx, \Cy){$C$} [br](\Dx, \Dy){$D$}}
    \axislabels{x}{{$-3$}-3, {$-2$}-2, {$-1$}-1, {$1$}1, {$2$}2, {$3$}3, {$4$}4, {$5$}5}
    \axislabels{y}{{$1$}1, {$2$}2, {$3$}3, {$4$}4}

Run it with LaTeX first (with whatever engine), then with MetaPost, then again with LaTeX.

enter image description here

Edit Once again, this time with Asymptote, short and efficient as ever. To be run with the asy command.

import graph;
real Xmin=-4, Xmax=5, Ymin=-.5, Ymax=5;
pair A = (3, 2), B = (5,4), C = (-3, 4), D = (-3, 0);
draw (A--B--C--D--cycle);
xaxis(xmin=Xmin, xmax=Xmax, Ticks(Step=1, OmitTick(0, Xmax)), arrow=Arrow);
yaxis(ymin=Ymin, ymax=Ymax, Ticks(Step=1, OmitTick(0, Ymax)), arrow=Arrow);
label("$x$", (Xmax, 0), S); label("$y$", (0, Ymax), W);
dot(Label("$A$", A, E)); label("$O$", (0, 0), SE); dot(Label("$B$", B, E));
dot(Label("$C$", C, W)); dot(Label("$D$", D, NW));

enter image description here

  • +1 Really nice. What is the difference between a = 1cm and a := 1cm? Mar 7 '15 at 9:52
  • 1
    @HenriMenke The latter is an assignment: the numeric variable a takes the value 1cm and if it had another value, it is discarded. The former is an equation: it checks that a is equal to 1cm or not. If a already had a value happening to be different from 1cm, MetaPost stops with an error message: inconsistent equation. If a was not given any value previously, the equation a = 1cm then is solved (MetaPost has a built-in solver of linear equations systems written implicitly), and here it means that a will take 1cm as value. So both formulations are (in that case) equivalent. Mar 7 '15 at 10:27
  • 1
    @HenriMenke By the way, I normally use assignments to give a value to a numeric variable (this way preventing some unpleasant surprises) but in this precise case I forgot the : of := :-). Another way of doing it would have been to write numeric a; a = 1cm;. numeric a; clears any former value a may have had, and then gives the value 1cm to a by the way of equation solving, but it's longer to write. :-) Mar 7 '15 at 10:35

A PSTricks solution using the pstricks-add package:




\begin{pspicture}(-3.5,-0.6)(6,5) % boundry found manually




Try this one. You'll just have to fix the axis if needed.

    \draw[->] (-10,0) -- (10,0) node[below] {$x$};
    \draw[->] (0,-5) -- (0,10) node[left] {$y$};

    \draw (3,2) -- (5,4) -- (-3,4) -- (-3,0);

  • That's true. I just copied this from a project I'm working on and forgot to remove it.
    – Bjartmar
    Mar 6 '15 at 18:13

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.