创建具有一致间距的自定义彩色 PGFPlots ybar

创建具有一致间距的自定义彩色 PGFPlots ybar

我正在尝试创建一个ybar在一组列之间具有一致间距的图表。根据此问题的指导,我在以下示例中获得了所需的配色方案这里

\documentclass{memoir}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in, headsep=0.1in, head=0.4in, includehead, includefoot, marginparsep=0in, marginparwidth=0in,footskip=0.4in]{geometry}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{xcolor}
\pagecolor{black}
\pgfplotsset{width=7cm,compat=1.5.1}
\usepackage{siunitx}
\sisetup{inter-unit-product={}\cdot{},sticky-per=true,multi-part-units=single,separate-uncertainty=true,list-units=single,range-units=single,detect-all,detect-weight=true,detect-inline-weight=math}

\DeclareSIUnit\calorie{cal}
\DeclareSIUnit\kcal{\kilo\calorie}
\DeclareSIUnit\kcalpmole{kcal\per\mole}

\definecolor{hoyemagenta}{HTML}{FF3FFF}
\definecolor{hoyegreen}{HTML}{31FF31}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        color=white,
        width=\textwidth,
        ybar,
        enlarge x limits=0.07,
        ylabel={Free Energy Difference (\si{\kcalpmole})},
        ylabel/.style={color=white},
        xtick={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7},
        every axis/.style={color=white},
        ymax=40,
        ymin=-110,
        xtick pos=left,
        nodes near coords,
        nodes/.style={color=white},
        nodes near coords align={vertical},
        x tick label style={rotate=45,anchor=east,color=white},
        symbolic x coords={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7}
        ]
        \addplot[fill=hoyegreen,]
        coordinates{
        (1,-48.93)
        (2b,-13.62)
        (4b,-46.85)
        (5b,-42.27)
        (6b,-55.74)};
        \addplot[fill=hoyemagenta]
        coordinates{        
        (2a,-26.90)
        (3,31.78)
        (4a,-49.25)
        (5a,-49.71)
        (6a,-44.14)
        (6c,-51.07)};
        \addplot[fill=yellow]
        coordinates{(7,-97.69)};
    \end{axis}
\end{tikzpicture}
\end{document}

但是,正如您所看到的,因为我使用了 3 个不同的图,所以每个 x 值都会为所有 3 个图腾出空间,这显然不是我想要的。我发现这个问题,描述了使用自定义样式和创建discard if命令。我的尝试没有成功(下面的代码)。我不断收到有关没有剩余坐标或它们已被过滤掉的警告。我想我可以使用这种方法,并且只为每个图discard if not创建一个单独的图并调用一次。discard if not

\documentclass{memoir}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in, headsep=0.1in, head=0.4in, includehead, includefoot, marginparsep=0in, marginparwidth=0in,footskip=0.4in]{geometry}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{filecontents}
\pagecolor{black}
\pgfplotsset{width=7cm,compat=1.5.1}
\usepackage{siunitx}
\sisetup{inter-unit-product={}\cdot{},sticky-per=true,multi-part-units=single,separate-uncertainty=true,list-units=single,range-units=single,detect-all,detect-weight=true,detect-inline-weight=math}

\DeclareSIUnit\calorie{cal}
\DeclareSIUnit\kcal{\kilo\calorie}
\DeclareSIUnit\kcalpmole{kcal\per\mole}

\definecolor{hoyemagenta}{HTML}{FF3FFF}
\definecolor{hoyegreen}{HTML}{31FF31}

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{keyword}}
            \edef\tempb{TestDetails}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}


\begin{document}

\begin{filecontents}{data.csv}
{Compound},{Energy}
1,-48.93
2a,-26.90
2b,-13.62
3,31.78
4a,-49.25
4b,-46.85
5a,-49.71
5b,-42.27
6a,-44.14
6b,-55.74
6c,-51.07
7,-97.69
\end{filecontents}
\pgfplotstablegetrowsof{data.csv}

