Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to plot normalized data in a bar chart. To better show that the data is normalized, I want the horizontal grid line at 1 to look differently, eg. dashed instead of dotted.

I guess this can be done by (1) changing the style of a single grid line (which I have no idea how to do) or by (2) manually adding the line at 1 (which I managed to do) and skipping the grid line so that the lines are not drawn over each other (which I, again, do not know how to do).

This is my MWE for the second case:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{backgrounds}

\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}

\pgfplotstableread{
Model       runtime normalized
(1)     0.17    0.11
(2)     1.60    1.00
(3)     3.17    1.98
(4)     1.72    1.08
(5)     4.01    2.51
}\data

\begin{axis}[
    ybar,
    ymin=0,
    xtick=data,
    xticklabels from table={\data}{Model},
    grid style={dotted,gray},
    ymajorgrids=true,
    nodes near coords,
]

\addplot [draw=black,fill=gray!15] table [y=normalized,x expr=\coordindex] {\data};
\begin{scope}[on background layer]
\draw [dashed] ({rel axis cs:0,0}|-{axis cs:0,1}) -- ({rel axis cs:1,0}|-{axis cs:0,1});
\end{scope}

\end{axis}
\end{tikzpicture}

\end{document}

This results in the following:

enter image description here

Here, the grid line for the value 1 is still drawn, and its dots appear between the dashes of my manually added line, which looks ugly to me.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

One way to do it is to specify all yticks except 1 and then use the extra y ticks and extra y ticks style to add the tick at y=1 and to change the grid style only for that tick.

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{backgrounds}

%\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}

\pgfplotstableread{
Model       runtime normalized
(1)     0.17    0.11
(2)     1.60    1.00
(3)     3.17    1.98
(4)     1.72    1.08
(5)     4.01    2.51
}\data

\begin{axis}[
    ybar,
    ymin=0,
    xtick=data,
   ytick={0.5,1.5,2.0,2.5},
    extra y ticks=1,
    extra y tick style={grid=major, grid style={dashed,black}},
    xticklabels from table={\data}{Model},
    grid style={dotted,gray},
    ymajorgrids=true,
    nodes near coords,
]

\addplot [draw=black,fill=gray!15] table [y=normalized,x expr=\coordindex] {\data};

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.