编辑:

编辑:

我有一张条形图,其中两个条形中的第一个条形具有相同的颜色,第二个条形也具有相同的颜色。我的条形图如下:

在此处输入图片描述

生成它的代码是:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{float}
\pgfplotsset{compat=1.10}

\definecolor{RYB2}{RGB}{245,245,245}%AAAAAAAAAAAAAAA
\definecolor{RYB1}{RGB}{218,232,252}%
\definecolor{RYB3}{RGB}{252,252,252}%
\definecolor{RYB4}{RGB}{108,142,191}%AAAAAAAAAAAA AAAAAAAAAAAA
\definecolor{RYB5}{RGB}{85, 114, 155}%
\definecolor{RYB6}{RGB}{67, 94, 132}%

\begin{document}
\begin{figure}[htb]
 \centering    
 \begin{tikzpicture}
   \begin{axis}[symbolic x coords={alg1,, alg2,,alg3,, alg4},
     xticklabel style={rotate=45,anchor=north east},
     xtick={alg1,, alg2,,alg3,, alg4},
     ylabel=Measure(\%),
     ymajorgrids,
     bar width=17pt,ybar,enlargelimits=0.1
     ]
     \addplot[fill=RYB1] coordinates {(alg1,44.71) 
     (alg2,26.57) (alg3,37.57) (alg4,39.57)};
     \addplot[fill=RYB4] coordinates {(alg1,24.71) 
     (alg2,16.57) (alg3,27.57) (alg4,59.57)};
   \end{axis}
 \end{tikzpicture}
 \caption{Figure name}
 \label{fig:results_1}
\end{figure} 
\end{document}

有没有办法让每两个条形图具有相同的颜色?

我发布的原始代码取自@marmot 的回答这个问题。特别感谢他/她的努力,我们真的很感激。

谢谢

答案1

我觉得我不太理解你的问题。如果你想让两个条形图具有相同的颜色,只需使用 中的相同颜色选项即可\addplot。在这里我使用RYB4

\documentclass[tikz,margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\definecolor{RYB2}{RGB}{245,245,245}%AAAAAAAAAAAAAAA
\definecolor{RYB1}{RGB}{218,232,252}%
\definecolor{RYB3}{RGB}{252,252,252}%
\definecolor{RYB4}{RGB}{108,142,191}%AAAAAAAAAAAA AAAAAAAAAAAA
\definecolor{RYB5}{RGB}{85, 114, 155}%
\definecolor{RYB6}{RGB}{67, 94, 132}%

\begin{document}
 \begin{tikzpicture}
   \begin{axis}[symbolic x coords={alg1,, alg2,,alg3,, alg4},
     xticklabel style={rotate=45,anchor=north east},
     xtick={alg1,, alg2,,alg3,, alg4},
     ylabel=Measure(\%),
     ymajorgrids,
     bar width=17pt,ybar,enlargelimits=0.1
     ]
     \addplot[fill=RYB4] coordinates {(alg1,44.71) 
     (alg2,26.57) (alg3,37.57) (alg4,39.57)};
     \addplot[fill=RYB4] coordinates {(alg1,24.71) 
     (alg2,16.57) (alg3,27.57) (alg4,59.57)};
   \end{axis}
 \end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:

如果你希望每个alg都有相同的颜色,那么这是一个非pgfplots解决方案

\documentclass[tikz,margin=3mm]{standalone}

\begin{document}
\begin{tikzpicture}
% Axis
\draw (0,1.3) rectangle (5.5,6.5);
\foreach \i in {2,...,6} {
    \draw[thin,gray!50] (0,\i)--(5.5,\i);
    \draw (0,\i) node[left] {\i0} -- (.1,\i);
}
\foreach \i [count=\ct] in {0.5,2,3.5,5} {
    \draw (\i,1.3)--(\i,1.2) node[below] {alg\ct};
    \draw (\i,6.5)--(\i,6.6);
}
\node[rotate=90,above=4ex] at (0,3.9) {Measure (\%)};
% Bars
\path[fill=red,draw=black,very thin] (0,1.3) rectangle (0.475,4.471) (0.525,1.3) rectangle (1,2.471);
\path[fill=yellow,draw=black,very thin] (1.5,1.3) rectangle (1.975,2.657) (2.025,1.3) rectangle (2.5,1.657);
\path[fill=green,draw=black,very thin] (3,1.3) rectangle (3.475,3.757) (3.525,1.3) rectangle (4,2.757);
\path[fill=blue,draw=black,very thin] (4.5,1.3) rectangle (4.975,3.957) (5.025,1.3) rectangle (5.5,5.957);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容