\begin{tikzpicture}
    \begin{axis}[
        color=white,
        width=\textwidth,
        ybar,/pgf/bar shift=0pt,
        enlarge x limits=0.07,
        ylabel={Free Energy Difference (\si{\kcalpmole})},
        ylabel/.style={color=white},
        xtick={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7},
        every axis/.style={color=white},
        ymax=40,
        ymin=-110,
        xtick pos=left,
        nodes near coords,
        nodes/.style={color=white},
        nodes near coords align={vertical},
        x tick label style={rotate=45,anchor=east,color=white},
        xticklabels={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7}
        ]
        \addplot[fill=hoyegreen,discard if not={Compound}{1}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta,discard if not={Compound}{2a}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyegreen,discard if not={Compound}{2b}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta,discard if not={Compound}{3}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta,discard if not={Compound}{4a}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyegreen,discard if not={Compound}{4b}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta,discard if not={Compound}{5a}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyegreen,discard if not={Compound}{5b}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta,discard if not={Compound}{6a}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyegreen,discard if not={Compound}{6b}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta,discard if not={Compound}{6c}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
        \addplot[fill=hoyegreen,discard if not={Compound}{7}] table[x expr=\coordindex,x index=0, col sep=comma]{data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

这是没有任何颜色的绘图代码,但它应该是理想的格式和间距:

\documentclass{memoir}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in, headsep=0.1in, head=0.4in, includehead, includefoot, marginparsep=0in, marginparwidth=0in,footskip=0.4in]{geometry}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{xcolor}
\pagecolor{black}
\pgfplotsset{width=7cm,compat=1.5.1}
\usepackage{siunitx}
\sisetup{inter-unit-product={}\cdot{},sticky-per=true,multi-part-units=single,separate-uncertainty=true,list-units=single,range-units=single,detect-all,detect-weight=true,detect-inline-weight=math}

\DeclareSIUnit\calorie{cal}
\DeclareSIUnit\kcal{\kilo\calorie}
\DeclareSIUnit\kcalpmole{kcal\per\mole}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        color=white,
        width=\textwidth,
        ybar,
        enlarge x limits=0.07,
        ylabel={Free Energy Difference (\si{\kcalpmole})},
        ylabel/.style={color=white},
        xtick=data,
        every axis/.style={color=white},
        ymax=40,
        ymin=-110,
        xtick pos=left,
        nodes near coords,
        nodes/.style={color=white},
        nodes near coords align={vertical},
        x tick label style={rotate=45,anchor=east,color=white},
        xticklabels={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7}
        ]
        \addplot[fill=white] table [
            x expr=\coordindex, % Use the row number as x coordinate
            header=false    % Do not assume the first row to contain column names
        ] {
        nmeimid         -48.93
        co2lactone      -26.90
        co2carbene      -13.62
        n2               31.78
        meNCOlactone    -49.25
        meNCOcarbene    -46.85        
        cs2lactone      -49.71
        cs2carbene      -42.27
        meNCSlactoneS   -44.14
        meNCScarbene    -55.74
        meNCSlactoneN   -51.07                  
        colactone       -97.69
        };
    \end{axis}
\end{tikzpicture}
\end{document}

那么,我该如何合并这些想法以获得一致的间距和底部的正确编号,同时实现我需要的配色方案?

答案1

如果您ybar在轴选项中使用,则每个绘图系列将获得不同数量的bar shift以防止条形重叠。通常,这正是您想要的。但是,对于您的情况,您需要bar shift=0pt在轴选项中设置:

\documentclass{memoir}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in, headsep=0.1in, head=0.4in, includehead, includefoot, marginparsep=0in, marginparwidth=0in,footskip=0.4in]{geometry}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{xcolor}
\pagecolor{black}
\pgfplotsset{width=7cm,compat=1.5.1}
\usepackage{siunitx}
\sisetup{inter-unit-product={}\cdot{},sticky-per=true,multi-part-units=single,separate-uncertainty=true,list-units=single,range-units=single,detect-all,detect-weight=true,detect-inline-weight=math}

