分组条形图

分组条形图

我想使用 minipage 将所有 4 个图表放在一页(一个面板)上。另外,我是否可以将图表垂直放置而不是水平放置?谢谢。

\documentclass{article}
\usepackage{pgfplots,float,amsmath,caption}
\begin{document}
\begin{figure}[H]
\centering
\resizebox{13cm}{!}{%
\begin{tikzpicture}%[font=\normalsize]
\begin{axis}
[xbar,width=22cm,height=12cm,bar width=20pt,enlargelimits=0.20,
nodes near coords,
nodes near coords align=horizontal, 
%xmajorgrids = true,
point meta=x * 1, % The displayed number.
xlabel={\textbf{Percentage of Cost}},
tick align=outside,
xtick={0,5,...,100}, 
ytick={1,...,5},
yticklabels={Penalty Cost, Fixed Cost, Operating and Maintenance Cost, Purchase Cost}]
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=red,fill=red!50]
coordinates {(0,1) (4.1,2) (16.8,3) (54.4,4)};
\addlegendentry{\text{MRI Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=blue, fill=blue!50] 
coordinates {(14.5,1) (4.6,2) (1.3,3) (4.3,4)};
\addlegendentry{\text{CT Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\end{axis}
\end{tikzpicture}
}
\captionsetup{font={footnotesize}}
\caption{Cost Analysis for No Technological Change and No Deterioration} \label{Fig: 3}
\end{figure}
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\begin{figure}[H]
\centering
\resizebox{13cm}{!}{%
\begin{tikzpicture}%[font=\normalsize]
\begin{axis}
[xbar,width=22cm,height=12cm,bar width=20pt,enlargelimits=0.20,
nodes near coords,
nodes near coords align=horizontal,
point meta=x * 1, % The displayed number.
xlabel={\textbf{Percentage of Cost}},
tick align=outside,
xtick={0,5,...,100}, 
ytick={1,...,5},
yticklabels={Penalty Cost, Fixed Cost, Operating and Maintenance Cost, Purchase Cost}]
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=red,fill=red!50]
coordinates {(26.9,1) (3.5,2) (9.0,3) (34.4,4)};
\addlegendentry{\text{MRI Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=blue, fill=blue!50] 
coordinates {(18.6,1) (3.3,2) (0.9,3) (3.4,4)};
\addlegendentry{\text{CT Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\end{axis}
\end{tikzpicture}
}
\captionsetup{font={footnotesize}}
\caption{Cost Analysis for 10\%~Deterioration and No Technological Change} \label{Fig: 4}
\end{figure}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

\begin{figure}[H]
\centering
\resizebox{13cm}{!}{%
\begin{tikzpicture}%[font=\normalsize]
\begin{axis}
[xbar,width=22cm,height=12cm,bar width=20pt,enlargelimits=0.20,
nodes near coords,
nodes near coords align=horizontal,
point meta=x * 1, % The displayed number.
xlabel={\textbf{Percentage of Cost}},
tick align=outside,
xtick={0,5,...,100}, 
ytick={1,...,5},
yticklabels={Penalty Cost, Fixed Cost, Operating and Maintenance Cost, Purchase Cost}]
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=red,fill=red!50]
coordinates {(0.0,1) (11.5,2) (22.3,3) (60.8,4)};
\addlegendentry{\text{MRI Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=blue, fill=blue!50] 
coordinates {(0.0,1) (1.2,2) (2.3,3) (1.9,4)};
\addlegendentry{\text{CT Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\end{axis}
\end{tikzpicture}
}
\captionsetup{font={footnotesize}}
\caption{Cost Analysis for 10\%~Technological Change and 10\%~Deterioration} \label{Fig: 5}
\end{figure}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\begin{figure}[H]
\centering
\resizebox{13cm}{!}{%
\begin{tikzpicture}%[font=\normalsize]
\begin{axis}
[xbar,width=22cm,height=12cm,bar width=20pt,enlargelimits=0.20,
nodes near coords,
nodes near coords align=horizontal,
point meta=x * 1, % The displayed number.
xlabel={\textbf{Percentage of Cost}},
tick align=outside,
xtick={0,5,...,100}, 
ytick={1,...,5},
yticklabels={Penalty Cost, Fixed Cost, Operating and Maintenance Cost, Purchase Cost}]
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=red,fill=red!50]
coordinates {(0.0,1) (9.8,2) (31.6,3) (49.6,4)};
\addlegendentry{\text{MRI Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\addplot[draw=blue, fill=blue!50] 
coordinates {(0.0,1) (2.6,2) (1.8,3) (4.6,4)};
\addlegendentry{\text{CT Scanning Machine}}

%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\end{axis}
\end{tikzpicture}
}
\captionsetup{font={footnotesize}}
\caption{Cost Analysis for 10\%~Technological Change and Deterioration} \label{Fig: 6}
\end{figure}
\end{document}

答案1

您可以缩小每个轴的尺寸,并将其与 一起放置\caption在 中minipage,然后将所有四个 放在minipage同一个 中figure。但由于所有轴标签可能会变得有点拥挤,所以我建议另一种方法,使用groupplots

要使条形图垂直,您需要从 切换xbarybar,并将所有 切换x<things>y<things>,反之亦然。例如,xtick切换到ytick。坐标的顺序也必须改变,例如从coordinates {(0,1) (4.1,2) (16.8,3) (54.4,4)}切换到 coordinates {(1,0) (2,4.1) (3,16.8) (4,54.4)};

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{groupplot}[
   group style={
       group size=2 by 2,
       x descriptions at=edge bottom,
       y descriptions at=edge left,
       vertical sep=0pt,
       horizontal sep=0pt},
  ybar,
  width=.55\linewidth,
  height=.4\textheight,
  every node near coord/.append style={font=\footnotesize},
  nodes near coords,
  nodes near coords align=vertical,
  enlarge x limits=0.2,
  %xmajorgrids = true,
  point meta=y * 1, % The displayed number.
  ylabel={\textbf{Percentage of Cost}},
  tick align=outside,
  ytick={0,10,...,100}, 
  xtick={1,...,5},
  xticklabels={
    Penalty Cost,
    Fixed Cost,
    Operating and\\Maintenance Cost,
    Purchase Cost},
  x tick label style={align=left,rotate=90},
  ymin=-5,
  ymax=75
]

\nextgroupplot[bar width=12pt]
\addplot[draw=red,fill=red!50]
coordinates {(1,0) (2,4.1) (3,16.8) (4,54.4)};
\addplot[draw=blue, fill=blue!50] 
coordinates {(1,14.5) (2,4.6) (3,1.3) (4,4.3)};

\node at (rel axis cs:0.1,0.9) {a)};

\nextgroupplot[bar width=12pt,legend style={at={(0,1)},anchor=north}]
\addplot[draw=red,fill=red!50]
coordinates {(1,26.9) (2,3.5) (3,9.0) (4,34.4)};
\addlegendentry{MRI Scanning Machine}
\addplot[draw=blue, fill=blue!50] 
coordinates {(1,18.6) (2,3.3) (3,0.9) (4,3.4)};
\addlegendentry{CT Scanning Machine}

\node at (rel axis cs:0.9,0.9) {b)};

\nextgroupplot[bar width=12pt]
\addplot[draw=red,fill=red!50]
coordinates {(1,0.0) (2,11.5) (3,22.3) (4,60.8)};
\addplot[draw=blue, fill=blue!50] 
coordinates {(1,0.0) (2,1.2) (3,2.3) (4,1.9)};

\node at (rel axis cs:0.1,0.9) {c)};

\nextgroupplot[bar width=12pt]
\addplot[draw=red,fill=red!50]
coordinates {(1,0.0) (2,9.8) (3,31.6) (4,49.6)};
\addplot[draw=blue, fill=blue!50] 
coordinates {(1,0.0) (2,2.6) (3,1.8) (4,4.6)};

\node at (rel axis cs:0.9,0.9) {d)};

\end{groupplot}
\end{tikzpicture}
\caption{Cost Analysis for a) No Technological Change and No Deterioration; b) 10\%~Deterioration and No Technological Change; c) 10\%~Technological Change and 10\%~Deterioration; d) 10\%~Technological Change and Deterioration}
\end{figure}
\end{document}

相关内容