0

I have these two pictures side-by-side, but they are weirdly aligned for some reason. I would like to align the one on the left to the left margin and the right on to the right margin... How can I fix this?

How it looks

\begin{figure}[h]
\begin{minipage}[b]{0.5\textwidth}
  \centering
 \begin{tikzpicture}
 \centering
\begin{axis}[
    height = 3 cm,
    width = 7 cm,
    axis lines = left,
    xtick pos=left,
    hide y axis,
    xlabel={$X$},
    xmin=0, xmax=105,
    ymin=-3, ymax=3,
    enlarge y limits = false,
    xtick={0,20,40,60,80,100},
    xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$}
]
\addplot[
    only marks,
    color=black,
    mark=*,
    ]
    coordinates {
    (0, 0)(20, 0)(40, 0)(60, 0)(80, 0)(100, 0) 
    };
\addplot +[mark=none, color=gray] coordinates {(20, 0) (40, 0)};
\addplot +[mark=none, color=black] coordinates {(0, -1) (0, 1)(100, 1) (100, -1) (0, -1)};
\node[] at (axis cs: 30,2) {$\Delta X$};
\end{axis}
\end{tikzpicture}
    \caption{Mesh for a 1D problem}
    \label{fig:fdm_1d_mesh}
\end{minipage}
\hfill
\begin{minipage}[b]{0.5\textwidth}
  \centering
 \begin{tikzpicture}
 \centering
\begin{axis}[
    axis lines = left,
    xtick pos=left,
    ytick pos=left,
    xlabel={$X$},
    ylabel={$Y$},
    xmin=0, xmax=105,
    ymin=0, ymax=130,
    xtick={0,20,40,60,80,100},
    xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$},
    ytick={0,20,40,60,80,100,120},
    yticklabels={$Y_{0}$, $Y_{1}$, , , , $Y_{M-1}$, $Y_{M}$},
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
]
\addplot[
    only marks,
    color=black,
    mark=*,
    ]
    coordinates {
    (0, 0)(0, 20)(0,40)(0,60)(0,80)(0,100)(0,120)
    
    (20, 0)(20, 20)(20,40)(20,60)(20,80)(20,100)(20,120)
    
    (40, 0)(40, 20)(40,40)(40,60)(40,80)(40,100)(40,120)
    
    (60, 0)(60, 20)(60,40)(60,60)(60,80)(60,100)(60,120)
    
    (80, 0)(80, 20)(80,40)(80,60)(80,80)(80,100)(80,120)
    
    (100, 0)(100, 20)(100,40)(100,60)(100,80)(100,100)(100,120)
    
    };
\addplot +[mark=none, color=gray] coordinates {(20, 60) (20, 80)};
\node[] at (axis cs: 14,70) {$\Delta Y$};
\addplot +[mark=none, color=gray] coordinates {(20, 60) (40, 60)};
\node[] at (axis cs: 30,54) {$\Delta X$};
\end{axis}
\end{tikzpicture}
    \caption{Mesh for a 2D problem}
    \label{fig:fdm_2d_mesh}
\end{minipage}
\end{figure}
2
  • 1
    My guess would be that they're too big (i.e. more than 0.5\textwidth) and the indent on the first one is pushing the second one off the edge on the other side. The \centering is also probably not helping.
    – Plergux
    Mar 24, 2021 at 11:46
  • Pease make your code fragment compilable by adding the documentclass as well as the relevant packages. Do you get any error messages of warnings upon compiling your code?
    – leandriis
    Mar 24, 2021 at 12:06

2 Answers 2

1

The right tikzpicture is to wide to fit in 0.5\textwidth and overflows into the margin.

The minimum change to your code is to scale (both figures, to keep same font size and distances) using \begin{tikzpicture}[scale=0.8].

Lines marked with % <<<<<< changed

f

\documentclass[12pt,a4paper]{article}

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

\usepackage{showframe} % show margins

\begin{document}
    
