添加子标题时出现问题

添加子标题时出现问题

如何为条形图添加子标题?

我想要为这三个条形图和标题图 1 添加子标题 (a)、(b)、(c):....

在此处输入图片描述

我的乳胶文件:

\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}

答案1

编辑:

  • 我对你问题的理解与@js bibra 不同...你喜欢有参考吗副标题例如下面每个图表?

无关:

  • 不要使用resizebox,它会使图表中使用的文本与文本中的字体不一致。最好定义图表的宽度(参见下面的 MWE)。
  • \pgfplotsset您可以为此图中的所有图表定义通用的“样式” (参见下面的 MWE)。

在此处输入图片描述

\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}

答案2

摘自手册第 121pgfplots

/pgfplots/title={text}

可以用作标题——放置选项在同一页——http://mirror.iopb.res.in/tex-archive/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf

在此处输入图片描述

因此添加title=caption将为您提供以下标题

在此处输入图片描述

\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}

相关内容