[Tex/LaTex] Styling a particular y tick on tikz plot

tickstikz-pgf

I want to change the style of a single y axis tick mark. The y-axis is for a car's speed in mph, and I want the speed limit to be a single bold line. I currently have major, and minor ticks set up. I have 2 ticks (speed limit & speed limit *2) for the major ticks and is styled bold, and the minor ticks have the speeds in between the two. The issue arises when a person does more then twice the speed limit and goes off the chart (the ymax value is set to the speed limit *2). I am making the .tex documents in bulk via a c program, so I cannot hand edit each chart for the occurrences. I would assume there is a way in order to style a single tick differently, or adding a separate horizontal line on the chart to represent the speed limit. Let me know if you need anymore information.

Here's the chart that I am making:

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
        [
            title = Peak vs. Average Speeds,
            enlarge x limits = 0.03,
            ymin = 0,
            ymax = 50,    %This is controlled by speed limit *2
            ybar = 0pt,
            ylabel=Speed (mph),
            ymajorgrids = true,
            yminorgrids = true,
            minor y tick num = {4},
            ytick = {0, 25, 50},
            ytick pos = left,
            extra y tick style = {major grid style = {line width = 0.1pt, draw=gray!}},

            % These tick labels are generated by a loop being controlled by the speed limit. It would be great if I didn't need it.
            extra y tick labels = {5, 10, 15, 20, 30, 35, 40, 45},
            extra y ticks = {5, 10, 15, 20, 30, 35, 40, 45},
            symbolic x coords = {00:00, 00:30, 01:00, 01:30, 02:00, 02:30, 03:00, 03:30, 04:00, 04:30, 05:00, 05:30, 06:00, 06:30, 07:00, 07:30, 08:00, 08:30, 09:00, 09:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30, 18:00, 18:30, 19:00, 19:30, 20:00, 20:30, 21:00, 21:30, 22:00, 22:30, 23:00, 23:30, 24:00},
            x axis line style = {line width = 1pt},
            xtick pos = left,
            xtick = {00:00, 02:00, 04:00, 06:00, 08:00, 10:00, 12:00, 14:00, 16:00, 18:00, 20:00, 22:00, 24:00},
            minor x tick num = {3},
            major grid style = {line width = 0.6pt, draw = black!},
            minor grid style = {line width = 0.1pt, draw = gray!},
            bar width = 3pt,
            width = 7in,
            height = 4.25in,
            legend pos = north west
        ]
        \addplot table[x=Time, y=Peak Speed, col sep = comma] {Peak_and_Average_Speeds.csv};
        \addplot table[x=Time, y=Average Speed, col sep = comma] {Peak_and_Average_Speeds.csv};
        \legend{Peak Speed, Average Speed};
    \end{axis}
\end{tikzpicture}

Thank you for your help.

Best Answer

Since you want the graph limits to come from the data, let pgfplots set the tick marks/grid lines itself. Just draw darker lines over the top.

One way to get the speed limit info is to write a file containing \def\limit{25} or even just 25. Alternatively, you could add it to one of the tables as a third column and read it using \pgfplotstableread and \pgfplotstablegetelem.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

% the following can be either inside or outside the tikzicture
\def\limit{25}% speed limit, possibly read using \input{file}
\pgfmathsetmacro{\double}{int(2*\limit)}%

\begin{tikzpicture}
    \begin{axis}[
            title = Peak vs. Average Speeds,
            enlarge x limits = 0.03,
            ymin = 0,
            ybar = 0pt,
            ymajorgrids=true,
            major grid style = {line width = 0.1pt, draw = gray!},
            ylabel=Speed (mph),
            ytick distance=5,
            ytick pos = left,
            ytick style={opacity=0},% otherwise, zooming in will reveal ticks on top of the grid lines
            symbolic x coords = {00:00, 00:30, 01:00, 01:30, 02:00, 02:30, 03:00, 03:30, 04:00, 04:30, 05:00, 05:30, 06:00, 06:30, 07:00, 07:30, 08:00, 08:30, 09:00, 09:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30, 18:00, 18:30, 19:00, 19:30, 20:00, 20:30, 21:00, 21:30, 22:00, 22:30, 23:00, 23:30, 24:00},
            x axis line style = {line width = 1pt},
            xtick pos = left,
            xtick = {00:00, 02:00, 04:00, 06:00, 08:00, 10:00, 12:00, 14:00, 16:00, 18:00, 20:00, 22:00, 24:00},
            minor x tick num = {3},
            bar width = 3pt,
            width = 7in,
            height = 4.25in,
            legend pos = north west
        ]
        \coordinate (sw) at (rel axis cs: 0,0);% lower left corner of axis box
        \coordinate (ne) at (rel axis cs: 1,1);% upper right corner of axis box
        \coordinate (limit) at (axis cs: 00:00,\limit);% any x coord will do
        \coordinate (double) at (axis cs: 00:00,\double);
        \draw[black,line width = 0.6pt] (sw|-limit) -- (ne|-limit) (sw|-double)--(ne|-double);
        %\addplot table[x=Time, y=Peak Speed, col sep = comma] {Peak_and_Average_Speeds.csv};
        %\addplot table[x=Time, y=Average Speed, col sep = comma] {Peak_and_Average_Speeds.csv};
        \addplot coordinates {(00:00,10) (24:00,65)}; % replace unavailable tables
        \legend{Peak Speed, Average Speed};
    \end{axis}
\end{tikzpicture}
\end{document}

demo