为 yaxis 刻度标签分配特定的移位值并保持“明显”的分离距离

为 yaxis 刻度标签分配特定的移位值并保持“明显”的分离距离

我想使 yaxis 上的特定刻度标签移动足够远,以便它们不会与堆积条形图中的条形重叠,但同时我希望所有标签保持相同的“明显”分离距离(最后一个条件我可以适应其他选项)。

我还希望得到额外的帮助,使 xaxis 与图像边框分离(这样看起来更对称)。

下面我附上了一张图片来说明我需要修复的问题 在此处输入图片描述

我相信可以通过实现“可视化取决于”来实现,但不确定如何在代码中引入这一点。

    \documentclass[border={0pt,0pt,0pt,0pt}]{standalone}%[a4paper]{article}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{tikz,pgfplotstable}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.8,
 every axis/.style={axis on top},
 tick label style = {font=\sansmath\sffamily\small\itshape,xshift={-19}},
 every axis label = {font=\sansmath\sffamily},
 legend style = {font=\sansmath\sffamily},
 label style = {font=\sansmath\sffamily}, 
 }
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
Species     A       B       C
text1       3.67    2.16    2.27
text2       2.76    0       0
text3       2.52    0       1.94
text4       0       0.90    0
text5       0       0.85    1.69
text6       0       -3.81   0
text7       0       -1.09   0



}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=-8,         % Start x axis at 0
            xmax=8,         
            xticklabel style=transparent,    
            xtick style=transparent,
            ytick style=transparent,            
            axis y line*=middle,
            every inner y axis line/.append style = {dotted, ultra thick},
            height=5cm,
            width=11.9cm,
            enlarge y limits={abs=0.925},
            enlarge x limits={abs=0.925},            
            xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$},             
            ytick=data,     % Use as many tick labels as y coordinates
            yticklabels from table={\testdata}{Species}  % Get the labels from the Label column of the \datatable
]
\addplot [fill=black!20!black!40] table [x=A, meta=Species,y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=black!60!black!20] table [x=B, meta=Species,y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=C, meta=Species,y expr=\coordindex] {\testdata};
\end{axis}
\end{tikzpicture}

\end{document} 

提前感谢您的帮助,这个社区太棒了!

答案1

这里有一些可以按照您想要的方式附加这些文本的东西。不幸的是,此时您的代码似乎无法运行,因为表格没有您使用的列。所以我做了一些东西。关键点是

\addplot [only marks,mark size=0pt,nodes near
    coords=text~\the\numexpr\coordindex+1,nodes near coords style={anchor=east}] 
    table[x expr={-0.01},y expr=\coordindex]  {\testdata};

自动将节点放置在您喜欢的位置。

\documentclass[border={0pt,0pt,0pt,0pt}]{standalone}%[a4paper]{article}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{tikz,pgfplotstable}
\pgfplotsset{compat=1.16,
 every axis/.style={axis on top},
 tick label style = {font=\sansmath\sffamily\small\itshape,xshift={-19}},
 every axis label = {font=\sansmath\sffamily},
 legend style = {font=\sansmath\sffamily},
 label style = {font=\sansmath\sffamily}}
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
LABEL       A       B       C
text1       3.67    2.16    2.27
text2       2.76    0       0
text3       2.52    0       1.94
text4       0       0.90    0
text5       0       0.85    1.69
text6       0       -3.81   0
text7       0       -1.09   0
}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=-8,         % Start x axis at 0
            xmax=8,         
            xticklabel style=transparent,    
            xtick style=transparent,
            ytick style=transparent,            
            axis y line*=middle,
            every inner y axis line/.append style = {dotted, ultra thick},
            height=5cm,
            width=11.9cm,
            enlarge y limits={abs=0.925},
            enlarge x limits={abs=0.925},            
            xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$},             
            ytick=\empty,     % Use as many tick labels as y coordinates
            %yticklabels from table={\testdata}{Species}  % Get the labels from the Label column of the \datatable
            visualization depends on=\thisrow{LABEL}\as\myX,
]
\addplot [fill=black!20!black!40] table [x=A, y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=black!60!black!20] table [x=B, y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=C, y expr=\coordindex] {\testdata};
\addplot [only marks,mark size=0pt,nodes near
    coords=text~\the\numexpr\coordindex+1,nodes near coords style={anchor=east}] 
    table[x expr={-0.01},y expr=\coordindex]  {\testdata};
\end{axis}
\end{tikzpicture}
\end{document} 

在此处输入图片描述

或者对于您更新的示例:从行中读取内容Species并使用另一个技巧。

\documentclass[border={0pt,0pt,0pt,0pt}]{standalone}%[a4paper]{article}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{tikz,pgfplotstable}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.8,
 every axis/.style={axis on top},
 tick label style = {font=\sansmath\sffamily\small\itshape,xshift={-19}},
 every axis label = {font=\sansmath\sffamily},
 legend style = {font=\sansmath\sffamily},
 label style = {font=\sansmath\sffamily}, 
 }
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
Species     A       B       C
text1       3.67    2.16    2.27
text2       2.76    0       0
text3       2.52    0       1.94
text4       0       0.90    0
text5       0       0.85    1.69
text6       0       -3.81   0
text7       0       -1.09   0



}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=-8,         % Start x axis at 0
            xmax=8,         
            xticklabel style=transparent,    
            xtick style=transparent,
            ytick style=transparent,            
            axis y line*=middle,
            every inner y axis line/.append style = {dotted, ultra thick},
            height=5cm,
            width=11.9cm,
            enlarge y limits={abs=0.925},
            enlarge x limits={abs=0.925},            
            xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$}, 
            ytick=\empty            
]
\addplot [fill=black!20!black!40] table [x=A, y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=black!60!black!20] table [x=B, y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=C, y expr=\coordindex] {\testdata};
\addplot [only marks,mark size=0pt,point meta=explicit symbolic,
    nodes near coords,nodes near coords style={anchor=east}] 
    table[x expr={-1*ifthenelse(\thisrow{A}+\thisrow{B}+\thisrow{C}>0,
        \thisrow{A}+\thisrow{B}+\thisrow{C},0)},y expr=\coordindex,meta=Species]  {\testdata};
\end{axis}
\end{tikzpicture}

在此处输入图片描述

相关内容