
私は練習として(そして退屈なので)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
ようこそ!凡例を追加するのはとても簡単です。必要なのは とmatrix
だけです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
これは私の提案です。 の代わりにpgfplots
、TikZ のみを使用します。つまり、 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}