7

i'm using TikZ to recreate several graphics as a practice (and because i'm bored). I draw this graph but i need to add a legend for the dotted line (green) and the normal one (black). How can i do that without using PGF Plots? That restriction is only for a personal challenge bc in differents answers here in this site i've found that everyone uses that. I'm just starting to use the TikZ library, i'm a completely noob. Thanks.

Here's my code:

\begin{figure}[h!]
\centering
\begin{tikzpicture}
\draw [<->] (0,4) -- (0,0) -- (4,0);
\draw [dotted, ultra thick, color=darkpastelgreen] (0,3) -- (3,0);
\draw [-] (0,3) -- (3,0);
\node [left] at (0,3) {$100$};
\node [below] at (3,0) {$100$};
\node [below left] at (0,0) {0};
\node [below] at (4,0) {$S_{B}$};
\node [left] at (0,4) {$S_{A}$};
\draw [dashed, color=lightgray] (0,3) -- (3,3) -- (3,0);
\draw[decoration={brace,raise=5pt},decorate]
  (0,2.9) -- node[above right=6pt] {Agreement Zone} (2.9,0);
\end{tikzpicture}
\end{figure}

And i want something like this: enter image description here

Last question: is my code optimal? or I could do it in a more optimal way? I appreciate all kinds of help to improve my graphs.

oldenios
  • 340

2 Answers2

5

Welcome! Adding a legend is rather easy. All you need is a matrix and a pic,

\path (current bounding box.north east) 
 node[matrix,anchor=north east,draw,nodes={anchor=center},inner sep=2pt]  {
  \pic{sample=black}; & \node{$BR_A$}; \\
  \pic{sample={Dotted,ultra thick,color=darkpastelgreen}}; & \node{$BR_B$}; \\
 };

Here, the pic sample was defined to feature the style of the plot you use in your picture. One could make it more automatic but this would merely mean switching to pgfplots, which you do not seem to want. One may improve your code in many ways, but this requires more input from your side. A rather obvious way of improving it is to use the nice dotted line from here. Anyway, here is a somewhat more ergonomic code that is closer to your target picture. It avoids duplication of coordinates (to some extent) and uses -| and similar tricks.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\definecolor{darkpastelgreen}{RGB}{105,186,72}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[>=stealth,scale=pi/2,
    pics/sample/.style={code={\draw[#1] (0,0) --(0.6,0) ;}},
    Dotted/.style={% https://tex.stackexchange.com/a/52856/194703
    dash pattern=on 0.1\pgflinewidth off #1\pgflinewidth,line cap=round,
    shorten >=#1\pgflinewidth/2,shorten <=#1\pgflinewidth/2},
    Dotted/.default=3]
\draw [<->] (0,4) node[left] {$S_{A}$} -- (0,0) node[below left]  {$0$}
-- (4,0) node [below] {$S_{B}$};
\draw [-] (0,3)node[left]  {$100$}  -- (3,0) node[below]  {$100$};
\draw [Dotted,ultra thick, color=darkpastelgreen] (0,3)-- (3,0);
\draw [dashed, color=lightgray] (0,3) -| (3,0);
\draw[decoration={brace,raise=5pt},decorate,thick]
  (0,2.9) -- node[above right=2ex,xshift=-2ex,align=left] {Agreement\\ Zone} (2.9,0);
\path (current bounding box.north east) 
 node[matrix,anchor=north east,draw,nodes={anchor=center},inner sep=2pt]  {
  \pic{sample=black}; & \node{$BR_A$}; \\
  \pic{sample={Dotted,ultra thick,color=darkpastelgreen}}; & \node{$BR_B$}; \\
 };
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

3

This is my suggestion. Instead of pgfplots, I use TikZ only: putting pic (name circ is a small colored circle) along path.

An other way is using decoration along path.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[>=latex,
circ/.pic={\fill[green!50!black] (0,0) circle(1.5pt);}]
\draw (0,3)--(3,0);
\draw[<->] (0,4)--(0,0)--(5,0);
\draw[dashed,lightgray] (0,3)-|(3,0);
\draw[decoration={brace,raise=3pt},decorate]
    (0,3)--(3,0);
\foreach \i in {0,.05,...,1.05}
\path (0,3)--(3,0) pic[pos=\i]{circ};

\path
(0,3) node[left]{$100$}
(3,0) node[below]{$100$}
(0,0) node[below left]{0}
(5,0) node[below]{$S_{B}$}
(0,4) node[left]{$S_{A}$}
(2.2,2.2) node[align=left,scale=.8] {Agreement\\Zone};

% for legend
\begin{scope}[local bounding box=L,shift={(4.5,3.5)}]
\path (0,0) node (A) {$BR_A$}
(-90:.4) node (B) {$BR_B$};
\draw (A.west)--+(180:.6);
\foreach \i in {0,.3,...,1}
\path (B.west)--+(180:.6) pic[pos=\i]{circ};
\end{scope} 
\draw ([xshift=-2mm]L.south west) rectangle (L.north east);
\end{tikzpicture}
\end{figure}
\end{document}
Black Mild
  • 17,569