1

I have the following bar chart and I would like to color each column differently. Both columns are in the same plot, when I separate them just to give a different color values for each one the appearance of the bar chart changes. Is there a way to specify different colors for the same plot?

 \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}

Thank you

2

Just do \addplotfor each column with a defined color.

\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}
  • Thank you for your answer, but the name of the second and third columns has eliminated – User505 Apr 19 '18 at 14:46
  • 1
    @User505 Should be fixed now. Added col1,,col2,,col3 in symbolic x coords and xtick. – AML Apr 19 '18 at 15:37
  • @AML Thanks for that comment, I had the exact same issue. – Tomáš Sláma Apr 6 '19 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.