1

How can I add subcaption for bar diagrams?

I want to have subcaptions (a), (b) (c) for these three bar diagrams and caption figure1: ....

enter image description here

my latex file:

\documentclass[12pt,a4paper]{article}
 \usepackage{tikz}
  \usepackage{array}
\usepackage[font=footnotesize, labelfont=bf]{caption}
\usepackage[active,floats,tightpage]{preview}
\setlength\PreviewBorder{1em}
\usetikzlibrary{shapes, arrows, positioning, decorations.markings}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{patterns}
\usetikzlibrary{fit, arrows.meta}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{subcaption}
\usepackage{multirow}

\begin{document}
%---------------------------------------------------------------%
\begin{figure}[h]
\centering
\captionof{table}{\label{tab:foldnores18} Test Table}
\begin{tabular}{c|c}
\hline
   & results     \\ \hline
  data          &X          \\  \hline
\end{tabular} 

\vspace*{1 cm}
\resizebox{0.42\linewidth}{!}{
 \begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.35,
legend style={at={(0.5,-0.15)},
  anchor=north,legend columns=-3},
 ybar=7.7pt,
  bar width=14pt,
   symbolic x coords={t1, t2},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
   ]
  \addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };  
  \addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};  
  \legend{test1, test2}
  \end{axis}
   \end{tikzpicture}
 }
 \begin{tabular}{*{2}{>{\centering\arraybackslash}b{\dimexpr0.5\linewidth-2\tabcolsep\relax}}}
 \resizebox{0.9\linewidth}{!}{
  \begin{tikzpicture}
  \begin{axis}[
     ybar,
    enlargelimits=0.35,
   legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-3},
   ybar=7.7pt,
    bar width=14pt,
   symbolic x coords={t1, t2},
   xtick=data,
   nodes near coords,
   nodes near coords align={vertical},
   ]
   \addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };  
   \addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};  
   \legend{test1, test2}
   \end{axis}
   \end{tikzpicture}
    }
    &
    \resizebox{0.9\linewidth}{!}{
    \begin{tikzpicture}
    \begin{axis}[
       ybar,
      enlargelimits=0.35,
      legend style={at={(0.5,-0.15)},
       anchor=north,legend columns=-3},
       ybar=7.7pt,
       bar width=14pt,
       symbolic x coords={t1, t2},
       xtick=data,
       nodes near coords,
       nodes near coords align={vertical},
       ]
     \addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };  
     \addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};  
     \legend{test1, test2}
     \end{axis}
     \end{tikzpicture}
      }
     \end{tabular}
     \captionof{figure}{Test Figures} 
     \end{figure}
     %---------------------------------------------------------------%
     \end{document}

2 Answers 2

0

Edit:

  • I understood your question different than @js bibra ... Do you like to have referable sub captions for example below of each diagram?

Off-topic:

  • Don't use resizebox, it make used text in diagrams inconsistent with fonts in text. Rather define width of diagrams (see MWE below).
  • You can define common "styles" with use of \pgfplotsset for all diagrams in this figure (see MWE below) .

enter image description here

\documentclass[12pt,a4paper]{article}

\usepackage[active,floats,tightpage]{preview}
\setlength\PreviewBorder{1em}

\usepackage{array, multirow}
\usepackage[font=footnotesize, labelfont=bf]{caption}
\usepackage{subcaption}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows, arrows.meta,
                calc,
                decorations.markings, decorations.pathmorphing, decorations.text,
                fit,
                patterns, positioning,
                shapes}


\begin{document}
    \begin{figure}[ht]
    \centering
%%%%
{
\captionof{table}{\label{tab:foldnores18} Test Table}
\begin{tabular}{c|c}
    \hline
        & results   \\ \hline
data    & X         \\  \hline
\end{tabular}
\vskip\baselineskip
}
%%%%

\captionsetup[sub]{skip=0.25ex, belowskip=1ex}
\pgfplotsset{
    width=\linewidth,
    enlargelimits=0.5,
    legend style={legend pos=north west, legend columns=-1},
    ybar=4pt,
    symbolic x coords={t1, t2},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
            }
\begin{subfigure}{0.45\linewidth}
    \begin{tikzpicture}
\begin{axis}[bar width=16pt]
\addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };
\addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};
\legend{test1, test2}
\end{axis}
    \end{tikzpicture}
\caption{}
\label{subfig:fig1a}
\end{subfigure}

\hfill\begin{subfigure}{0.45\linewidth}
    \begin{tikzpicture}
\begin{axis}[bar width=16pt]
\addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };
\addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};
\legend{test1, test2}
\end{axis}
    \end{tikzpicture}
