在 tikz 图上设置特定的 y 刻度样式

在 tikz 图上设置特定的 y 刻度样式

我想更改单个 y 轴刻度标记的样式。y 轴表示汽车的英里/小时速度,我希望速度限制为一条粗线。我目前设置了主刻度和次刻度。主刻度有 2 个刻度(速度限制和速度限制 *2),样式为粗体,次刻度的速度介于两者之间。当某人的行驶速度超过速度限制的两倍并超出图表时,就会出现问题(ymax 值设置为速度限制 *2)。我正在通过 ac 程序批量制作 .tex 文档,因此无法手动编辑每个图表。我认为有一种方法可以对单个刻度设置不同的样式,或者在图表上添加一条单独的水平线来表示速度限制。如果您需要更多信息,请告诉我。

这是我正在制作的图表:

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

感谢您的帮助。

答案1

由于您希望图表范围来自数据,因此请pgfplots自行设置刻度标记/网格线。只需在顶部绘制较暗的线条即可。

获取速度限制信息的一种方法是编写一个包含\def\limit{25}或甚至只是 的文件25。或者,您可以将其作为第三列添加到其中一个表中,然后使用\pgfplotstableread和读取它\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}

演示

相关内容