2

I would like to include 2 Tikz into one figure and caption each of the Tikz. I've explored subfigure but that did not seem to work. I'm trying to have the two diagrams appear side-by-side with a caption under each diagram. I appreciate any help rendered.

\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=6cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,295)
        (1,175)
        (2,20)
      };
    \end{axis}
\end{tikzpicture}
%\caption{40-bit primes} \label{40bits}
\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=7cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2,3,4},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,182)
        (1,215)
        (2,75)
        (3,17)
        (4,1)
      };
    \end{axis}
\end{tikzpicture}
%\caption{41-bit primes} \label{41bits}

1 Answers1

4

Here's a possibility that makes use of the \subcaption command of the subcaption package and the minipage environment (see p. 4 of the documentation; there are also dedicated subfigure and subtable environments that are explained on p. 5, if you prefer another possibility).

\documentclass{article}

\usepackage{pgfplots}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{minipage}[b]{.49\linewidth}
\centering
\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=6cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,295)
        (1,175)
        (2,20)
      };
    \end{axis}
\end{tikzpicture}
\subcaption{40-bit primes}\label{40bits}
\end{minipage}
\begin{minipage}[b]{.49\linewidth}
\centering
\begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      height=8cm,
      width=7cm,
      bar width=15pt,
      xlabel={$c$},
      ylabel={Number of primes},
      ymin=0,
      ytick=\empty,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={0,1,2,3,4},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta}
    ]
      \addplot[fill=white] coordinates {
        (0,182)
        (1,215)
        (2,75)
        (3,17)
        (4,1)
      };
    \end{axis}
\end{tikzpicture}
\subcaption{41-bit primes} \label{41bits}
\end{minipage}
\end{figure}

\end{document}

enter image description here


Edit

In response your question in the comment section, you can change the appearance of the \subcaption formatting by renewing \thesubfigure command and by making use of the \captionsetup command.

To achieve the result you want, add

\renewcommand\thesubfigure{Fig~\thefigure(\alph{subfigure})}
\captionsetup[sub]{labelformat=simple}

to your preamble.

enter image description here

Adam Liter
  • 12,567