pgfplots 中的分组条形颜色

pgfplots 中的分组条形颜色

我有一个简单的分组条形图,但希望所有组的条形都具有相同的颜色。目前,组中的每个条形都是彩色的,可能是因为我使用 addplot 的方式。我尝试使用 pgfplotsset 列表,但这仍然会改变每个条形的颜色,而不是组。我想要的是所有黑色的条形都变成单一颜色,所有棕色的条形都变成单一颜色,等等。欢迎提出任何建议!

我现在拥有的

![\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pagestyle{empty}
\begin{document}
\pgfplotsset{
/pgfplots/bar cycle list/.style={/pgfplots/cycle list={%
{blue,fill=blue,mark=none},%
{red,fill=red,mark=none},%
{brown!60!black,fill=brown!30!white,mark=none},%
{black,fill=gray,mark=none},
{fill=orange},
{fill=green},
{fill=olive},%
}
},
}
\begin{tikzpicture}
\begin{axis}\[
    ybar=1pt,
    bar width=6pt,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={TWh},
    symbolic x coords={black,brown,gas,hydro,other},
    nodes near coords align={vertical},
    \]
\addplot coordinates {(black,118.4) (brown,56.3) (gas,17.5) (hydro,11.5) (other,4.3 )};
\addplot coordinates {(black,112.9) (brown,56.4) (gas,23.4) (hydro,13.4) (other,5.0)};
\addplot coordinates {(black,107.4) (brown,54.3) (gas,23.2) (hydro,14.7) (other,6.4)};
\addplot coordinates {(black,103.4) (brown,52.9) (gas,23.9) (hydro,14.6) (other,7.1)};
\addplot coordinates {(black,99.7) (brown,47.7) (gas,23.6) (hydro,16.9) (other,8.5)};
\legend{black, brown, gas, hydro, other}
\end{axis}
\end{tikzpicture}
\end{document}]

答案1

这里有一个使用表格的建议。每个组都有自己的列。然后表格按列逐列绘制,也就是按组绘制。

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotsset{
  bar cycle list/.style={
    cycle list={%
      {blue,fill=blue,mark=none},%
      {red,fill=red,mark=none},%
      {brown!60!black,fill=brown!30!white,mark=none},%
      {black,fill=gray,mark=none},
      {fill=orange},
      {fill=green},
      {fill=olive},%
    }
  },
}

\pgfplotstableread{
black brown gas hydro other
118.4 56.3 17.5 11.5 4.3
112.9 56.4 23.4 13.4 5.0
107.4 54.3 23.2 14.7 6.4
103.4 52.9 23.9 14.6 7.1
 99.7 47.7 23.6 16.9 8.5
}{\mydata}

\pgfplotstablegetrowsof{\mydata}
  \edef\rownumber{\pgfmathresult}
\pgfmathparse{(\rownumber-1)/2}
  \edef\firsttick{\pgfmathresult}
\pgfmathparse{\firsttick+\rownumber+1}
  \edef\secondtick{\pgfmathresult}

\pgfplotstablegetcolsof{\mydata}
  \pgfmathtruncatemacro\colmax{\pgfmathresult-1}

\xdef\mylist{}
  \pgfplotstableforeachcolumn{\mydata}\as{\col}{\xdef\mylist{\mylist"\col",}}
\newcommand\mygrouplist{{\mylist}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar=-5pt,% ybar must be -bar width
    bar width=5pt,
    enlarge x limits={0.08},
    enlarge y limits={0.15},
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={TWh},
    xtick style={draw=none},
    xtick={\firsttick,\secondtick, ...,1000},
    xticklabel={\pgfmathparse{\mygrouplist[\ticknum]}\pgfmathresult},
    ]

\pgfplotsinvokeforeach{0,...,\colmax}{
  \pgfmathparse{\mygrouplist[#1]}\edef\columnname{\pgfmathresult}
  \addplot table [x expr=\coordindex+#1*(\rownumber+1),y=\columnname,meta expr=\coordindex+1]{\mydata};
  \addlegendentryexpanded{\columnname}
  }
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

也可以仅绘制选定的列/组:

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotsset{
  bar cycle list/.style={
    cycle list={%
      {blue,fill=blue,mark=none},%
      {red,fill=red,mark=none},%
      {brown!60!black,fill=brown!30!white,mark=none},%
      {black,fill=gray,mark=none},
      {fill=orange},
      {fill=green},
      {fill=olive},%
    }
  },
}

\pgfplotstableread{
black brown gas hydro other
118.4 56.3 17.5 11.5 4.3
112.9 56.4 23.4 13.4 5.0
107.4 54.3 23.2 14.7 6.4
103.4 52.9 23.9 14.6 7.1
 99.7 47.7 23.6 16.9 8.5
}{\mydata}

\pgfplotstablegetrowsof{\mydata}
  \edef\rownumber{\pgfmathresult}
\pgfmathparse{(\rownumber-1)/2}
  \edef\firsttick{\pgfmathresult}
\pgfmathparse{\firsttick+\rownumber+1}
  \edef\secondtick{\pgfmathresult}

%\pgfplotstablegetcolsof{\mydata}
  %\pgfmathtruncatemacro\colmax{\pgfmathresult-1}
\newcommand\colmax{2}

%\xdef\mylist{}
  %\pgfplotstableforeachcolumn{\mydata}\as{\col}{\xdef\mylist{\mylist"\col",}}
%\newcommand\mygrouplist{{\mylist}}
\newcommand\mygrouplist{{"black","gas","hydro"}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar=-8pt,% ybar=-bar width
    bar width=8pt,
    enlarge x limits={0.08},
    enlarge y limits={0.15},
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={TWh},
    xtick style={draw=none},
    xtick={\firsttick,\secondtick, ...,1000},
    xticklabel={\pgfmathparse{\mygrouplist[\ticknum]}\pgfmathresult}
    ]

\pgfplotsinvokeforeach{0,...,\colmax}{
  \pgfmathparse{\mygrouplist[#1]}\edef\columnname{\pgfmathresult}
  \addplot table [x expr=\coordindex+#1*(\rownumber+1),y=\columnname]{\mydata};
  \addlegendentryexpanded{\columnname}
  }
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容