添加额外的 y 刻度,也会在图表的另一部分添加不必要的刻度

添加额外的 y 刻度,也会在图表的另一部分添加不必要的刻度

我想构建一个正态分布概率图。x 轴表示我的箱体的 x 值,y 轴显示刻度标签上的累积概率,但 y 值是相应的正态分布 z 值。我的教授使用的格式是:主网格带有标签,用于表示我的图上看到的 y 值,次网格没有标签,用于 2 到 9 和 91 到 98 的 y 值(代码中注释掉的行)。

无标签次网格是使用额外的 y 刻度实现的。但是,当我将 y 值添加为额外的 y 刻度时,会出现两条次网格线。为什么会出现它们,我该如何摆脱它们?

\documentclass[12pt,table]{report}
\usepackage{tikz,pgfplots}
\begin{document}
    \begin{tikzpicture}[font=\tiny,scale=1.6]
        \begin{axis}[
            xtick pos=left,
            xtick={3.68,3.84,4.00,4.16,4.32,4.48,4.64,4.80,4.96,5.12},
            xticklabels={3.68,3.84,4.00,4.16,4.32,4.48,4.64,4.80,4.96,5.12},
            xticklabel style={shift={(0,0 |- {axis description cs:0,-0.97})}},
            ytick={-2.33,-1.28,-0.84,-0.52,-0.25,0.01,0.25,0.52,0.84,1.28,2.33},
            yticklabels={1.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,99.0},
        %   extra y ticks={-2.05,-1.88,-1.75,-1.64,-1.55,-1.48,-1.41,
-1.34,1.34,1.41,1.55,1.64,1.75,1.88,2.05},   % All the extra ticks that I want
            extra y ticks={-2.05,-1.88,-1.75}, % best shows issue
            extra y tick style={tick style={draw=none},grid=minor},
            minor grid style={line width=.2pt,draw=red!120},
            extra y tick labels={},
            major grid style={line width=.2pt,draw=gray!120},
            grid=both,
            x axis line style={draw=none},
            axis x line=middle,
            axis y line=middle,
            xmin=3.6,
            xmax=5.27,
            ymin=-2.5,
            ymax=2.5,
            domain=3.599:5.2,
            ]
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

对于pgfplots,次要网格是 N 个自动放置的刻度/网格线,放置在每个主要刻度之间。主要刻度放置在指定的刻度位置。

因此,我认为您需要grid=major额外的刻度,并major grid style单独设置额外的刻度:

extra y tick style={
  tick style={draw=none},
  grid=major,
  major grid style={line width=.2pt,draw=red!120}
},

完整代码:

\documentclass[12pt,table]{report}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}[font=\tiny,scale=1.6]
        \begin{axis}[
            xtick pos=left,
            xtick={3.68,3.84,4.00,4.16,4.32,4.48,4.64,4.80,4.96,5.12},
            xticklabels={3.68,3.84,4.00,4.16,4.32,4.48,4.64,4.80,4.96,5.12},
            xticklabel style={shift={(0,0 |- {axis description cs:0,-0.97})}},
            ytick={-2.33,-1.28,-0.84,-0.52,-0.25,0.01,0.25,0.52,0.84,1.28,2.33},
            yticklabels={1.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,99.0},
            extra y ticks={-2.05,-1.88,-1.75,-1.64,-1.55,-1.48,-1.41,
-1.34,1.34,1.41,1.55,1.64,1.75,1.88,2.05},   % All the extra ticks that I want
            extra y tick style={tick style={draw=none},grid=major, major grid style={line width=.2pt,draw=red!120}},
            extra y tick labels={},
            major grid style={line width=.2pt,draw=gray!120},
            grid=both,
            x axis line style={draw=none},
            axis x line=middle,
            axis y line=middle,
            xmin=3.6,
            xmax=5.27,
            ymin=-2.5,
            ymax=2.5,
            domain=3.599:5.2,
            ]
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容