来自 CSV 的分组垂直条形图

来自 CSV 的分组垂直条形图

我有以下.csv文件percentages.csv

prefetcher,trace,ipc,ipc-per-diff
gcc,ampm-lite,0.339157,0.164172
gcc,ghb-pc-dc,0.306047,0.050520
gcc,ip-stride,0.308053,0.057406
gcc,next-line,0.284244,-0.024320
gcc,stream,0.276043,-0.052470
GemsFDTD,ampm-lite,3.451324,0.001837
GemsFDTD,ghb-pc-dc,3.447773,0.000806
GemsFDTD,ip-stride,3.448985,0.001158
GemsFDTD,next-line,3.425332,-0.005708
GemsFDTD,stream,3.437907,-0.002057
lbm,ampm-lite,2.035,0.924161
lbm,ghb-pc-dc,1.374755,0.299877
lbm,ip-stride,1.529501,0.446194
lbm,next-line,1.600095,0.512943
lbm,stream,1.259173,0.190590
leslie3d,ampm-lite,1.623532,0.647876
leslie3d,ghb-pc-dc,1.295898,0.315329
leslie3d,ip-stride,1.407264,0.428365
leslie3d,next-line,1.448414,0.470132
leslie3d,stream,1.033456,0.048952
libquantum,ampm-lite,3.274124,0.040013
libquantum,ghb-pc-dc,3.276536,0.040779
libquantum,ip-stride,3.242776,0.030055
libquantum,next-line,3.293444,0.046150
libquantum,stream,3.172061,0.007593
mcf,ampm-lite,0.372714,0.082426
mcf,ghb-pc-dc,0.349371,0.014634
mcf,ip-stride,0.353506,0.026643
mcf,next-line,0.35061,0.018232
mcf,stream,0.342385,-0.005654
milc,ampm-lite,1.179511,0.202991
milc,ghb-pc-dc,1.117595,0.139842
milc,ip-stride,1.136986,0.159619
milc,next-line,1.212129,0.236258
milc,stream,1.068124,0.089387
omnetpp,ampm-lite,2.269448,0.035387
omnetpp,ghb-pc-dc,2.100247,-0.041807
omnetpp,ip-stride,2.220496,0.013054
omnetpp,next-line,1.85322,-0.154508
omnetpp,stream,2.214889,0.010496

我希望第一列为prefetcher组,第二列为trace单独的垂直条之内每个组(并显示为图例),以及第四列,ipc-per-diff为每个条的高度(正数或负数)。

理想情况下,我还想将 x 轴放在图表的中间,但将轴标签保留在底部。

我的第一次尝试:

\begin{tikzpicture}
    \pgfplotstableread[col sep=comma,]{../percentages.csv}\datatable
    \begin{axis}[
        ybar,
        symbolic x coords={gcc,GemsFDTD,lbm,leslie3d,libquantum,mcf,milc,omnetpp},
        enlarge x limits=0.2,
        bar width=0.2cm,
        legend style={draw=none,column sep=0.5cm},
        legend cell align=right,
    ]
        \foreach \i in {AMPM-Lite,GHB PC/DC,IP Stride,Next Line, Stream} {
            \addplot+ [] table [
                col sep=comma,
                x=prefetcher,
                y=ipc-per-diff,
            ]{../percentages.csv};
            \addlegendentryexpanded{\i}
        }
    \end{axis}
\end{tikzpicture}

我最终得到如下结论: 在此处输入图片描述

显然不太理想,因为看起来条形图堆叠在一起,不用说,图表太拥挤了。我该如何解决这个问题?或者我可以重新构建.csv数据文件以更轻松地绘制它吗?

答案1

这应该是你想要的:

\documentclass{article}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\section{Exmaple}
\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    width=\textwidth,
    xlabel={ipc},ylabel={ipc-per-diff},
    symbolic x coords={gcc,GemsFDTD,lbm,leslie3d,libquantum,mcf,milc,omnetpp},
    xtick=data,
    ybar,bar width=5pt,
    legend style={at={(1.0,1.03)},anchor=south east},legend columns=5,
    table/col sep=comma
    ]
    \addplot[draw=none,fill=red] table [x=x,y=y]{ampm-lite.csv};
    \addlegendentry{ampm-lite}
    \addplot[draw=none,fill=blue] table [x=x,y=y]{ghb-pc-dc.csv};
    \addlegendentry{ghb-pc-dc}
    \addplot[draw=none,fill=brown] table [x=x,y=y]{ip-stride.csv};
    \addlegendentry{ip-stride}
    \addplot[draw=none,fill=gray] table [x=x,y=y]{next-line.csv};
    \addlegendentry{next-line}
    \addplot[draw=none,fill=purple] table [x=x,y=y]{stream.csv};
    \addlegendentry{stream}
    \end{axis}
    \end{tikzpicture}
    \caption{Example Bar Plot}
    \label{fig:label1}
\end{figure}
\end{document}
%ampm-lite.csv
x,y
gcc,0.164172
GemsFDTD,0.001837
lbm,0.924161
leslie3d,0.647876
libquantum,0.040013
mcf,0.082426
milc,0.202991
omnetpp,0.035387
%ghb-pc-dc.csv
x,y
gcc,0.050520
GemsFDTD,0.000806
lbm,0.299877
leslie3d,0.315329
libquantum,0.040779
mcf,0.014634
milc,0.139842
omnetpp,-0.041807
%ip-stride.csv
x,y
gcc,0.057406
GemsFDTD,0.001158
lbm,0.446194
leslie3d,0.428365
libquantum,0.030055
mcf,0.026643
milc,0.159619
omnetpp,0.013054
%next-line.csv
x,y
gcc,-0.024320
GemsFDTD,-0.005708
lbm,0.512943
leslie3d,0.470132
libquantum,0.046150
mcf,0.018232
milc,0.236258
omnetpp,-0.154508
%stream.csv
x,y
gcc,-0.052470
GemsFDTD,-0.002057
lbm,0.190590
leslie3d,0.048952
libquantum,0.007593
mcf,-0.005654
milc,0.089387
omnetpp,0.010496

在此处输入图片描述

相关内容