Tikz:如何分别设置额外刻度线的样式?

Tikz:如何分别设置额外刻度线的样式?

我的问题与这个问题非常相关:https://tex.stackexchange.com/a/201153- 但并不完全相同。

编辑:我有一个带有 2 个额外 y 刻度的图(见下文),但我需要分别设置标签和线条的样式。比如,在下面的例子中,我希望线条的颜色与标签相同,橙色线应该是虚线,蓝色线应该是点线。我该如何实现这一点?

% Simplified MWE

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{groupplots, units}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\begin{groupplot}[
        group style={group size=1 by 1},
        height=8cm,
        width=8cm,
        ymax=100,
        ymin=0,
        ybar,
        x tick label style={rotate=0,anchor=east}
        ]
        \nextgroupplot[
        bar width=9pt,
        symbolic x coords={ResNet18, ResNet50, Inception},
        xtick=data,
        x tick label style={rotate=45,anchor=east},
        extra y ticks={10,50},
        extra y tick label={THIS},
        every extra y tick/.style={
        grid=major, 
        tick0/.initial=orange,
        tick1/.initial=blue,
        yticklabel style={
            color=\pgfkeysvalueof{/pgfplots/tick\ticknum},
            },
        },
        every node near coord/.append style={font={\tiny}}
        ]
    
        \addplot+[ybar, draw=green] plot coordinates {
            (ResNet18,32.58)
            (ResNet50,25.17)
            (Inception,26.92)};
    
        \addplot+[ybar, draw=red] plot coordinates {
            (ResNet18,60.17)
            (ResNet50,65.00)
            (Inception,66.83)};
        \coordinate (top) at (rel axis cs:0,1);
\end{groupplot}
\end{tikzpicture}

\end{document}

例子

答案1

使用不同刻度样式的技巧之所以有效,是因为\ticknum。我不认为网格线有相应的宏。您可以只使用\draw线条和可选的,也可以同时绘制刻度标签,如下所示:

\documentclass[tikz, border=1 cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
        group style={group size=1 by 1},
        height=8cm, width=8cm,
        ymin=0, ymax=100,
        ybar,
       ]
        \nextgroupplot[
        bar width=9pt,
        symbolic x coords={ResNet18, ResNet50, Inception},
        xtick=data,
        x tick label style={rotate=45,anchor=east},
        every node near coord/.append style={font={\tiny}},
        clip=false, axis on top,
        ]    
        \addplot+[ybar, draw=green] plot coordinates {
            (ResNet18,32.58)
            (ResNet50,25.17)
            (Inception,26.92)};
        \addplot+[ybar, draw=red] plot coordinates {
            (ResNet18,60.17)
            (ResNet50,65.00)
            (Inception,66.83)};
        \draw[orange, dashed] ({rel axis cs:0,0} |- {axis cs:ResNet18,10}) node[left]{THIS} -- ({rel axis cs:1,0} |- {axis cs:ResNet18,10});
        \draw[blue, dotted] ({rel axis cs:0,0} |- {axis cs:ResNet18,50}) node[left]{THIS} -- ({rel axis cs:1,0} |- {axis cs:ResNet18,50});
\end{groupplot}
\end{tikzpicture}
\end{document}

带有橙色虚线和蓝色虚线水平线的图表

相关内容