我在图中添加了线性回归,我想在图中调出线性回归以及直线公式。我该怎么做?我知道它非常接近实际数据,但我该怎么做才能让它看起来更好?
谢谢
\documentclass{article}
\usepackage[pass,paperwidth=6.25in,paperheight=9in]{geometry}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{scalerel}
\usepackage{pict2e}
\usepackage{tkz-euclide}
\usetikzlibrary{calc}
\usetikzlibrary{patterns,arrows.meta}
\usetikzlibrary{shadows}
\usetikzlibrary{external}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{fillbetween}
\usepackage{xcolor}
\usepackage{layout}
\usepackage{enumerate}
\pagestyle{fancy}
\rhead{\today}
\lhead{xxx}
\pagenumbering{gobble}
%\addtolength{\textheight}{0pt}
%\addtolength{\paperheight}{-92pt}
%\addtolength{\paperwidth}{-92pt}
\usepackage{circuitikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={at={(0,1)},anchor=north west},
ymin = 0.00071,
ymax = 0.00214,
clip = true,
clip mode=individual,
grid = major,
title=Voltage \& Current,
ylabel=Current,
xlabel=Voltage,
scaled ticks=false,
enlargelimits=0.2,
]
\addplot[opacity=.7,
scatter,
only marks,
point meta=explicit symbolic,
scatter/classes={
a={mark=square*,blue},%
b={mark=triangle*,red},%
c={mark=o,draw=black}},
]
table[meta=label] {
x y label
4.025 0.00071 b
6.042 0.00107 b
7.94 0.00140 b
9.98 0.00177 b
12 0.00214 b
};
\addplot [opacity=.5] table[
y={create col/linear regression={y=Y}}] % compute a linear regression from the input table
{
X Y
4.025 0.000706862
6.042 0.001067945
7.94 0.001407724
9.98 0.001772924
12 0.002134544
};
\legend{Voltage,Current}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
由于您没有多个数据类别,因此不应使用scatter/classes
三个单独的类别。您将为每个类别获得一个“图例图像”,因此前两个图例条目与这两个条目相对应。如您所见,图例中的第一个符号是蓝色正方形,第二个是红色三角形,对应于散点类别a
和b
。
最简单的修改就是删除两个不使用的类,即
scatter/classes={
b={mark=triangle*,red}
}
但是,您最好放弃所有分散的内容,然后只使用only marks, mark=triangle*, red
。
\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics,fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={at={(0,1)},anchor=north west},
ymin = 0.00071,
ymax = 0.00214,
clip = true,
clip mode=individual,
grid = major,
title=Voltage \& Current,
ylabel=Current,
xlabel=Voltage,
scaled ticks=false,
enlargelimits=0.2,
]
\addplot[opacity=.7,
only marks,
mark=triangle*,
red
]
table {
x y
4.025 0.00071
6.042 0.00107
7.94 0.00140
9.98 0.00177
12 0.00214
};
\addplot [opacity=.5] table[
y={create col/linear regression={y=Y}}] % compute a linear regression from the input table
{
X Y
4.025 0.000706862
6.042 0.001067945
7.94 0.001407724
9.98 0.001772924
12 0.002134544
};
\legend{Voltage,Current}
\end{axis}
\end{tikzpicture}
\end{document}