
問題 1:我試著產生一個長條圖,每個圖表有 3 個長條。但正如您從下面的輸出中看到的,我的 x 軸標籤重複了兩次。 X 軸標籤 = {3-6, 6-12, 12-24}
問題2:當我在背面編譯程式碼時,我得到了完整的中心圖片,當我將程式碼貼到另一個文件時,它佔用了大量空間(因為整個pdf頁面正在複製到另一個文件)。有什麼方法可以讓我在編譯專案時只得到一個僅限於該圖像的 pdf 下載嗎?
問題2輸出
問題1輸出:
乳膠代碼:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgf}
\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{shapes,arrows,automata}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
legend columns=-1,
%bar width=1pt, % Width of the bar
%, % Distance between the centers of the bars
symbolic x coords={3-6, 6-12, 12-24},
enlarge x limits={abs=0.6cm},
width=1.2\textwidth,
%xtick={3,6,12,24},
height=0.8\textheight,
ymin=0,
ymax=15,
nodes near coords,
every node near coord/.append style={font=\scriptsize},
nodes near coords align={vertical},
xlabel={x},
ylabel={y},
% The distance between the center of the first bar and the left edge
%enlarge y limits=false,
legend entries={{\color{olive}{\tiny Random}},{\color{orange}{\tiny ++Cost}},{\color{teal}{\tiny ++FTE}}},
legend to name=CombinedLegendBar,
footnotesize,
ybar legend,
legend image code/.code={%
\draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
},
% area legend, % This is the alternate option
group style={
group size=3 by 1,
xlabels at=edge bottom,
ylabels at=edge left}]
\nextgroupplot[title={\scriptsize MADDPG}]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886)};
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
\addplot[ybar, fill=teal] coordinates { (12-24, 10.179)};
%\addplot[ybar, fill=gray] coordinates { (4, 11.14)};
% \addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\nextgroupplot[title={\scriptsize MATD3}]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886)};
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
\addplot[ybar, fill=teal] coordinates { (12-24, 10.179)};
% \addplot[ybar, fill=gray] coordinates { (4, 11.14)};
% \addplot[ybar, pattern=north east lines] coordinates {(5, 15.001)};
\nextgroupplot[title={\scriptsize MASAC}]
` \addplot[ybar, fill=olive] coordinates { (3-6, 6.886)};
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
\addplot[ybar, fill=teal] coordinates { (12-24, 10.179)};
% \addplot[ybar, fill=gray] coordinates { (4, 11.14)};
% \addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\end{groupplot}
\end{tikzpicture}
\ref{CombinedLegendBar}
\caption{Triage++ Performance}
\label{PlusPlusCombinedBar}
\end{figure}
\end{document}
誰能幫我解決這個問題嗎?
謝謝!
答案1
- 您的問題並不完全清楚,例如,如何將此圖像插入到真實(最終)文件中?
- 對於頁面上的圖形定位,請嘗試使用放置位置選項,即改為
\begin{figure}
寫入\begin{figure}[ht]
或\begin{figure}[!ht]
。 - 在按照下面的 MWE 中的方式清理
groupplot
設定並重新組織圖形程式碼後,本地 MiKTeX 安裝以及 Overleaf 中的編譯會給出相同的結果。 - 清理工作是按照我對您之前的回答的精神進行的問題(無需新增
xtick
選項):
(紅線表示頁面佈局)
\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[ht]
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=3 by 1,
ylabels at=edge left,
horizontal sep=6mm,
},
%
width=0.27\linewidth, %height=4cm,
scale only axis,
title style = {font=\small},
%
enlarge x limits=0.3,
xlabel={x}, ylabel={y},
symbolic x coords={3-6, 6-12, 12-24},
ytick={0,2,...,12},
ticklabel style={font=\footnotesize},
ymin=0, ymax=12,
%
nodes near coords,
every node near coord/.append style={yshift=-3pt, font=\scriptsize},
%
legend entries = {\color{olive}{Random},
\color{orange}{++Cost},
\color{teal}{++FTE}},
legend to name=CombinedLegendBar,
legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);},
legend style={at={(0.5,-0.33)},
below,
legend columns=-1,
font=\footnotesize,
/tikz/every even column/.append style={column sep=1em}
}
]
\nextgroupplot[title=MADDPG]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886) };
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501) };
\addplot[ybar, fill=teal] coordinates { (12-24,10.179) };
\nextgroupplot[title=MATD3]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886) };
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501) };
\addplot[ybar, fill=teal] coordinates { (12-24,10.179) };
\nextgroupplot[title=MASAC]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886) };
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501) };
\addplot[ybar, fill=teal] coordinates { (12-24,10.179) };
\end{groupplot}
\end{tikzpicture}
\ref{CombinedLegendBar}
\caption{Triage++ Performance}
\label{PlusPlusCombinedBar}
\end{figure}
\end{document}
答案2
關於問題 1:添加帶有符號標籤的 xtick 命令(見下文)對我來說很有效。
關於問題2:如果我理解正確,將文章類從文檔替換為獨立對我來說是這樣的(我必須刪除圖形環境+標題)
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgf}
\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{shapes,arrows,automata}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}
\begin{document}
\centering
\begin{tikzpicture}
\begin{groupplot}[
legend columns=-1,
%bar width=1pt, % Width of the bar
%, % Distance between the centers of the bars
symbolic x coords={3-6, 6-12, 12-24},
enlarge x limits={abs=0.6cm},
width=1.2\textwidth,
xtick={3-6, 6-12, 12-24},
height=0.8\textheight,
ymin=0,
ymax=15,
nodes near coords,
every node near coord/.append style={font=\scriptsize},
nodes near coords align={vertical},
xlabel={x},
ylabel={y},
% The distance between the center of the first bar and the left edge
%enlarge y limits=false,
legend entries={{\color{olive}{\tiny Random}},{\color{orange}{\tiny ++Cost}},{\color{teal}{\tiny ++FTE}}},
legend to name=CombinedLegendBar,
footnotesize,
ybar legend,
legend image code/.code={%
\draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
},
% area legend, % This is the alternate option
group style={
group size=3 by 1,
xlabels at=edge bottom,
ylabels at=edge left}]
\nextgroupplot[title={\scriptsize MADDPG}]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886)};
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
\addplot[ybar, fill=teal] coordinates { (12-24, 10.179)};
%\addplot[ybar, fill=gray] coordinates { (4, 11.14)};
% \addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\nextgroupplot[title={\scriptsize MATD3}]
\addplot[ybar, fill=olive] coordinates { (3-6, 6.886)};
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
\addplot[ybar, fill=teal] coordinates { (12-24, 10.179)};
% \addplot[ybar, fill=gray] coordinates { (4, 11.14)};
% \addplot[ybar, pattern=north east lines] coordinates {(5, 15.001)};
\nextgroupplot[title={\scriptsize MASAC}]
` \addplot[ybar, fill=olive] coordinates { (3-6, 6.886)};
\addplot[ybar, fill=orange] coordinates { (6-12, 8.501)};
\addplot[ybar, fill=teal] coordinates { (12-24, 10.179)};
% \addplot[ybar, fill=gray] coordinates { (4, 11.14)};
% \addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\end{groupplot}
\end{tikzpicture}
\end{document}