我正在尝试通过 Latex 中水平条形图中的多个条形图来显示不同机器学习模型的性能比较。我尝试了以下代码:
\begin{figure*}[!tbp]
\centering
\pgfplotstableread[col sep=comma,header=false]{
LSTM,60,10,30
GRU,20,55,22
SVM,35,25,40
NB,35,25,40
}\data
\pgfplotstablecreatecol[
create col/expr={
\thisrow{1} + \thisrow{2} + \thisrow{3} + \thisrow{4}
}
]{sum}{\data}
\pgfplotsset{
percentage plot/.style={
point meta=explicit,
every node near coord/.append style={
align=center,
text width=0.5cm
},
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber{\originalvalue}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\pgfplotspointmeta}
\fi},
nodes near coords align=vertical,
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=100,
enlarge y limits={upper,value=0},
visualization depends on={y \as \originalvalue}
},
percentage series/.style={
table/y expr=\thisrow{#1},table/meta=#1
}
}
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=10cm,
ylabel=Responses in Percent,
xlabel=Scenario,
percentage plot,
ybar=0pt,
bar width=0.75cm,
enlarge x limits=0.25,
symbolic x coords={LSTM, GRU, SVM, NB},
xtick=data
]
\addplot table [percentage series=1] {\data};
\addplot table [percentage series=2] {\data};
\addplot table [percentage series=3] {\data}
\addplot table [percentage series=4] {\data};
\legend{Stylometric Feature, Word Embedding Feature, TF-IDF Feature}
\end{axis}
\end{tikzpicture}
\caption{Effect of Epoch in Accuracy and Loss}
\label{fig:bar_chart_performance_compare}
\end{figure*}
但是通过上面的代码,我得到了以下图片:
如何在图表中分割多个条形图?请帮帮我。
答案1
作为起点:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.18}
\pgfplotstableread[col sep=comma]{
X, Y1, Y2, Y3
LSTM, 60, 10, 30
GRU, 20, 55, 22
SVM, 35, 25, 40
NB, 35, 25, 40
}\mydata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
height=75mm, width=100mm,
%
bar width=0.22,
ybar=2pt,
enlarge x limits={abs=0.5},
ymin=0,
ymax=100,
ytick distance=20,
yticklabel style={font=\small},
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ylabel={Responses in Percent},
xlabel={Scenario},
xtick=data,
xticklabels from table={\mydata}{X},
%
nodes near coords,
nodes near coords style={font=\tiny,
/pgf/number format/.cd,
precision=1,
zerofill,
},
legend style={legend pos=north east,
cells={anchor=west},
font=\footnotesize,
}
]
\addplot table [x expr=\coordindex,y=Y1]{\mydata};
\addplot table [x expr=\coordindex,y=Y2]{\mydata};
\addplot table [x expr=\coordindex,y=Y3]{\mydata};
\legend{Stylometric Feature, Word Embedding Feature, TF-IDF Feature} \end{axis}
\end{tikzpicture}
\end{document}
由于我在你的百分比代码中迷失了方向,我省略了你的 MWE 的这一部分,并留给你来添加。