\DeclareSIUnit\calorie{cal}
\DeclareSIUnit\kcal{\kilo\calorie}
\DeclareSIUnit\kcalpmole{kcal\per\mole}

\definecolor{hoyemagenta}{HTML}{FF3FFF}
\definecolor{hoyegreen}{HTML}{31FF31}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        color=white,
        width=\textwidth,
        ybar, bar shift=0pt,
        enlarge x limits=0.07,
        ylabel={Free Energy Difference (\si{\kcalpmole})},
        ylabel/.style={color=white},
        xtick={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7},
        every axis/.style={color=white},
        ymax=40,
        ymin=-110,
        xtick pos=left,
        nodes near coords,
        nodes/.style={color=white},
        nodes near coords align={vertical},
        x tick label style={rotate=45,anchor=east,color=white},
        symbolic x coords={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7}
        ]
        \addplot[fill=hoyegreen,]
        coordinates{
        (1,-48.93)
        (2b,-13.62)
        (4b,-46.85)
        (5b,-42.27)
        (6b,-55.74)};
        \addplot[fill=hoyemagenta]
        coordinates{        
        (2a,-26.90)
        (3,31.78)
        (4a,-49.25)
        (5a,-49.71)
        (6a,-44.14)
        (6c,-51.07)};
        \addplot[fill=yellow]
        coordinates{(7,-97.69)};
    \end{axis}
\end{tikzpicture}
\end{document}

如果您想更改单个条的颜色,您可以使用第二个链接中描述的方法,但代码中存在几个错误。

\documentclass{memoir}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in, headsep=0.1in, head=0.4in, includehead, includefoot, marginparsep=0in, marginparwidth=0in,footskip=0.4in]{geometry}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{filecontents}
\pagecolor{black}
\pgfplotsset{width=7cm,compat=1.5.1}
\usepackage{siunitx}
\sisetup{inter-unit-product={}\cdot{},sticky-per=true,multi-part-units=single,separate-uncertainty=true,list-units=single,range-units=single,detect-all,detect-weight=true,detect-inline-weight=math}

\DeclareSIUnit\calorie{cal}
\DeclareSIUnit\kcal{\kilo\calorie}
\DeclareSIUnit\kcalpmole{kcal\per\mole}

\definecolor{hoyemagenta}{HTML}{FF3FFF}
\definecolor{hoyegreen}{HTML}{31FF31}

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}


\begin{document}

\begin{filecontents}{data.csv}
{Compound},{Energy}
1,-48.93
2a,-26.90
2b,-13.62
3,31.78
4a,-49.25
4b,-46.85
5a,-49.71
5b,-42.27
6a,-44.14
6b,-55.74
6c,-51.07
7,-97.69
\end{filecontents}
\pgfplotstablegetrowsof{data.csv}

\begin{tikzpicture}
    \begin{axis}[
        color=white,
        width=\textwidth,
        ybar,/pgf/bar shift=0pt,
        enlarge x limits=0.07,
        ylabel={Free Energy Difference (\si{\kcalpmole})},
        ylabel/.style={color=white},
        every axis/.style={color=white},
        ymax=40,
        ymin=-110,
        xtick pos=left,
        nodes near coords,
        nodes/.style={color=white},
        nodes near coords align={vertical},
        x tick label style={rotate=45,anchor=east,color=white},
        xtick={1,...,11},
        xticklabels={1,2a,2b,3,4a,4b,5a,5b,6a,6b,6c,7}
        ]
        \addplot[fill=hoyegreen, discard if not={Compound}{4b}] table[x expr=\coordindex, col sep=comma]{data.csv};
        \addplot[fill=hoyemagenta, discard if={Compound}{4b}] table[x expr=\coordindex, col sep=comma]{data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容