0

I am trying to show a performance comparison of the different machine learning models by multiple bars in a horizontal bar chart in latex. I have tried the following code:

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

But by the above code, I am getting the following picture:

enter image description here

How to searate the multiple bar in the boave chart? Please help me.

6
  • You can play with ybar and bar width
    – user263192
    May 24, 2022 at 11:26
  • how can I do that ? May 24, 2022 at 11:27
  • For example ybar=0.05cm, bar width=0.35cm
    – user263192
    May 24, 2022 at 11:31
  • Can you give the full code as answer? May 24, 2022 at 11:35
  • 1
    When you post a question, please always provide a "Minimal Working Example" (MWE) that starts with \documentclass, includes all relevant \usepackage commands, ends with \end{document} and compiles without errors, even if it does not produce your desired output.
    – Sandy G
    May 24, 2022 at 11:39

1 Answer 1

1

As starting point:

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

Since I got lost in your percentage code, I omitted this part of your MWE, and left it to you to add it.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .