TransWikia.com

clip `plot` but leaving plot marks intact

TeX - LaTeX Asked by A. Donda on February 11, 2021

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)?

One Answer

As shown here, you need to add clip marker paths=true to the options. Additionally, you may want to use axis on top.

documentclass{standalone}
usepackage{pgfplots}

begin{document}

begin{tikzpicture}
    begin{axis}[
        xmin=0,xmax=5,
        ymin=0,ymax=5,
        axis equal image, axis on top,
        clip marker paths=true, ticks=none]
        addplot+[color=blue,mark=*]
            coordinates {(-2,3) (0,1) (2,3) (4,2) (6,4)};
    end{axis}
end{tikzpicture}

end{document}

MWE

Answered by manooooh on February 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP