如何使用 \usepackage{pgfplotstable} 从表格中绘图时更改条形图案

如何使用 \usepackage{pgfplotstable} 从表格中绘图时更改条形图案

我想在每个条形中添加不同的图案而不是颜色在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{patterns}

\pgfplotsset{compat=1.11, width=10.5cm, height=7cm,
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},},
}

\begin{document}

\pgfplotstableread[col sep=comma]{

    workload,       EMRSAY,     L-BOUND,    L-MSPAN
    {128M,64R},     112798,     110052,     108772
    {128M,128R},    58183,      63319,      58303
    {256M,64R},     58183,      63319,      58303
    {256M,128R},    58183,      63319,      58303
    {256M,256R},    58183,      63319,      58303
    {512M,128R},    58183,      63319,      58303
    {512M,256R},    58183,      63319,      58303
    {512M,512R},    58183,      63319,      58303

}\datatable

\begin{tikzpicture}

\begin{axis}[
%x tick label style={/pgf/number format/1000 sep=},
ybar,
ymin=0, ymax=150000,
ytick={0,20000,40000,60000,80000,100000,120000},
bar width=0.15cm, %width=1\textwidth,
enlarge x limits=0.15,
enlarge y limits={.45, upper},
legend style={at={(1,1)}, anchor=north east, legend columns=-1},
xlabel={Workload},
ylabel={Total energy consumption (J)},
xticklabels from table={\datatable}{workload},
xtick=data,
x tick label style={rotate=45,anchor=east},
]

\pgfplotsinvokeforeach {1,...,3}{
    \addplot table [x expr=\coordindex, y index=#1] {\datatable};
}
\legend{EMRSAY,OPT,MSPAN}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

您可以为模式添加循环列表。请确保添加列表环境ybar

梅威瑟:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{patterns}

\pgfplotsset{compat=1.11, width=10.5cm, height=7cm,
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},},
}

\begin{document}

\pgfplotstableread[col sep=comma]{

    workload,       EMRSAY,     L-BOUND,    L-MSPAN
    {128M,64R},     112798,     110052,     108772
    {128M,128R},    58183,      63319,      58303
    {256M,64R},     58183,      63319,      58303
    {256M,128R},    58183,      63319,      58303
    {256M,256R},    58183,      63319,      58303
    {512M,128R},    58183,      63319,      58303
    {512M,256R},    58183,      63319,      58303
    {512M,512R},    58183,      63319,      58303

}\datatable

\begin{tikzpicture}

\begin{axis}[
%x tick label style={/pgf/number format/1000 sep=},
ybar,
cycle list={pattern=horizontal lines, pattern=grid, pattern=crosshatch},
ymin=0, ymax=150000,
ytick={0,20000,40000,60000,80000,100000,120000},
bar width=0.15cm, %width=1\textwidth,
enlarge x limits=0.15,
enlarge y limits={.45, upper},
legend style={at={(1,1)}, anchor=north east, legend columns=-1},
xlabel={Workload},
ylabel={Total energy consumption (J)},
xticklabels from table={\datatable}{workload},
xtick=data,
x tick label style={rotate=45,anchor=east},
]

\pgfplotsinvokeforeach {1,...,3}{
    \addplot table [x expr=\coordindex, y index=#1] {\datatable};
}
\legend{EMRSAY,OPT,MSPAN}
\end{axis}
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

相关内容