如何单独自定义直方图中的刻度标记和标签

如何单独自定义直方图中的刻度标记和标签

我需要在直方图中标记三个 x 值。我认为一种方法是引入extra x ticks并自定义major tick length厚度,以便它们更显眼地显示出来。是否可以为每个值分配不同的长度和厚度,如果可以,我可以得到一些帮助吗?

此外,我希望每个刻度标签都不同。当我引入时,它xticklabel会为所有额外的刻度分配相同的标签。我如何为每个额外的 x 刻度分配不同的标签?

这是我迄今为止的代码:

\documentclass[border=0.2mm]{standalone}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{pgfplots}
\pgfplotsset{ compat=1.9, every axis/.style={axis on top}}
\usetikzlibrary{patterns}
\makeatletter

\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ybar,
    width=7.5cm,
    ymax=10,
    xmax=3,    
    xticklabel style = {font=\sansmath\sffamily},
    yticklabel style = {font=\sansmath\sffamily},
    xtick pos=left,
    ytick pos=left,
    extra x ticks = {1.10,1.38,1.62},
    extra x tick style={
    xticklabel={\footnotesize SC},
    xticklabel style={above, rotate=45,ultra thick,yshift=65, xshift=75},
    tick align=inside,
    major tick length=95},      
    every axis label = {font=\sansmath\sffamily},
    xlabel={xxxx},
    ylabel={yyyy},
    legend style = {anchor=north east, 
                   nodes={scale=0.75,transform shape},
                   font=\sansmath\sffamily},
    label style = {font=\sansmath\sffamily},    
    enlarge y limits=-0.5,
    ]             
    \addplot+[hist={data=x,bins=9,data max=2,data min=-2.6},black!30, fill=black!30]
            table [y expr=1] {

2
0.5
2
-1.2
-0.3
-2
-1.55
0.2
-0.1
-1.4
1.1
-2
-2
-1
-2
0.5
1.7
2
0.33
-2
0.22
-2
-1
-1
0.2
1
2
-1
1.1
2
-1.3
-1.22
-2
0.33
0.22
1
0.1          
            };
            \addlegendimage{empty legend},
\addlegendentry{aaaa},
\end{axis}
\end{tikzpicture}
\end{document}

这就是我想要实现的目标:

在此处输入图片描述

提前感谢您的帮助!

答案1

我只需“手动”添加它们,即使用循环。

\documentclass[border=0.2mm]{standalone}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{pgfplots}
\pgfplotsset{ compat=1.9, every axis/.style={axis on top}}
\usetikzlibrary{patterns}
\makeatletter

\begin{document}

\begin{tikzpicture} 
    \begin{axis}[ybar,
    width=7.5cm,
    ymax=10,
    xmax=3,    
    xticklabel style = {font=\sansmath\sffamily},
    yticklabel style = {font=\sansmath\sffamily},
    xtick pos=left,
    ytick pos=left,
    every axis label = {font=\sansmath\sffamily},
    xlabel={xxxx},
    ylabel={yyyy},
    legend style = {anchor=north east, 
                   nodes={scale=0.75,transform shape},
                   font=\sansmath\sffamily},
    label style = {font=\sansmath\sffamily},    
    enlarge y limits=-0.5,
    ]             
    \addplot+[hist={data=x,bins=9,data max=2,data min=-2.6},black!30, fill=black!30]
            table [y expr=1] {

2
0.5
2
-1.2
-0.3
-2
-1.55
0.2
-0.1
-1.4
1.1
-2
-2
-1
-2
0.5
1.7
2
0.33
-2
0.22
-2
-1
-1
0.2
1
2
-1
1.1
2
-1.3
-1.22
-2
0.33
0.22
1
0.1          
            };
            \addlegendimage{empty legend},
\addlegendentry{aaaa},
\foreach \X [count=\Y] in {1.10,1.38,1.62}
{\edef\temp{\noexpand\draw[ultra thick] (axis cs:\X,0) -- (axis cs:\X,8.8-\Y) node[anchor=south
west,xshift=-2ex,font=\noexpand\sffamily]{label \Y} ;}
\temp} % axis cs: is not needed for pgfplots compat=1.11 or higher
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容