[Tex/LaTex] Size and placement of subfigure of different original size

floatsgraphicssubfloatswidth

I have a figure which has three similar subfigures and one of a different size (showing the legend of the other three). I'm having problems making the size and placing in the document look good.

This is my current code:

\begin{figure}
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[width=\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{./Data_Figures/Coeff_evolution_LS_os.pdf}
\caption{}
\end{subfigure}%
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[width=\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{./Data_Figures/Coeff_evolution_IP_p.pdf}
\caption{}
\end{subfigure}%
\\
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[width=\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{./Data_Figures/Coeff_evolution_LS_p.pdf}
\caption{}
\end{subfigure}%
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[width=\textwidth,trim=5cm 1mm 5cm 5.2cm, clip=true]{./Data_Figures/Coeff_evo_legend.pdf}
\end{subfigure}%
\end{figure}

All the figures are cropped as much as I could. This gives the following result, where the legend is too big:

enter image description here

But then I try e.g. changing the last figure to

\includegraphics[width=0.5\textwidth...]{...}

the spacing is off:

enter image description here

Nothing else I tried worked either. Any ideas?

Best Answer

With use of \sbox{...} you can measures the size of left images and then accommodate your image with legend to its height:

    \documentclass{article}
    \usepackage{subcaption}
    \usepackage{graphicx}

\begin{document}
    \begin{figure}
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[width=0.9\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{example-image-a}
\caption{}
\end{subfigure}%
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[width=0.9\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{example-image-b}
\caption{}
\end{subfigure}

\begin{subfigure}[t]{0.5\textwidth}
\centering
\includegraphics[width=0.9\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{example-image-c}
\caption{}
\end{subfigure}%
\begin{subfigure}[t]{0.5\textwidth}
\centering
\sbox0{\includegraphics[width=0.9\textwidth,trim=3mm 4mm 50mm 2mm, clip=true]{example-image-c}}%
\includegraphics[height=\ht0,keepaspectratio]{example-image}
\end{subfigure}%
    \end{figure}
\end{document}

Which gives (with using example-image from graphicx package):

enter image description here

With your real images estimating from your picture in question, the width of picture with legend is narrowed than other picture, so consequently ti will not protrude outside of text width.

If the aspect ratio of image is not essential, you can limit its width to fraction of available width, for example:

\includegraphics[width=0.8\textwidth, height=\ht0]{example-image}

In this case the picture becomes:

enter image description here

Note: for positioning of images in second row the placement parameter of subfigure is changed from [b] to [t]. This would be good to do also for sub figures in the first row of picture.

Addendum: Another possibility to fit image with legend is to use the following code for the last sub image:

\begin{subfigure}[t]{0.5\textwidth}
\sbox0{\includegraphics[width=0.9\textwidth,%
                        trim=3mm 4mm 55mm 2.5mm,clip]{./Data_Figures/Coeff_evolution_LS_p}}%
\centering
\framebox{\includegraphics[trim=52mm -3.5ex 52mm 53mm,clip,height=\ht0]{./Data_Figures/Coeff_evo_legend}}
\end{subfigure}%

Considering this part of code and after appropriate trimming of your original images, special one with containing legend, my MWE gives:

enter image description here

Frames around images are added that trimming of images is better to observe. Of course, in real use these frames had to be omitted.

Let be noted, that measurement of image heght consider whole image height (with white space around it). Consider this height the legend image become to tall, so to it is need to add a white space below it which simulate text below graph (tick labels, axis label) on the left image. I estimate, that equivalent heght is 3ex which I add to trim values.

Complete code with frames around image is:

    \documentclass{article}
    \usepackage{subcaption}
    \usepackage{calc}
    \usepackage{graphicx}

\begin{document}
    \begin{figure}
\begin{subfigure}[b]{0.5\textwidth}
\centering
\framebox{\includegraphics[width=0.9\textwidth,%
                           trim=3mm 4mm 55mm 2.5mm,clip]{./Data_Figures/Coeff_evolution_LS_os}}
\caption{}
\end{subfigure}%
\begin{subfigure}[b]{0.5\textwidth}
\centering
\framebox{\includegraphics[width=0.9\textwidth,%
                           trim=3mm 4mm 55mm 2.5mm,clip]{./Data_Figures/Coeff_evolution_IP_p}}
\caption{}
\end{subfigure}

\bigskip
\begin{subfigure}[t]{0.5\textwidth}
\centering
\framebox{\includegraphics[width=0.9\textwidth,%
                           trim=3mm 4mm 55mm 2.5mm,clip]{./Data_Figures/Coeff_evolution_LS_p}}
\caption{}
\end{subfigure}%
\begin{subfigure}[t]{0.5\textwidth}
\sbox0{\includegraphics[width=0.9\textwidth,%
                        trim=3mm 4mm 55mm 2.5mm,clip]{./Data_Figures/Coeff_evolution_LS_p}}%
\centering
\includegraphics[trim=52mm -3.5ex 52mm 53mm,clip,height=\ht0]{./Data_Figures/Coeff_evo_legend}
\end{subfigure}
    \end{figure}
\end{document}

From code you can observe, that main your problem was bad trimming of image with legend (you left and top white space in it).