pgfplots を使用して特定のグラフを作成するにはどうすればよいですか?

pgfplots を使用して特定のグラフを作成するにはどうすればよいですか?

望んだ

このグラフやそれに似たグラフは作成できません。pgfplotsプロット用。

答え1

これは を使用した 1 つの可能な解決法ですpgfplots。ここで、内部ラベルx\iおよび は、を介してy\i青い線のみで定義されます。次に、青い線上の点から相対距離を持つ垂直線が描画されます。距離は変数 を介して与えられます。最後に、 およびにラベルを付けます。coordinate[pos=xx]0 < xx < 1\h\epsilon_1y_i, \hat y

ここに画像の説明を入力してください

コード

\documentclass{article}
\usepackage{pgfplots} 
\usetikzlibrary{calc}
\pgfplotsset{compat=1.11}
\pgfplotsset{every axis x label/.style={
  at={(1,0)},  below,  yshift=-5pt},
  every axis y label/.style={ at={(0,1)},
  left,xshift=-5pt}
  }
\begin{document}
\begin{tikzpicture}[scale=1]
\begin{axis}[ymax=5,ymin=0,,xmin=0,xmax=10,
    axis lines=left, xtick=\empty, ytick=\empty,
    ytick={2,4},xlabel=$x$, ylabel=$y$,
    yticklabels={$y_i$, $\hat y_x$},
    xtick={0},
    ]
\addplot+[domain=1:10,mark=none, thick] {1 + 0.25*x} 
\foreach \i/\p in {1/0.2,2/0.4,3/0.6,4/0.8}{
coordinate[pos=\p] (x\i)   % define vertical points for upward direction
}
\foreach \i/\p in {1/0.3,2/0.5,3/0.7,4/0.9}{
coordinate[pos=\p] (y\i)   % define vertical points for downward direction
};
\end{axis}
% draw lines showing variances
\foreach \i/\h in {1/0.5,2/0.5,3/1.2,4/0.5}{
\draw (x\i) --++ (0,\h);                   % relative distance for height upward
}
\foreach \i/\h in {1/0.5,2/1.2,3/0.5,4/0.7}{
\draw (y\i) --++ (0,-\h) coordinate(e\i);  % relative distance for height upward
}
\draw [decorate,decoration={brace,amplitude=5pt,raise=1pt}] 
(y2)--(e2) node [midway,right,xshift=5pt] {\footnotesize $\epsilon_1$};
\end{tikzpicture}
\end{document}

関連情報