1

I have been trying to reproduce the following example

\documentclass[tikz,margin=3pt]{standalone} \usepackage{tikz,pgfplots} \usetikzlibrary{pgfplots.polar,intersections} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \begin{polaraxis}[samples=50,smooth,thick,axis lines=none] \begin{scope} % Everything inside this scope is clipped % frame \path[clip,draw] plot[domain=44.9:135] (axis cs: \x, {(4/sin(\x))/(1+0.01*(4/sin(\x))^2)}) --plot[domain=135:225] (axis cs: \x, {(-4/cos(\x))/(1+0.01*(-4/cos(\x))^2)}) --plot[domain=225:315] (axis cs: \x, {(-4/sin(\x))/(1+0.01*(-4/sin(\x))^2)}) --plot[domain=-45:45] (axis cs: \x, {(4/cos(\x))/(1+0.01*(4/cos(\x))^2)}) --cycle; % Clipped plot: \addplot[dotted,domain=30:150]{(3/sin(x))/(1+0.01*(3/sin(x))^2)}; \end{scope} % Scope ended, so this is not clipped: \addplot[red,dotted,domain=20:160]{(2/sin(x))/(1+0.01*(2/sin(x))^2)}; \end{polaraxis} \end{tikzpicture} \end{document} 

from this answer. Unfortunately, the region I want to create involves functions that can't be properly plotted using tikz's plotting capability. I can plot them using pgfplots' \addplot, but I have no idea how to extract a path and join them to other points and paths. My overall aim is to plot a contour filled region (also from pgfplot). I would appreciate any direction or help.

Edit: I want to connect the point (-0.66,2) to the path

\addplot[domain=-0.66:0, clip = true] {-2*x*(x^2 + sqrt(1+ x^4))}; 

this path to

\addplot[domain=0:2] {2*x*(-x^2 + sqrt(1+ x^4))}; 

then, this path to (2,2) and cycle to (-0.66,2) and then use this region within a scope to clip a contour filled.

4
  • Welcome to TeX.SX! So, how does the path you wish to use as clipping path look like? Please provide the path as \addplot construct if this is the only way you get it working. Commented Nov 18, 2022 at 0:14
  • Thanks! sure, I want to connect the point (-0.66,2) to the path \addplot[domain=-0.66:0, clip = true] {-2*x*(x^2 + sqrt(1+ x^4))} this path to \addplot[domain=0:2] {2*x*(-x^2 + sqrt(1+ x^4))} then, this path to (2,2) and cycle to (-0.66,2) and then use this region within a scope to clip a contour filled. Commented Nov 18, 2022 at 0:18
  • Maybe add this to your question? Use the Edit link. Commented Nov 18, 2022 at 0:19
  • Added! thanks in advance... maybe this is trivial but Ive spent a couple of hours looking at how to do this before I decided to ask the experts. Commented Nov 18, 2022 at 0:22

1 Answer 1

1

Actually, it is not so much of a problem and TikZ is totally capable of plotting these functions.

There is only one thing you need to take care of: Due to some mysterious mechanisms, you need to wrap \x in parentheses if you want it to take negative values that are to be exponentiated. So, you should write (\x)^2 instead of \x^2 if \x may be negative. But this only holds for the plot functions in TikZ.

You probably don't want polar axes, I guess:

\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \begin{axis}[samples=100, smooth, axis lines=none] \begin{scope} \path[clip] (-0.66,2) -- plot[domain=-0.66:0] (axis cs: \x, {-2*\x*((\x)^2 + sqrt(1+(\x)^4))}) -- plot[domain=0:2] (axis cs: \x, {2*\x*(-\x^2 + sqrt(1+\x^4))}) -- (2,2) -- cycle; \fill[red] (-1,5) rectangle (10,-10); \end{scope} \addplot[domain=-0.66:0] {-2*x*(x^2 + sqrt(1+ x^4))}; \addplot[domain=0:2] {2*x*(-x^2 + sqrt(1+ x^4))}; \end{axis} \end{tikzpicture} \end{document} 

enter image description here


Another way would be to use the fillbetween library. This way, you don't need to plot everything twice.

Since the two paths don't really intersect (only in one point, which can be easily missed by TikZ due to rounding errors), we should use the whole paths in the sequence definition using L* and R* and not intersection segments:

\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=newest} \usepgfplotslibrary{fillbetween} \begin{document} \begin{tikzpicture} \begin{axis}[samples=100, smooth, axis lines=none] \addplot[domain=-0.66:0, name path=A] {-2*x*(x^2 + sqrt(1+ x^4))}; \addplot[domain=0:2, name path=B] {2*x*(-x^2 + sqrt(1+ x^4))}; \begin{scope} \path[clip, intersection segments={of=A and B, sequence={L* -- R*}}] -- (2,2) -- (-0.66,2) -- cycle; \fill[red] (-1,5) rectangle (10,-10); \end{scope} \end{axis} \end{tikzpicture} \end{document} 

The output looks the same as above.

4
  • Thank you for your answer... just as a note for future reference, I just tested and it also works if you replace (\x^2) with pow(\x,2)... such a weird little 'bug'. Just one more question, would this be possible using pgfplots' \addplot? Commented Nov 18, 2022 at 0:49
  • Well, it would be a bit more complicated, but it could probably be done in a similar manner as this example. Commented Nov 18, 2022 at 0:50
  • @NoobNoob Correction: it can be done, as long as the paths you want to combine intersect. See my edit. Commented Nov 18, 2022 at 0:56
  • @NoobNoob Correction 2: The paths only intersect in one point and TikZ has sometimes problems to identify such things due to rounding errors. But we still can use this approach and just define the sequence in such a way that the whole paths are selected. Commented Nov 18, 2022 at 1:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.