直方图中颜色的代码

直方图中颜色的代码

我想知道以下直方图(代码+图像)中出现的颜色代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}

\pgfplotstableread[col sep=comma,header=false]{
Category 1,6,0,0
Category 2,2,2,0
Category 3,3.5,2.5,4
}\data

\pgfplotstablecreatecol[
    create col/expr={
        \thisrow{1} + \thisrow{2} + \thisrow{3}
    }
]{sum}{\data}

\pgfplotsset{
    percentage plot/.style={
        point meta=explicit,
    every node near coord/.append style={
        align=center,
        text width=1cm
    },
        nodes near coords={
        \pgfmathtruncatemacro\iszero{\originalvalue==0}
        \ifnum\iszero=0
            \pgfmathprintnumber{\originalvalue}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\pgfplotspointmeta}
        \fi},
    nodes near coords align=vertical,
        yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
        ymin=0,
        ymax=100,
        enlarge y limits={upper,value=0.18},
    visualization depends on={y \as \originalvalue}
    },
    percentage series/.style={
        table/y expr=\thisrow{#1}/\thisrow{sum}*100,table/meta=#1
    }
}

\begin{tikzpicture}
\begin{axis}[
    axis on top,
    width=10cm,
    percentage plot,ybar,bar width=0.75cm,
    enlarge x limits=0.25,
    symbolic x coords={Category 1,Category 2,Category 3},
    xtick=data
]
\addplot table [percentage series=1] {\data};
\addplot table [percentage series=2] {\data};
\addplot table [percentage series=3] {\data};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

例如,我尝试使用蓝色绘制以下直方图,但没有得到相同的颜色:

 \begin{center}
\begin{tikzpicture}
 \begin{axis}[
    ymin=0, ymax=25,
    xmin=-5.3, xmax=80,
    xtick={0, 10, 25, 35, 45, 55, 70},
    ytick={0, 5, ..., 30},
    enlargelimits=0, 
    area style,
    every node near coord/.append style={
        anchor=north,
        },
  ]
\addplot[ybar interval,fill=blue!30,draw=blue] coordinates{(0,16.33)(10,20.57)};
\addplot[ybar interval,fill=blue!30,draw=blue] coordinates{(10,20.57)(25,19.58)};
\addplot[ybar interval,fill=blue!30,draw=blue] coordinates{(25,19.58)(35,17.78)};
\addplot[ybar interval,fill=blue!30,draw=blue] coordinates{(35,17.78)(45,11.97)};
\addplot[ybar interval,fill=blue!30,draw=blue] coordinates{(45,11.97)(55,13.77)};
\addplot[ybar interval,fill=blue!30,draw=blue] coordinates{(55,13.77)(70,13.77)};
  \end{axis}
\end{tikzpicture}

太感谢了

答案1

颜色是

\definecolor{newblue}{rgb}{0.70,0.70,1.00} % with  border blue
\definecolor{newred}{rgb}{1.00,0.70,0.70} % with border red
\definecolor{newbrown}{rgb}{0.925,0.850,0.775} % with border newbrownborder
\definecolor{newbrownborder}{rgb}{0.45,0.30,0.15}

它们是使用 Inkscape(免费)直接从文件中读取的.pdf。也可以使用 Illustrator 或 Adob​​e Acrobat。在这种情况下,可以在源代码中找到它们,但情况并非总是如此。您可以在不知道文件生成方式的情况下获取文件,或者源代码可能不可用。

A

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\definecolor{newblue}{rgb}{0.70,0.70,1.00} % with  border blue
\definecolor{newred}{rgb}{1.00,0.70,0.70} % with border red
\definecolor{newbrown}{rgb}{0.925,0.850,0.775} % with border newbrownborder
\definecolor{newbrownborder}{rgb}{0.45,0.30,0.15}

\begin{document}
    
    \pgfplotstableread[col sep=comma,header=false]{
        Category 1,6,0,0
        Category 2,2,2,0
        Category 3,3.5,2.5,4
    }\data
    
    \pgfplotstablecreatecol[
    create col/expr={
        \thisrow{1} + \thisrow{2} + \thisrow{3}
    }
    ]{sum}{\data}
    
    \pgfplotsset{
        percentage plot/.style={
            point meta=explicit,
            every node near coord/.append style={
                align=center,
                text width=1cm
            },
            nodes near coords={
                \pgfmathtruncatemacro\iszero{\originalvalue==0}
                \ifnum\iszero=0
                \pgfmathprintnumber{\originalvalue}$\,\%$\\ \pgfmathprintnumber[fixed zerofill,precision=1]{\pgfplotspointmeta}
                \fi},
            nodes near coords align=vertical,
            yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
            ymin=0,
            ymax=100,
            enlarge y limits={upper,value=0.18},
            visualization depends on={y \as \originalvalue}
        },
        percentage series/.style={
            table/y expr=\thisrow{#1}/\thisrow{sum}*100,table/meta=#1
        }
    }
    
\noindent   \begin{tikzpicture}
        \begin{axis}[
            axis on top,
            width=10cm,
            percentage plot,ybar,bar width=0.75cm,
            enlarge x limits=0.25,
            symbolic x coords={Category 1,Category 2,Category 3},
            xtick=data
            ]
            \addplot table [percentage series=1] {\data};
            \addplot table [percentage series=2] {\data};
            \addplot table [percentage series=3] {\data};
        \end{axis}
    \end{tikzpicture}

\bigskip

    \begin{tikzpicture}
        \begin{axis}[
            ymin=0, ymax=25,
            xmin=-5.3, xmax=80,
            xtick={0, 10, 25, 35, 45, 55, 70},
            ytick={0, 5, ..., 30},
            enlargelimits=0, 
            area style,
            every node near coord/.append style={
                anchor=north,
            },
            ]
            \addplot[ybar interval,fill=newblue,draw=blue] coordinates{(0,16.33)(10,20.57)};
            \addplot[ybar interval,fill=newblue,draw=blue] coordinates{(15,20.57)(25,19.58)};
            \addplot[ybar interval,fill=newred,draw=red] coordinates{(26,19.58)(36,17.78)};
            \addplot[ybar interval,fill=newblue,draw=blue] coordinates{(40,17.78)(50,11.97)};
            \addplot[ybar interval,fill=newred,draw=red] coordinates{(51,11.97)(61,13.77)};
            \addplot[ybar interval,fill=newbrown,draw=newbrownborder] coordinates{(62,13.77)(72,13.77)};
        \end{axis}
    \end{tikzpicture}

\end{document}

相关内容