\begin{figure}[h]
    \begin{minipage}[b]{0.5\textwidth}
        \centering
        \begin{tikzpicture}
        \centering
        \begin{axis}[
        height = 3 cm,
        width = 7 cm,
        axis lines = left,
        xtick pos=left,
        hide y axis,
        xlabel={$X$},
        xmin=0, xmax=105,
        ymin=-3, ymax=3,
        enlarge y limits = false,
        xtick={0,20,40,60,80,100},
        xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$}
        ]
        \addplot[
        only marks,
        color=black,
        mark=*,
        ]
        coordinates {
            (0, 0)(20, 0)(40, 0)(60, 0)(80, 0)(100, 0) 
        };
        \addplot +[mark=none, color=gray] coordinates {(20, 0) (40, 0)};
        \addplot +[mark=none, color=black] coordinates {(0, -1) (0, 1)(100, 1) (100, -1) (0, -1)};
        \node[] at (axis cs: 30,2) {$\Delta X$};
        \end{axis}
        \end{tikzpicture}
        \caption{Mesh for a 1D problem}
        \label{fig:fdm_1d_mesh}
    \end{minipage}
    \hfill
    \begin{minipage}[b]{0.5\textwidth}
        \centering
        \begin{tikzpicture}
        \centering
        \begin{axis}[
        axis lines = left,
        xtick pos=left,
        ytick pos=left,
        xlabel={$X$},
        ylabel={$Y$},
        xmin=0, xmax=105,
        ymin=0, ymax=130,
        xtick={0,20,40,60,80,100},
        xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$},
        ytick={0,20,40,60,80,100,120},
        yticklabels={$Y_{0}$, $Y_{1}$, , , , $Y_{M-1}$, $Y_{M}$},
        ymajorgrids=true,
        xmajorgrids=true,
        grid style=dashed,
        ]
        \addplot[
        only marks,
        color=black,
        mark=*,
        ]
        coordinates {
            (0, 0)(0, 20)(0,40)(0,60)(0,80)(0,100)(0,120)
            
            (20, 0)(20, 20)(20,40)(20,60)(20,80)(20,100)(20,120)
            
            (40, 0)(40, 20)(40,40)(40,60)(40,80)(40,100)(40,120)
            
            (60, 0)(60, 20)(60,40)(60,60)(60,80)(60,100)(60,120)
            
            (80, 0)(80, 20)(80,40)(80,60)(80,80)(80,100)(80,120)
            
            (100, 0)(100, 20)(100,40)(100,60)(100,80)(100,100)(100,120)
            
        };
        \addplot +[mark=none, color=gray] coordinates {(20, 60) (20, 80)};
        \node[] at (axis cs: 14,70) {$\Delta Y$};
        \addplot +[mark=none, color=gray] coordinates {(20, 60) (40, 60)};
        \node[] at (axis cs: 30,54) {$\Delta X$};
        \end{axis}
        \end{tikzpicture}
        \caption{Mesh for a 2D problem}
        \label{fig:fdm_2d_mesh}
    \end{minipage}  
\end{figure}
    
\medskip
\textbf{scaled = 0.8}
\medskip

\begin{figure}[h]
    \begin{minipage}[b]{0.5\textwidth}
        \centering
        \begin{tikzpicture}[scale=0.8] % <<<<<< changed
        \centering
        \begin{axis}[
        height = 3 cm,
        width = 7 cm,
        axis lines = left,
        xtick pos=left,
        hide y axis,
        xlabel={$X$},
        xmin=0, xmax=105,
        ymin=-3, ymax=3,
        enlarge y limits = false,
        xtick={0,20,40,60,80,100},
        xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$}
        ]
        \addplot[
        only marks,
        color=black,
        mark=*,
        ]
        coordinates {
            (0, 0)(20, 0)(40, 0)(60, 0)(80, 0)(100, 0) 
        };
        \addplot +[mark=none, color=gray] coordinates {(20, 0) (40, 0)};
        \addplot +[mark=none, color=black] coordinates {(0, -1) (0, 1)(100, 1) (100, -1) (0, -1)};
        \node[] at (axis cs: 30,2) {$\Delta X$};
        \end{axis}
        \end{tikzpicture}
        \caption{Mesh for a 1D problem}
        \label{fig:fdm_1d_meshi}
    \end{minipage}
    \hfill
    \begin{minipage}[b]{0.5\textwidth}
        \centering
        \begin{tikzpicture}[scale=0.8] % <<<<<< changed
        \centering
        \begin{axis}[
        axis lines = left,
        xtick pos=left,
        ytick pos=left,
        xlabel={$X$},
        ylabel={$Y$},
        xmin=0, xmax=105,
        ymin=0, ymax=130,
        xtick={0,20,40,60,80,100},
        xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$},
        ytick={0,20,40,60,80,100,120},
        yticklabels={$Y_{0}$, $Y_{1}$, , , , $Y_{M-1}$, $Y_{M}$},
        ymajorgrids=true,
        xmajorgrids=true,
        grid style=dashed,
        ]
        \addplot[
        only marks,
        color=black,
        mark=*,
        ]
        coordinates {
            (0, 0)(0, 20)(0,40)(0,60)(0,80)(0,100)(0,120)
            
            (20, 0)(20, 20)(20,40)(20,60)(20,80)(20,100)(20,120)
            
            (40, 0)(40, 20)(40,40)(40,60)(40,80)(40,100)(40,120)
            
            (60, 0)(60, 20)(60,40)(60,60)(60,80)(60,100)(60,120)
            
            (80, 0)(80, 20)(80,40)(80,60)(80,80)(80,100)(80,120)
            
            (100, 0)(100, 20)(100,40)(100,60)(100,80)(100,100)(100,120)
            
        };
        \addplot +[mark=none, color=gray] coordinates {(20, 60) (20, 80)};
        \node[] at (axis cs: 14,70) {$\Delta Y$};
        \addplot +[mark=none, color=gray] coordinates {(20, 60) (40, 60)};
        \node[] at (axis cs: 30,54) {$\Delta X$};
        \end{axis}
        \end{tikzpicture}
        \caption{Mesh for a 2D problem}
        \label{fig:fdm_2d_meshi}
    \end{minipage}
