6

Is it possible to add a straight horizontal black line (from (0, 0.4) until the very end of the graph) in a bar chart environment like this:

enter image description here

Here is the code that generates this bar chart (borrowed from: Grouped bar chart)

\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}

% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.85*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Run time speed},
        symbolic x coords={EgyptHD,Hover,Navi},
        xtick = data,
        scaled y ticks = false,
        enlarge x limits=0.25,
        ymin=0,
        legend cell align=left,
        legend style={
                at={(1,1.05)},
                anchor=south east,
                column sep=1ex
        }
    ]
        \addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(EgyptHD, 1.0) (Hover,1.0) (Navi,1.0)};

        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(EgyptHD,1.123) (Hover,0.85) (Navi,1.09)};

        \addplot[style={ggreen,fill=ggreen,mark=none}]
             coordinates {(EgyptHD,0.92) (Hover,0.56) (Navi,0.95)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {(EgyptHD,0.74) (Hover,1.07) (Navi,1.23)};

        \legend{No vectorization,TreeScore $>2$,TreeScore $>3$,TreeScore $>4$}
    \end{axis}
\end{tikzpicture}
\end{document}
4

You can use extra y ticks like

    extra y ticks = 0.4,
    extra y tick labels={},
    extra y tick style={grid=major,major grid style={thick,draw=black}}

and draw a grid. To add a legend, you can use \addlegendimage and define an image code my legend as in the following code.

\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}

% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}

\pgfplotsset{
/pgfplots/my legend/.style={
legend image code/.code={
\draw[thick,black](-0.05cm,0cm) -- (0.3cm,0cm);%
   }
  }
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.85*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Run time speed},
        symbolic x coords={EgyptHD,Hover,Navi},
        xtick = data,
        scaled y ticks = false,
        enlarge x limits=0.25,
        ymin=0,
        legend cell align=left,
        legend style={
                at={(1,1.05)},
                anchor=south east,
                column sep=1ex
        },
        extra y ticks = 0.4,
        extra y tick labels={},
        extra y tick style={grid=major,major grid style={thick,draw=black}}
    ]
        \addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(EgyptHD, 1.0) (Hover,1.0) (Navi,1.0)};

        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(EgyptHD,1.123) (Hover,0.85) (Navi,1.09)};

        \addplot[style={ggreen,fill=ggreen,mark=none}]
             coordinates {(EgyptHD,0.92) (Hover,0.56) (Navi,0.95)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {(EgyptHD,0.74) (Hover,1.07) (Navi,1.23)};

        \legend{No vectorization,TreeScore $>2$,TreeScore $>3$,TreeScore $>4$}
        \addlegendimage{my legend}
        \addlegendentry{My line}
        %\draw (rel axis cs: 0,0.3) -- (rel axis cs: 1, 0.3);
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • @warship That is because I forgot extra y tick labels={},. Add it please. – user11232 Oct 15 '15 at 3:00
  • +1 Can you please show how to add this line to the legend entries? – warship Oct 15 '15 at 3:06
  • @warship Sure, I updated. Sorry for the delay I was held up. – user11232 Oct 15 '15 at 3:48
  • I mean add a legend entry "my_line" next to "No vectorization", "TreeScore>2", etc, etc in the legend box.... in other words, to show what the straight horizontal line represents. – warship Oct 15 '15 at 4:42
  • @warship Answer updated. – user11232 Oct 15 '15 at 5:30

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.