1

How is it possible to "merge" 2 tikzpictures side by side ? I have tried several solutions provided in related posts using \subfigure but it keeps creating a little white space between figures.

This is my code :

\documentclass{article}

\usepackage{pgfplots} \pgfplotsset{compat=newest}

\usepackage{pgf, tikz, adjustbox} \usepgfplotslibrary{fillbetween} \usetikzlibrary{patterns, matrix, positioning} \usetikzlibrary{arrows.meta, patterns.meta }

\begin{document}

\begin{figure} \begin{tikzpicture}[ declare function={ gauss(\x)=3exp(-(\x/3)^2); }, scale = 0.6] \fill[cyan!20] plot[domain=0:8, samples=100] (\x, {gauss(\x)}) |-(0,0); \filldraw[fill=pink!20, very thick] plot[domain=0:8, samples=100] (\x, {gauss(\x)}) -- plot[domain=8:0, samples=100] (\x, {gauss(\x)+0.6}) -- cycle; \end{tikzpicture} \begin{tikzpicture}[ declare function={ gauss(\x)=3exp(-(\x/3)^2); }, scale = 0.6] \fill[cyan!20] plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) |-(0,0); \filldraw[fill=pink!20, very thick] plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) -- plot[domain=0:-8, samples=100] (\x, {gauss(\x)+0.6}) -- cycle; \end{tikzpicture} \end{figure}

\end{document}

I have also tried the \scope tool as mentioned on this post : Merging plots in TikZ but I think that I don't do it correctly because I keep having a blank space too...

Thank you very much,

enter image description here

Waxler
  • 680

1 Answers1

5

Two tikzpictures side by side

You need to hide the space between \end{tikzpicture} and \begin{tikzpicture} by adding a % otherwise this space will be put between the pictures.

Since TikZ considers the line width when determing the bounding box you will then have a seemingly a double wide line there. I suggest using the trim left and trim right options.

(We can't use (0:8) there but would need to use (0:.6*8) since it doesn't take the scale in consideration which is why I'm using named coordinates along the plot.)

Shifting the second part

Though, you could also just draw both parts in one tikzpicture by shifting the second part about 16 units to the right, i.e. shift={(16,0)} or shift=(0:16).

Since your left side goes from x = 0 to 8 and the right side goes from −8 to 0 you have a total different of 16 units between both x = 0.

Drawing it together

If you want, you can also draw it at once ine one path (without the vertical bar between them).

We still use the shift=(0:16) key so that the plot domain works correctly but this time only for a part of the path by enclosing that part including the transformation in { and }.

The vertical line can simply added by

\draw[very thick] (0:8) -- ++ (up:.6);

Code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
  declare function={gauss(\x)=3*exp(-(\x/3)^2);}, scale = 0.6,
  trim right=(@tr)]
\fill[cyan!20]
  plot[domain=0:8, samples=100] (\x, {gauss(\x)}) coordinate (@tr) -| cycle;
\filldraw[fill=pink!20, very thick]
     plot[domain=0:8, samples=100] (\x, {gauss(\x)})
  -- plot[domain=8:0, samples=100] (\x, {gauss(\x)+0.6}) -- cycle;
\end{tikzpicture}% <- important
\begin{tikzpicture}[
  declare function={gauss(\x)=3*exp(-(\x/3)^2);}, scale = 0.6,
  trim left=(@tl)]
\fill[cyan!20]
  plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) |- cycle
  coordinate[at start] (@tl);
\filldraw[fill=pink!20, very thick]
     plot[domain=-8:0, samples=100] (\x, {gauss(\x)})
  -- plot[domain=0:-8, samples=100] (\x, {gauss(\x)+0.6}) -- cycle;
\end{tikzpicture}

\begin{tikzpicture}[ declare function={gauss(\x)=3*exp(-(\x/3)^2);}, scale = 0.6] \fill[cyan!20] plot[domain=0:8, samples=100] (\x, {gauss(\x)}) -| cycle; \filldraw[fill=pink!20, very thick] plot[domain=0:8, samples=100] (\x, {gauss(\x)}) -- plot[domain=8:0, samples=100] (\x, {gauss(\x)+0.6}) -- cycle; \begin{scope}[shift=(0:16)] \fill[cyan!20] plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) |- cycle; \filldraw[fill=pink!20, very thick] plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) -- plot[domain=0:-8, samples=100] (\x, {gauss(\x)+0.6}) -- cycle; \end{scope} \end{tikzpicture}

\begin{tikzpicture}[ declare function={gauss(\x)=3*exp(-(\x/3)^2);}, scale = 0.6, s/.style={shift=(0:16)}] \fill [cyan!20] plot[domain= 0:8, samples=100] (\x, {gauss(\x)}) {[s] -- plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) |- (-8,0)} -| cycle;

\filldraw[fill=pink!20, very thick] plot[domain=0:8, samples=100] (\x, {gauss(\x)}) {[s] -- plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) -- plot[domain=0:-8, samples=100] (\x, {gauss(\x)+0.6})} -- plot[domain=8:0, samples=100] (\x, {gauss(\x)+0.6}) -- cycle; % \draw[very thick] (0:8) -- ++ (up:.6); \end{tikzpicture} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • An argument can be made for replacing the plots with .. controls … .. if it doesn't need to be precisely a curve following the formula since all the plots add 600 hundreds tiny curves to the document. – Qrrbrbirlbel Oct 03 '22 at 19:25
  • Thank you for your complete answer : I like the last plotting with the 2 gaussian bells drawn in one figure. But I didn't understand the practical difference between this last plot and the one using \shift : in fact the last code use also the \shift option as an argument in tikzpicture. Also another question is related to the "closing of the path" since you impose the part of the curve on the [-8:0] domain to be closed on (8,0) (which is supposed to be non defined on this domain) : plot[domain=-8:0, samples=100] (\x, {gauss(\x)}) |- (-8,0) : how is it possible ? Thank you very much – Waxler Oct 04 '22 at 11:10
  • @Wiss The difference is that in the last approach the whole “tub” is one path and not two. This is probably not that important unless you use shading or a path picture since they apply to a whole path and not just segments of it. It also depends on the vertical divider in the middle. (Though, we can remove that from the other solutions too by reorganizing the plots a bit and removing the -- cycle operation.) – Qrrbrbirlbel Oct 04 '22 at 11:30
  • 1
    @Wiss And you're right, [-8, 0] is technically not on the curve. I made it there easy on my part. (The part is hidden by the thick line anyway.) Though, you can also use (-8, gauss 8). – Qrrbrbirlbel Oct 04 '22 at 11:33
  • Thanks for your explanations ! – Waxler Oct 04 '22 at 12:29