How can I draw a graph like this in LaTeX using pgfplot package?
Asked
Active
Viewed 213 times
1
Torbjørn T.
- 206,688
Ragnar
- 49
2 Answers
3
I have edited a little bit on the code given by @Claudio. and I came up with this solution.
\documentclass[10pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\draw[->, thick](0,0)--node[below]{$Tails(x)$}(5,0) ;
\draw[->, thick](0,0)--node[left]{$Heads(y)$}(0,5);
\draw[- ] (4,0)node[below]{$(n,0)$}--(0,4) node[right] {$(0,n)$};
\node[right] at (3.6,0.4) {$(n-1,1)$};
\node[right] at (0.4,3.6) {$(1,n-1)$};
\node[right] at (0.8,3.2) {$(2,n-2)$};
\node[right] at (3,3) {$\mathbf{x+y=n}$};
\fill (0,4) circle (1.5pt);
\fill (4,0) circle (1.5pt);
\fill (3.6,0.4) circle (1.5pt);
\fill (0.4,3.6) circle (1.5pt);
\fill (0.8,3.2) circle (1.5pt);
\end{tikzpicture}
\end{document}
Ragnar
- 49
-
\usepackage{amsmath}and then$\text{Tails}(x)$,$\text{Heads}(y)$. – Torbjørn T. Jun 13 '16 at 21:01 -
sorry for not mentioning \usepackage{amsmath} but the other two are done in a different way, as repositioned nodes. @TorbjørnT. – Ragnar Jun 13 '16 at 21:11
-
1My main point was that you don't want to write
TailsandHeadsin math mode, they are words, not a string of variables. I only mentionedamsmathbecause it defines\text. – Torbjørn T. Jun 13 '16 at 21:14 -
Alright, but '\mathbf' is also from 'amsmath ' package, if I am not wrong. – Ragnar Jun 13 '16 at 21:18
-
1No, you don't need
amsmathto use\mathbf(which your example proves, it works withoutamsmath). Don't thinkamsmathdefines any font styles at all actually, but for\mathbbfor example you needamsfonts. – Torbjørn T. Jun 13 '16 at 21:26
1
Your comment is almost a solution. You might want to use axis lines=middle to match the sketch from your question.
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
xlabel=$\mathrm{Tails}(x)$,
ylabel=$\mathrm{Heads}(y)$,
domain=0:15,no marks]
\addplot {-x + 10};
\addlegendentry{$x+y=n$}
\end{axis}
\end{tikzpicture}
\end{document}
Henri Menke
- 109,596


\addplot [ domain=0:15, samples=15, ] {- x + 10}; \addlegendentry{$x+y=n$} Thank you
– Ragnar Jun 13 '16 at 08:52{}above the text field). – Torbjørn T. Jun 13 '16 at 08:57