40

I try to draw something like this:

enter image description here

To do that, I have written some code using TikZ, unfortunately I don't get what I want.

\documentclass{report}
\usepackage{tikz}
  \usetikzlibrary{automata,topaths}
\begin{document}
  \begin{tikzpicture}[x=1cm,y=0.4cm]
    \draw[<->] (-4,0)--(4,0); % l'axe des abscisses
    \draw[<->] (0,-5)--(0,5); % l'axe des ordonnées
    \draw[-] (-3,-2)--(3,4); % l'axe des abscisses

    \draw (-2,1.5) node[anchor=south] {.};
    \draw (-1,1) node[anchor=south] {.};
    \draw (-2,3) node[anchor=south] {.};
    \draw (-1,2.5) node[anchor=south] {.};
    \draw (1,3) node[anchor=south] {.};
  \end{tikzpicture}
\end{document}

The result is:

enter image description here

1
  • 10
    You can use \draw (0,0) circle[radius=2pt]; and \fill (0,0) circle[radius=2pt];. Jun 26, 2012 at 21:30

4 Answers 4

34

I would recomend you start using pgfplots to draw graphs. But, if you really want to use a TikZ \node to place the coordinates you can use a \foreach loop to simplify things:

\foreach \Point in {(-2,1.5), (-1,1), (-2,3), (-1,2.5), (1,3)}{
    \node at \Point {\textbullet};
}

enter image description here

Note:

  • The red and blue nodes are to show that the points are being properly placed at the coordinates.

Code:

\documentclass{report}

\usepackage{tikz}
%\usetikzlibrary{automata,topaths}% note neded for this.

 \begin{document}

 \begin{tikzpicture}[x=1cm,y=0.4cm]


 \draw[latex-latex, thin, draw=gray] (-4,0)--(4,0) node [right] {$x$}; % l'axe des abscisses
 \draw[latex-latex, thin, draw=gray] (0,-5)--(0,5) node [above] {$y$}; % l'axe des ordonnées
 \draw[thick] (-3,-2)--(3,4); % l'axe des abscisses

\foreach \Point in {(-2,1.5), (-1,1), (-2,3), (-1,2.5), (1,3)}{
    \node at \Point {\textbullet};
}

\foreach \Point in {(2,-1.5), (1,-1), (2,-3), (1,-2.5), (1,-3)}{
    \node at \Point {$\circ$};
}

% to ensure that the points are being properly centered:
\draw [dotted, gray] (-4,-6) grid (5,5);
\node [red] at (3,2.5) {\textbullet};
\node [blue] at (3,-2.5) {$\circ$};

\end{tikzpicture}
\end{document}
5
  • 4
    IMHO, this is a bad solution: \textbullet and $\circ$ are not perfectly centering on coordinates! Jun 26, 2012 at 22:01
  • @PolGab: Have updated solution to show points are properly placed. Jun 26, 2012 at 22:15
  • 3
    Are you really sure? Have you used a magnifying glass? Compare your red bullet with \fill (3,2.5) circle(2pt);... Jun 26, 2012 at 22:21
  • @PolGab: Hmmm... Good point. I will look into why this is the case. Thanks for pointing this out. Jun 26, 2012 at 23:08
  • Look at my answer... Jun 27, 2014 at 15:11
38

Just to show how this could be done with pgfplots:

\documentclass{article}
\usepackage{pgfplots}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmin=-10, xmax=10,
    ymin=-10, ymax=10,
    xtick=\empty, ytick=\empty
]
\addplot [only marks] table {
-10 -4
-8  2
-5  5   
-4  7
-3  3
0   6
};
\addplot [only marks, mark=o] table {
-4  -5
-2  -1
-1  -4
2   -3
4   3
6   -1
};
\addplot [domain=-10:10, samples=2, dashed] {1*x+3};
\end{axis}
\end{tikzpicture}
\end{document}
1
  • I get an error "! Package pgfplots Error: Could not read table file '" -10 -4 -8 2 -5 5 -4 7 -3 3 0 6 "' in 'search path=.'. In case you intended to provide inline data: mayb e TeX screwed up your end-of-lines? Try row sep=crcr' and terminate your lines with \\' (refer to the pgfplotstable manual for details)."
    – A J
    Jun 23, 2023 at 23:09
25

A simple solution using my first comment:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=1cm,y=0.4cm]
  \draw[-stealth] (-3,0)--(4,0) node[right]{x}; % x axis
  \draw[-stealth] (0,-3)--(0,5) node[above]{y}; % y axis
  \draw[dashed] (-3,-2)--(3,4) node[above]{H}; % a line...

  \draw (-2,1.5) circle[radius=2pt];
  \fill (-1,1)  circle[radius=2pt];
  \draw (-2,3)  circle[radius=2pt];
  \fill (-1,2.5)  circle[radius=2pt];
  \draw (1,3)  circle[radius=2pt];
\end{tikzpicture}
\end{document}

enter image description here

2
  • You could combine all the filled/unfilled circles into a single \fill/\draw command (\fill [radius=2pt] (-1,1) circle [] (-1,2.5) circle;), which saves some typing and makes it easier to adjust the colour and radius for all circles simultaneously.
    – Jake
    Jun 27, 2012 at 5:37
  • 1
    @Jake: You are right: there are plenty of ways to reduce the size of this code. But it is perhaps too much at once for a beginner. Jun 27, 2012 at 6:19
6

If you want to draw several points with labels using TikZ the easiest way to do this is

\foreach \Point/\PointLabel in {(1,1)/A, (3,1)/B, (1,4)/P_1, (3,4)/P_1}
\draw[fill=black] \Point circle (0.05) node[above right] {$\PointLabel$};

Example

enter image description here

Code

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}

    \begin{tikzpicture}

        % help lines
        \draw[step=1,help lines,black!20] (-0.95,-0.95) grid (4.95,4.95);
        % axis
        \draw[thick,->] (-1,0) -- (5,0);
        \draw[thick,->] (0,-1) -- (0,5);

        % points
        \foreach \Point/\PointLabel in {(1,1)/A, (3,1)/B, (1,4)/P_1, (3,4)/P_1}
        \draw[fill=black] \Point circle (0.05) node[above right] {$\PointLabel$};

    \end{tikzpicture}

\end{document}
0

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