我正在尝试在 tikz 中重新创建该图像。
我创建的情节是:
\documentclass[svgnames, pstricks]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\title{Python Notebook}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
lbl/.style = {text=black, anchor=south, sloped},
lbl1/.style={text=black, anchor=south,sloped},
dot/.style = {circle, fill, inner sep=1.6pt,
node contents={}}
]
\begin{axis}[
axis lines=middle,
axis equal,
xlabel=$x$, x label style={anchor=west},
ylabel=$y$, y label style={anchor=south},
grid=both,
ytick={-5,-4,...,9},
ticklabel style={font=\footnotesize, fill=white,
inner sep=2pt},
ymin=-2.5, ymax=6,
no marks,
every axis plot post/.append style={blue, thick}
]
\addplot coordinates{(-1,0) (5,5) } node [near start, lbl] {$y=x+1$};
\addplot coordinates{(-1,-1) (5,4)} node[near start, lbl]{$y=x+0.3$};
\addplot coordinates{(-1,-2) (5, 3)}
node[near end, lbl] {$y=x-0.8$};
\node at (1,3) [dot,label=right:${(1,3)}$];
\node at (3,4) [dot,label=right:${(3,4)}$];
\node at (2,1) [dot, label=right: ${(2,1)}$];
\node at (4,3) [dot,label=right: ${(4,3)}$];
\end{axis}
\end{tikzpicture}
\caption{Fitting a regression Line}
\label{fig:enter-label}
\end{figure}
Referring to Fig \ref{fig:enter-label}.
\end{document}
我试图做的是提高图表的可读性。具体来说:
- 尝试拉伸图形,使所有元素不会相互堆叠
- 使字体大小足够大,使其在文章类文档中可读
- 其他细微调整让剧情更加直观
答案1
我的建议:
- 我认为使用图例更易读。如果没有颜色,可以使用虚线。
- 使用正确的线(让我们
pgfplots
计算它们!) - 调整限制和图例位置以使结果令人满意。
- 使用
\footnotesize
字体应该可以保证可读性,但是不要扩大规模图形;使用width
和height
适合列,然后重复第 3 点。
\documentclass[svgnames]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\title{Python Notebook}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
lbl dots/.style = {text=black, fill=white, font=\footnotesize,
inner sep=0pt},
dot/.style = {circle, fill, inner sep=1.6pt,
node contents={}}
]
\begin{axis}[
axis lines=middle,
axis equal,
ymin=-2.5, ymax=5.5,
xmin=-4, xmax=5.5,
domain = -2:4, % added
xlabel=$x$, x label style={anchor=west},
ylabel=$y$, y label style={anchor=south},
grid=both,
ytick={-5,-4,...,9},
ticklabel style={font=\footnotesize, fill=white,
inner sep=2pt},
no marks,
every axis plot post/.append style={thick},
legend style = {nodes={right, font=\footnotesize},
at={(0.0,0.6)}, anchor=west},
]
\addplot {x+1};
\addplot {x+0.3};
\addplot {x-0.8};
\legend{$y=x+1$, $y=x+0.3$, $y=x-0.8$}
\node at (1,3) [dot,label={[above, lbl dots]:${(1,3)}$}];
\node at (3,4) [dot,label={[above left, lbl dots]:${(3,4)}$}];
\node at (2,1) [dot, label={[right=4pt, lbl dots]:${(2,1)}$}];
\node at (4,3) [dot,label={[right=4pt, lbl dots]:${(4,3)}$}];
\end{axis}
\end{tikzpicture}
\caption{Fitting a regression Line}
\label{fig:enter-label}
\end{figure}
Referring to Fig \ref{fig:enter-label}.
\end{document}
答案2
像这样:
代码:
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz,siunitx}
\begin{document}
\begin{tikzpicture}[scale=1.5]
\draw[gray!20] (0,-.5) grid (5,6);
\draw[thin,-latex] (-.1,0)--(5.6,0) node[right] () {$x$};
\draw[thin,-latex] (0,-1)--(0,6) node[above] () {$y$};
\foreach \i in {1,...,5}
\draw[gray] (\i,.1)--(\i,-.1) node[below] () {\i};
\foreach \i in {-1,0,...,6}
\draw[gray] (.1,\i)--(-.1,\i) node[left] () {\i};
\filldraw[cyan] (1,3) circle(.1);
\filldraw[cyan] (2,1) circle(.1);
\filldraw[cyan] (3,4) circle(.1);
\filldraw[cyan] (4,3) circle(.1);
\draw [magenta, line width=3pt] plot [domain=-.5:5] (\x,{\x-.8});
\draw[magenta] (6,4.2) node () {\large $C:\quad \hat{y}=x-.8$};
\draw [orange, line width=3pt] plot [domain=-.5:5] (\x,{\x+.3});
\draw[orange] (6,5.2) node () {\large $B:\quad \hat{y}=x+.3$};
\draw [green,line width=3pt] plot [domain=-.5:5] (\x,{\x+1}) ;
\draw[green] (6,6) node () {\large $A:\quad \hat{y}=x+1$};
\end{tikzpicture}
\end{document}
选项1:这个情节
使用以下代码:
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
%\usepackage{amssymb}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1.5]
%\draw[gray!20] (0,-.5) grid (5,6);
\draw[thin,-latex] (-.1,0)--(5.6,0) node[right] () {$x$};
\draw[thin,-latex] (0,-1)--(0,6) node[above] () {$y$};
\foreach \i in {1,...,5}
\draw[gray] (\i,.1)--(\i,-.1) node[below] () {\i};
\foreach \i in {-1,0,...,6}
\draw[gray] (.1,\i)--(-.1,\i) node[left] () {\i};
\filldraw[cyan] (1,3) circle(.1);
\filldraw[cyan] (2,1) circle(.1);
\filldraw[cyan] (3,4) circle(.1);
\filldraw[cyan] (4,3) circle(.1);
\draw[magenta,line width=3pt] (-.5,-1.3)--(5,4.2) node[midway, above,sloped] () {\large $C:\quad \hat{y}=x-.8$};
\draw[orange,line width=3pt] (-.5,-.2)--(5,5.3) node[midway, above,sloped] () {\large $B:\quad \hat{y}=x+.3$};
\draw[green!50!black,line width=3pt] (-.5,.5)--(5,6) node[midway, above,sloped] () {\large $A:\quad \hat{y}=x+1$};
\end{tikzpicture}
\end{document}