2

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}

enter image description here

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}

enter image description here

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:

enter image description here

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

3

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}

Output

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.