我希望图表中的网格线以 x 轴和 y 轴为终点,且不延伸到两侧。如果我打开网格线,它看起来会填满边缘。我还希望图表从 0 开始,并且轴上每个刻度之间的间隔合理。
\documentclass{article}
\usepackage[utf8]{inputenc}
\documentclas{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=center,
grid=both,
scale only axis=true,
ymin=0,
xmin=0,
xtick={0,2,4,6,8,10,12},
legend pos=outer north east,
clip = true,
clip mode=individual,
axis lines=middle,
title=Voltage \& Current,
ylabel=Current,
xlabel=Voltage,
scaled ticks=false,
enlargelimits=0.2,
]
\addplot[opacity=.5,
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,$y=0.00017902x$}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我想这就是你想要的!?-根据你的喜好调整数字:
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
grid=both,
%scale only axis=true,
ymin=0,
xmin=0,
xtick={0,2,4,6,8,10,12},
legend pos=outer north east,
clip mode=individual,
axis lines=middle,
title=Voltage \& Current,
ylabel=Current,
xlabel=Voltage,
scaled ticks=false,
%enlargelimits=0.2,
x axis line style={shorten >=-35pt, shorten <=-35pt},
x label style={xshift=35pt},
y axis line style={shorten >=0pt, shorten <=-35pt},
y label style={yshift=-5pt},
]
\addplot[
opacity=.5,
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,$y=0.00017902x$}
\end{axis}
\end{tikzpicture}
\end{document}