\end{figure}

    
\end{document}
1

It may solve your problem:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{figure}[ht]
\pgfplotsset{width=0.98\linewidth,
             axis lines=left,
             xlabel=$X$,
             xtick={0,20,...,100},
             xticklabels={$X_{0}$, $X_{1}$, , , $X_{L-1}$, $X_{L}$},
             enlarge y limits = false
             }
\begin{minipage}[b]{0.46\textwidth}
\centering
    \begin{tikzpicture}
\begin{axis}[
    height = 3 cm,
    hide y axis,
    xmin=0, xmax=105,
    ymin=-3, ymax=3,    
]
\addplot[only marks]
    coordinates {
    (0, 0)(20, 0)(40, 0)(60, 0)(80, 0)(100, 0)
    };
\addplot +[mark=none, color=gray] coordinates {(20, 0) (40, 0)}
    node[midway, above, black]  {$\Delta X$};
\addplot +[mark=none, color=black] coordinates {(0, -1) (0, 1)(100, 1) (100, -1) (0, -1)};

\end{axis}
\end{tikzpicture}
    \caption{Mesh for a 1D problem}
    \label{fig:fdm_1d_mesh}
\end{minipage}%
%
\begin{minipage}[b]{0.54\textwidth}
\centering
    \begin{tikzpicture}
\begin{axis}[
    ylabel={$Y$},
    xmin=0, xmax=105,
    ymin=0, ymax=130,
    ytick={0,20,...,120},
    yticklabels={$Y_{0}$, $Y_{1}$, , , , $Y_{M-1}$, $Y_{M}$},
    grid,
    grid style=dashed,
]
\addplot[only marks]
    coordinates {
    (0, 0)(0, 20)(0,40)(0,60)(0,80)(0,100)(0,120)
    (20, 0)(20, 20)(20,40)(20,60)(20,80)(20,100)(20,120)
    (40, 0)(40, 20)(40,40)(40,60)(40,80)(40,100)(40,120)
    (60, 0)(60, 20)(60,40)(60,60)(60,80)(60,100)(60,120)
    (80, 0)(80, 20)(80,40)(80,60)(80,80)(80,100)(80,120)
    (100, 0)(100, 20)(100,40)(100,60)(100,80)(100,100)(100,120)
    };
\addplot +[mark=none, color=gray] coordinates {(20, 60) (20, 80)} 
        node[midway, left,black]    {$\Delta Y$};
\addplot +[mark=none, color=gray] coordinates {(20, 60) (40, 60)}
        node[midway,below,black]    {$\Delta X$};
\end{axis}
\end{tikzpicture}
    \caption{Mesh for a 2D problem}
    \label{fig:fdm_2d_mesh}
\end{minipage}
    \end{figure}
\end{document}

enter image description here

(red lined show text border)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .