我有 4 个图需要放在同一页上。有人知道怎么做吗?我需要在每个图下添加标题/标题(因为有数字,所以我不能将其放在图上方),图例可以适用于所有图。
以下是代码:
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
ybar,
ymin=0,ymax=1,
enlarge x limits=0.5,
bar width=10pt,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={percentage, \%},
symbolic x coords={P,R},
xtick=data,
major x tick style = transparent,
ymajorgrids = true,
tick align=outside,
nodes near coords,
every node near coord/.append style={anchor=mid west, rotate=90},
% nodes near coords,
% nodes near coords align={horizontal},
]
\addplot [fill=red!50]coordinates {(P,0.4) (R,0.3) };
\addplot [fill=red!50]coordinates {(P,0.5) (R,0.5)};
legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results P}
\label{fig:3}
\end{figure}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
ybar,
ymin=0,ymax=1,
enlarge x limits=0.5,
bar width=10pt,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={percentage, \%},
symbolic x coords={P,R},
xtick=data,
major x tick style = transparent,
ymajorgrids = true,
tick align=outside,
nodes near coords,
every node near coord/.append style={anchor=mid west, rotate=90},
% nodes near coords,
% nodes near coords align={horizontal},
]
\addplot [fill=green!50]coordinates {(P,0.94) (R,0.5) };
\addplot [fill=violet!50]coordinates {(P,0.6) (R,0.6)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results I}
\label{fig:4}
\end{figure}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
ybar,
ymin=0,ymax=1,
enlarge x limits=0.5,
bar width=10pt,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={percentage, \%},
symbolic x coords={P,R},
xtick=data,
major x tick style = transparent,
ymajorgrids = true,
tick align=outside,
nodes near coords,
every node near coord/.append style={anchor=mid west, rotate=90},
% nodes near coords,
% nodes near coords align={horizontal},
]
\addplot [fill=green!50]coordinates {(P,0.5) (R,0.7) };
\addplot [fill=violet!50]coordinates {(P,0.4) (R,0.9)};
legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results U}
\label{fig:5}
\end{figure}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
ybar,
ymin=0,ymax=1,
enlarge x limits=0.5,
bar width=10pt,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={percentage, \%},
symbolic x coords={P,R},
xtick=data,
major x tick style = transparent,
ymajorgrids = true,
tick align=outside,
nodes near coords,
every node near coord/.append style={anchor=mid west, rotate=90},
% nodes near coords,
% nodes near coords align={horizontal},
]
\addplot [fill=green!50]coordinates {(P,0.1) (R,0.6) };
\addplot [fill=green!50]coordinates {(P,0.2) (R,0.9)};
legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results X}
\label{fig:6}
\end{figure}
我会很感激任何建议!我尝试了\matrix
环境,但无法在每个情节中添加标题。为所有 4 个情节添加一个通用标题也不错。
答案1
这是使用该subcaption
包的一个选项
我还擅自引入了一种名为的风格,mystyle
它可以避免重复太多代码。
% arara: pdflatex
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\pgfplotsset{
every axis/.append style={width=\textwidth},
mystyle/.style={
axis lines*=left,
ybar,
ymin=0,ymax=1,
enlarge x limits=0.5,
bar width=10pt,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={percentage, \%},
symbolic x coords={P,R},
xtick=data,
major x tick style = transparent,
ymajorgrids = true,
tick align=outside,
nodes near coords,
every node near coord/.append style={anchor=mid west, rotate=90},
}
}
\begin{document}
\begin{figure}
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
mystyle
]
\addplot [fill=red!50]coordinates {(P,0.4) (R,0.3) };
\addplot [fill=red!50]coordinates {(P,0.5) (R,0.5)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results P}
\label{fig:3}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
mystyle,
% nodes near coords,
% nodes near coords align={horizontal},
]
\addplot [fill=green!50]coordinates {(P,0.94) (R,0.5) };
\addplot [fill=violet!50]coordinates {(P,0.6) (R,0.6)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results I}
\label{fig:4}
\end{subfigure}
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
mystyle
]
\addplot [fill=green!50]coordinates {(P,0.5) (R,0.7) };
\addplot [fill=violet!50]coordinates {(P,0.4) (R,0.9)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results U}
\label{fig:5}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
mystyle
]
\addplot [fill=green!50]coordinates {(P,0.1) (R,0.6) };
\addplot [fill=green!50]coordinates {(P,0.2) (R,0.9)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results X}
\label{fig:6}
\end{subfigure}
\caption{Global caption goes here}
\end{figure}
\end{document}