如何在图表中添加新颜色?

如何在图表中添加新颜色?

我有一个图,其中有多个图例条目。6 个图例条目的颜色会轮换为不同的颜色。之后,我有一个重复的颜色。如何为第 7 个条目指定新颜色(在 MWE 中,它是“永恒”)。

梅威瑟:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    bar width=15pt,
    nodes near coords,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
    ylabel={\#participants},
    symbolic x coords={2005, tool2, tool3, tool4, 
        tool5, tool6, tool7},
    xtick=data,
    x tick label style={rotate=45,anchor=east},
    ]
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2) 
  (tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0) 
  (tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)
  (tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\legend{\strut never, \strut rarely, \strut sometimes, \strut often, \strut always, \strut forever, \strut eternity}
\end{axis}
\end{tikzpicture}
\end{document}

问题:条目 1(“never”)和条目 7(“eternity”)的颜色相同。我希望所有颜色都不同。

在此处输入图片描述

更新:我尝试添加自己的颜色cycle list,如新的 MWE 中所示,但颜色仍然相同。

最大能量损失 2:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\pgfplotscreateplotcyclelist{Syphirint}{
   fill=black!80,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!10,draw=black,solid,mark=none\\
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[cycle list name=Syphirint,
    ybar stacked,
    bar width=15pt,
    nodes near coords,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
    ylabel={\#participants},
    symbolic x coords={2005, tool2, tool3, tool4, 
        tool5, tool6, tool7},
    xtick=data,
    x tick label style={rotate=45,anchor=east},
    ]
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2) 
  (tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0) 
  (tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)
  (tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\legend{\strut never, \strut rarely, \strut sometimes, \strut often, \strut always, \strut forever, \strut eternity}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

使其发挥作用的关键是提供cycle list/cycle list name 说明ybar/ybar stacked因为它们会自行加载bar cycle list,从而覆盖先前说明的cycle list/ cycle list name

(请注意,我也大大简化了您的代码。有关详细信息,请查看代码中的注释。)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
    \usepgfplotslibrary{colorbrewer}
    \pgfplotsset{
        compat=1.8,
        width=7cm,
        % (either create you own cycle list which has enough colors
        %  or use one that is already defined. I have chosen the latter
        %  from the `colorbrewer` library.
        %  Please note that this only *loads* the `cycle list`,
        %  but doesn't *activate* it yet.
        %  (Please note that this provided only a `draw` color,
        %   but not a `fill` color))
        cycle list/Set1-7,
    }
    % (I moved your data to a table which much more clearly
    %  I used the headings to provide the legend entries as well)
    \pgfplotstableread{
        x       never   rarely  sometimes   often   always  forever eternity
        tool1   0       0       6           4       4       4       4
        tool2   2       0       6           2       2       2       2
        tool3   2       0       8           0       0       0       0
        tool4   3       3       2           2       2       2       2
        tool5   0       1       6           3       3       3       3
        tool6   2       1       5           2       2       2       2
        tool7   0       0       6           4       4       4       4
    }{\tabledata}
    % (adapted from <https://tex.stackexchange.com/a/24023/95441>)
    \newcommand{\plottable}[1]{
        % get the number of columns in the table ...
        \pgfplotstablegetcolsof{#1}
        % ... and store it in a variable
        \pgfmathtruncatemacro\numberofcols{\pgfplotsretval-1}
        % cycle through the (y) columns
        \pgfplotsinvokeforeach{1,...,\numberofcols}{
            % plot the data
            \addplot table [x expr=\coordindex,y index=##1] {#1};
            % get the heading (name) of the currently processed column
            % and store it in a variable
            \pgfplotstablegetcolumnnamebyindex{##1}\of{#1}\to{\colname}
            % add the legend entry using the stored variable
            \addlegendentryexpanded{\colname}
        }
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        % activate the custom `cycle list`
        % It is important that this is done *after* stating
        % `ybar`/`ybar stacked`, because these styles incorporate loading
        % a default `cycle list` themselves. Thus a previously added
        % `cycle list` would be overwritten.
        cycle list name=Set1-7,
        bar width=15pt,
        enlargelimits=0.15,
        ylabel={\#participants},
        xtick=data,
        % (use the "x" column to provide the `xticklabels`
        %  so you don't have to repeat them manually by providing them
        %  as `symbolic coordinates`. But then numbers are required as
        %  input which we did above by stating `x expr=\coordindex`.)
        xticklabels from table={\tabledata}{x},
        x tick label style={rotate=45,anchor=east},
        nodes near coords,
        % (in case you don't create a `cycle list` *with* a fill color
        %  -- like in this case -- you could do this to
        %  add a fill color for all `\addplot` commands)
        every axis plot post/.append style={
            fill=.!75,
        },
        legend style={
            at={(0.5,-0.20)},
            anchor=north,
            legend columns=-1,
        },
    ]
        % (use above command to plot the bars)
        \plottable{\tabledata}
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容