如何在不使用 pgfplots 的情況下加入圖例?

如何在不使用 pgfplots 的情況下加入圖例?

我正在使用 TikZ 重新創建幾個圖形作為練習(並且因為我很無聊)。我繪製了這個圖表,但我需要為虛線(綠色)和普通線(黑色)添加圖例。如果不使用 PGF 圖,我該如何做到這一點?這個限制僅適用於個人挑戰,因為在本網站的不同答案中,我發現每個人都使用它。我剛開始使用 TikZ 庫,我完全是個菜鳥。謝謝。

這是我的程式碼:

\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}

我想要這樣的東西: 在此輸入影像描述

最後一個問題:我的程式碼是最優的嗎?或者我可以以更優化的方式做到這一點?我感謝為改進我的圖表提供的各種幫助。

答案1

歡迎!新增圖例相當容易。你所需要的只是 amatrix和 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$}; \\
 };

此處,pic範例被定義為具有您在圖片中使用的繪圖樣式。人們可以讓它更加自動化,但這只意味著切換到pgfplots您似乎不想要的。人們可以透過多種方式改進您的程式碼,但這需要您提供更多的輸入。改進它的一個相當明顯的方法是使用漂亮的虛線這裡。無論如何,這是一個更符合人體工學的代碼,更接近您的目標圖片。它避免了坐標的重複(在某種程度上)並使用-|和類似的技巧。

\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}

在此輸入影像描述

答案2

這是我的建議。我只使用 TikZ而不是pgfplots:將pic(名稱circ是一個小彩色圓圈)沿著path

另一種方法是沿路徑使用裝飾。

在此輸入影像描述

\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}

相關內容