如何用不同的颜色为条形图中的每一列着色?

如何用不同的颜色为条形图中的每一列着色?

我有以下条形图,我想为每列指定不同的颜色。两列位于同一个图中,当我将它们分开,只为每列指定不同的颜色值时,条形图的外观就会发生变化。有没有办法为同一个图指定不同的颜色?

 \documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}


\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{bchart}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{subfig}
\pgfplotsset{compat=1.10}

\definecolor{RYB2}{RGB}{245,245,245}
\definecolor{RYB1}{RGB}{218,232,252}
\definecolor{RYB4}{RGB}{108,142,191}


\begin{document}




\begin{figure}
    \centering


     \subfloat[][name]{\resizebox{0.45\textwidth}{!}{
                                    \begin{tikzpicture}
                                \begin{axis}[
                                symbolic x coords={col1, col2, col3},
                                xtick=data,
                                ylabel=Passed Challenges(\%),
                                xticklabel style={rotate=45,anchor=north east},
                                ymajorgrids,
                                bar width=17pt,
                                ]
                                \addplot[ybar,fill=RYB1] coordinates {
                                    (col1, 44.71) 
                                    (col2, 26.57) 
                                    (col3, 45.42) 
                                };
                                \end{axis}
                            \end{tikzpicture}}}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}


\end{document}

谢谢

答案1

只需\addplot对每列执行定义的颜色即可。

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}    
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{bchart}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{subfig}
\pgfplotsset{compat=1.10}

\definecolor{RYB2}{RGB}{245,245,245}
\definecolor{RYB1}{RGB}{218,232,252}
\definecolor{RYB4}{RGB}{108,142,191}

\begin{document}    

\begin{figure}
    \centering    

     \subfloat[][name]{\resizebox{0.45\textwidth}{!}{
                                    \begin{tikzpicture}
                                \begin{axis}[
                                symbolic x coords={col1,,col2,,col3},
                                xticklabel style={rotate=45,anchor=north east},
                                xtick={col1,col2,col3},
                                ylabel=Passed Challenges(\%),
                                ymajorgrids,
                                bar width=17pt,
                                ]
                                \addplot[ybar,fill=RYB1] 
                                coordinates {(col1,44.71)};

                                \addplot[ybar,fill=RYB2] 
                                coordinates {(col2,26.57)};

                                \addplot[ybar,fill=RYB4] 
                                coordinates {(col3,45.42)};

                                \end{axis}
                            \end{tikzpicture}}}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}    

\end{document}

相关内容