Pgfplots – Proportional axis ticks and terminating grid lines at axis

graphspgfplotstikz-pgf

I want to have grid lines in my graph that end at the x and y axis and do not carry over to the sides. If I turn gridlines on it seems like it fills in past the edges. I'd also like to have the graph start at 0 and have intervals that make sense between each tick on the axis.

\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}

enter image description here

Best Answer

I think this is what you want!? -adjust the numbers to your liking:

\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}

Graph with extended axis