# clip plot but leaving plot marks intact

In this TikZ picture

\begin{tikzpicture}
\draw (0,0) rectangle (5,5);
\draw[color=blue] plot[mark=*] coordinates {(-2,3) (0,1) (2,3) (4,2) (6,4)};
\end{tikzpicture}

I would like to clip the blue plot lines to the black rectangle. Of course I can achieve that by adding the clip option to the first draw command:

\begin{tikzpicture}
\draw[clip] (0,0) rectangle (5,5);
\draw[color=blue] plot[mark=*] coordinates {(-2,3) (0,1) (2,3) (4,2) (6,4)};
\end{tikzpicture}

However, that leads to the plot mark at the edge to be cut in half. Instead, I would like a plot mark to be clipped (not displayed) either completely if the coordinate lies outside of the clip region, or displayed completely if the coordinate lies inside. The result would be like this:

Is it possible to achieve that with some TikZ trickery (i.e. apart from plotting the lines and marks separately)?

You want something like this?:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ticks=none,xmin=0,xmax=5,ymin=0,ymax=5,axis equal image] % This simulates the rectangle with extremes (0,0) and (5,5)
\addplot[color=blue,mark=*] coordinates {(-2,3) (0,1) (2,3) (4,2) (6,4)};
\end{axis}
\end{tikzpicture}

\end{document}

• This transformed the 5cm square into a rectangle. Jul 27 '20 at 5:56
• @AndréC that's correct. We can use axis equal image. Jul 27 '20 at 5:57
• Yes, but I was looking for a pure TikZ solution, without pgfplots. Jul 27 '20 at 5:59
• @A.Donda oh, sorry. I didn't understand. You can wait better answers. Jul 27 '20 at 6:00
• @manooooh, still interesting to know that pgfplots does it this way, thanks! Jul 27 '20 at 6:18