\caption{}
\label{subfig:fig1b}
\end{subfigure}
    \hfil
\begin{subfigure}{0.45\linewidth}
    \begin{tikzpicture}
\begin{axis}[bar width=16pt]
\addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };
\addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};
\legend{test1, test2}
\end{axis}
    \end{tikzpicture}
\caption{}
\end{subfigure}
\label{subfig:fig1c}
\caption{Test Figures}
\label{subfig:fig1}
    \end{figure}
\end{document}
4
  • This is exactly one that I was looking for, Thanks! It works perfectly, the only change that I applied, I comment the line \pgfplotsset{compat=1.17} , it showed error for this line. I removed it and it worked perfectly.
    – Niki
    May 24, 2020 at 12:40
  • I have a problem with this code, I used this code multiple times in my document. The first plotting starts subcaption from (c) instead of (a), the rest of the plottings are correct they start from (a), but the first one has this problem. I run the code separately from the rest of the document it works perfectly but when I add it to the document it shows this error. Any ideas what can cause this problem?
    – Niki
    Jun 15, 2020 at 17:56
  • @Niki, subfigures should be numbered on the same orders as they are inserted in document. Please show us (as new question or by editing this one), a MWE (Minimal Working Example) which reproduce your problem. Without it is imposible to say, what is going wrong in your real document.
    – Zarko
    Jun 15, 2020 at 18:10
  • Thanks, Zarko. I create a new question with ``subcaption problem" title.
    – Niki
    Jun 15, 2020 at 18:37
0

From page 121 of the pgfplots manual

/pgfplots/title={text}

can be used for caption-- placement options are on the same page -- http://mirror.iopb.res.in/tex-archive/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf

enter image description here

So adding title=caption will give you the following caption

enter image description here

\documentclass[12pt,a4paper]{article}
 \usepackage{tikz}
  \usepackage{array}
\usepackage[font=footnotesize, labelfont=bf]{caption}
\usepackage[active,floats,tightpage]{preview}
\setlength\PreviewBorder{1em}
\usetikzlibrary{shapes, arrows, positioning, decorations.markings}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{patterns}
\usetikzlibrary{fit, arrows.meta}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{subcaption}
\usepackage{multirow}

\begin{document}
%---------------------------------------------------------------%
\begin{figure}[h]
\centering
\captionof{table}{\label{tab:foldnores18} Test Table}
\begin{tabular}{c|c}
\hline
   & results     \\ \hline
  data          &X          \\  \hline
\end{tabular} 

\vspace*{1 cm}
\resizebox{0.42\linewidth}{!}{
 \begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.35,
legend style={at={(0.5,-0.15)},
  anchor=north,legend columns=-3},
 ybar=7.7pt,
  bar width=14pt,
   symbolic x coords={t1, t2},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
   ]
  \addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };  
  \addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};  
  \legend{test1, test2}
  \end{axis}
   \end{tikzpicture}
 }
 \begin{tabular}{*{2}{>{\centering\arraybackslash}b{\dimexpr0.5\linewidth-2\tabcolsep\relax}}}
 \resizebox{0.9\linewidth}{!}{
  \begin{tikzpicture}
  \begin{axis}[
     ybar,
    enlargelimits=0.35,
   legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-3},
   ybar=7.7pt,
    bar width=14pt,
   symbolic x coords={t1, t2},
   xtick=data,
   nodes near coords,
   nodes near coords align={vertical}, title=\textbf{caption},
   ]
   \addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };  
   \addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};  
   \legend{test1, test2}
   \end{axis}
   \end{tikzpicture}
    }
    &
    \resizebox{0.9\linewidth}{!}{
    \begin{tikzpicture}
    \begin{axis}[
       ybar,
      enlargelimits=0.35,
      legend style={at={(0.5,-0.15)},
       anchor=north,legend columns=-3},
       ybar=7.7pt,
       bar width=14pt,
       symbolic x coords={t1, t2},
       xtick=data,
       nodes near coords,
       nodes near coords align={vertical},
       ]
     \addplot [pattern = north east lines, pattern color =blue] coordinates {(t1, 2) (t2,3) };  
     \addplot [pattern = north east lines, pattern color =lime] coordinates {(t1,4) (t2,5)};  
     \legend{test1, test2}
     \end{axis}
     \end{tikzpicture}
      }
     \end{tabular}
     \captionof{figure}{Test Figures} 
     \end{figure}
     %---------------------------------------------------------------%
     \end{document}
1
  • That worked. Thanks!
    – Niki
    May 24, 2020 at 1:12

You must log in to answer this question.

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