指定网格线样式为“额外 y 刻度标签”

指定网格线样式为“额外 y 刻度标签”

我不明白如何设计添加的额外网格线extra y tick style

也许我忽略了一些东西。这里解释了如何仅在指定位置绘制网格线,以及这个答案实际上解释了这一点,但是在我当前的代码中没有任何内容被呈现。

MWE 如下:

\begin{filecontents*}{data.csv}
t,ch1,ch2,ch3
-4,-2,0.0625,0.756
-3,-1.5,0.125,-0.141
-2,-1,0.25,-0.909
-1,-0.5,0.5,-0.841
0,0,1,0
1,0.5,2,0.841
2,1,4,0.909
3,1.5,8,0.141
4,2,16,-0.758
\end{filecontents*}


\documentclass[border=5mm]{standalone}
\usepackage{pgfplots,siunitx}
\pgfplotsset{compat=newest}
\begin{document} 
\begin{tikzpicture}
\pgfplotsset{
  ymin=-4,
  ymax=17
  }
%% ch2 plot
\begin{axis}[
    scale only axis,
    axis y line*=left,
    axis x line*=bottom,
    xlabel={Time [\si{s}]},
    ylabel={Current [\si{\A}]},
    grid=major
]
\addplot [green] table[x=t,y=ch2,col sep=comma] {data.csv};
\end{axis}

%% ch3 plot
\begin{axis}[
    scale only axis,
    axis y line*=right,
    axis x line*=bottom,
    xtick=\empty,
    ylabel={Voltage (Normalised)},
    ylabel style={red},
extra y ticks={-1,1},   %% <-- Here starts the confusion
        extra y tick labels={-3,5},
        extra y tick style={
            grid=major,
        },
]
\addplot [red] table[x=t,y=ch3,col sep=comma] {data.csv};
\end{axis}
   
\end{tikzpicture}
\end{document}

y=-1因此,我成功地在和处绘制了网格线y=+1,分别带有标签-35,但现在我想将其样式设置为红色,线宽=2pt,并且“密集虚线”。

怎么做?

我也尝试过这个:

extra y tick style={y tick label style={draw=red}}

但现在输出是额外标签周围的红框。

和这个

extra y tick style={y tick label style={line width=2pt}}

不绘制任何额外的网格线。

有什么帮助吗?

答案1

正如评论中提到的,这个问题处理你的主题。

major grid style={...}您可以指定环境中使用的额外刻度的主要网格线的样式extra y tick style

在此处输入图片描述

\begin{filecontents*}{data.csv}
    t,ch1,ch2,ch3
    -4,-2,0.0625,0.756
    -3,-1.5,0.125,-0.141
    -2,-1,0.25,-0.909
    -1,-0.5,0.5,-0.841
    0,0,1,0
    1,0.5,2,0.841
    2,1,4,0.909
    3,1.5,8,0.141
    4,2,16,-0.758
\end{filecontents*}


\documentclass[border=5mm]{standalone}
\usepackage{pgfplots,siunitx}
\pgfplotsset{compat=newest}
\begin{document} 
    \begin{tikzpicture}
        \pgfplotsset{
            ymin=-4,
            ymax=17
        }
        %% ch2 plot
        \begin{axis}[
            scale only axis,
            axis y line*=left,
            axis x line*=bottom,
            xlabel={Time [\si{s}]},
            ylabel={Current [\si{\A}]},
            grid=major
            ]
            \addplot [green] table[x=t,y=ch2,col sep=comma] {data.csv};
        \end{axis}
        
        %% ch3 plot
        \begin{axis}[
            scale only axis,
            axis y line*=right,
            axis x line*=bottom,
            xtick=\empty,
            ylabel={Voltage (Normalised)},
            ylabel style={red},
            extra y ticks={-1,1},   %% <-- Here starts the confusion
            extra y tick labels={-3,5},
            extra y tick style={
                grid=major,
                major grid style={red, line width=2pt, densely dashed}, % <- this solves your problem
            },
            ]
            \addplot [red] table[x=t,y=ch3,col sep=comma] {data.csv};
        \end{axis}
        
    \end{tikzpicture}
\end{document}

相关内容