[Tex/LaTex] How to fill a specified area using TikZ

path-clippingtikz-pgf

I want to fill some regions which are only parts of a shape in a TikZ drawing (marked as "Here" in the following drawings):

\documentclass{standalone}
\usepackage[tikz]{bclogo}
\usetikzlibrary{arrows,shapes,backgrounds,calc,arrows}
\begin{document}
    \begin{tikzpicture}[axis/.style={very thick, ->, >=stealth'}]
    % axis
        \draw[axis] (9,2.5)  -- (13.5,2.5) node(xline)[right]
            {$x_N$};
        \draw[axis] (11.25,0.5) -- (11.25,4.5) node(yline)[above] {$\hat{x}_N$};
         
    \draw[->, ultra thick](7,2.5) -- (8.5,2.5);    
    \draw [very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) };
    %\draw[very thick] (0.5,3) to [ curve through ={(5.5,2) . . ( 3,0.25) . . (1,0.25) }]( 0.5,3 ) ;
    
    \draw[dashed, very thick] (11.25,2.5) ellipse (1cm and 1.5cm);
    \draw[dashed, very thick] (5,3) ellipse (1.5cm and 1cm);
    \draw[step=0.25cm,gray,very thin] (0,0) grid (15,5);
    \node (a) at (4,3) {Here};
    \node (b) at (11.7,3) {Here};
    \node (a) at (11.7,2) {Here};
    
    \end{tikzpicture}
\end{document}

enter image description here

How can I achieve that?

Best Answer

Is this what you seek? Here scope environment is used. Be aware that clip command must be contained in the scope environment to limit the effect of clipping. Without scope environment, the clipping effect will continue to the end of the code.

enter image description here

Code

\documentclass{standalone}
\usepackage[tikz]{bclogo}
\usetikzlibrary{arrows,shapes,backgrounds,calc,arrows,patterns}
\begin{document}
    \begin{tikzpicture}[axis/.style={very thick, ->, >=stealth'}]
    % axis
        \draw[axis] (9,2.5)  -- (13.5,2.5) node(xline)[right]
            {$x_N$};
        \draw[axis] (11.25,0.5) -- (11.25,4.5) node(yline)[above] {$\hat{x}_N$};
    \draw[->, ultra thick](7,2.5) -- (8.5,2.5);  
\begin{scope}
\draw [very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) }; 
\clip (5,3) ellipse (1.5cm and 1cm);
 \draw [fill=red,very thick] plot [smooth cycle] coordinates {(2,4) (5,3) (2,0.5) (1,1.75) (1,4) };
\end{scope}
\begin{scope}
\draw [dashed,very thick] (11.25,2.5) ellipse (1cm and 1.5cm);
\clip (11.25,2.5) ellipse (1cm and 1.5cm);
\fill [red] (11.25,0) rectangle (15,5);
\end{scope}
    \draw[dashed, very thick] (5,3) ellipse (1.5cm and 1cm);
    \draw[step=0.25cm,gray,very thin] (0,0) grid (15,5);
    \node (a) at (4,3) {Here};
    \node (b) at (11.7,3) {Here};
    \node (a) at (11.7,2) {Here};
    \end{tikzpicture}
\end{document}

Edit: With patterns from tikzlibrary, and put pattern=crosshatch dots in the fill/draw options, one gets

enter image description here

Related Question