7

I am trying to create a ybar graph that has consistent spacing between a set of columns. I have obtained the color scheme I need in the following example, based off guidance from this question on here

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

However, as you can see, because I am using 3 different plots, each x value makes room for all 3 plots, which is obviously not what I want. I found this question, describing the use of a custom style with created discard if and discard if not commands. My attempt at that is not successful (code below). I keep getting warnings about no coordinates left, or they've all been filtered away. I thought I could use this method, and just have an individual plot with a discard if not call for each plot.

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

Here is code for the plot without any coloring, but the ideal format and spacing that it should look like:

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

So how do I merge the ideas to get consistent spacing and the correct numbering on the bottom, while achieving the color scheme that I need?

1 Answer 1

6

If you use ybar in the axis options, each plot series will get a different amount of bar shift to keep the bars from overlapping. Usually, that's exactly what you want. However, in your case, you'll want to set bar shift=0pt in the axis options:

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

If you want to change the colour of an individual bar, you can use the approach described in your second link, but there were a couple of errors in the code.

\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}
1
  • 1
    Perfect! Knew I was probably close, just couldn't get all the pieces to come together
    – J M
    Commented Sep 6, 2012 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .