我想从图表中条形图各自部分显示的 Cd、Cr 和 Ni 列中获取值。我希望它们在每个部分的中心对齐。我已使用nodes near coords
和\addplot
坐标来显示值。前三个适合正确的位置,但它们不在中心。此外,后续\addplot
显示在错误的位置。我能得到一些关于这个问题的帮助吗?提前感谢您的帮助。
代码是
\documentclass[border={0pt,0pt,0pt,0pt}]{standalone}
\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={-15}},
every axis label = {font=\sansmath\sffamily},
legend style = {font=\sansmath\sffamily},
label style = {font=\sansmath\sffamily},
}
\begin{document}
\pgfplotstableread{
Species Cd Cr Ni
V.\space c.\space cochiensis 3.67 2.16 2.27
L.\space teres 2.76 0 0
L.\space s.\space claibornensis 2.52 0 1.94
A.\space pectorosa 2.34 0 1.34
L.\space subviridis 2.34 0 0
U.\space imbecillis 2.26 0.76 1.44
V.\space vibex 2.21 0 1.58
C.\space fluminalis 1.39 0 0
P.\space casertanum 1.17 0 0
P.\space compressum 1.08 0 0
C.\space rhizophorae 0.40 0 0
P.\space favidens 0.24 0 0
L.\space marginalis 0.10 0 0
M.\space nervosa 0 1.08 1.82
R.\space cuneata 0 0.90 0
A.\space plicata 0 0.85 1.69
L.\space siliquoidea 0 0.80 1.42
M.\space falcata 0 0.42 1.63
H.\space perovalis 0 0 1.56
V.\space nebulosa 0 0 1.35
H.\space cumingii 0 -0.81 0
D.\space chilensis 0 -1.09 0
}\testdata
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
xmin=-3,
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=11cm,
width=11.9cm,
enlarge y limits={abs=0.725},
enlarge x limits={abs=0.725},
xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$},
ytick=\empty,
legend style={at={(0.8,0.6)},anchor=north west, draw=none},
nodes near coords,
visualization depends on=x\as\myx,
nodes near coords align={center},
every node near coord/.append style={anchor ={180-sign(\myx)*180},font=\footnotesize},
point meta = explicit symbolic
]
\addlegendimage{empty legend},
\addplot [fill=black!90!black!45] table [x=Cr, y expr=\coordindex] {\testdata}; % "First" column against the data index
\addplot [fill=black!90!black!20] table [x=Cd, y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=Ni, y expr=\coordindex] {\testdata};
\addplot [only marks,mark size=0pt,point meta=explicit symbolic,
nodes near coords,nodes near coords style={anchor=east,font =\small\itshape}]
table[x expr={-1*ifthenelse(\thisrow{Cd}+\thisrow{Cr}+\thisrow{Ni}>0,
\thisrow{Cd}+\thisrow{Cr}+\thisrow{Ni},0)},y expr=\coordindex,meta=Species] {\testdata};
\addplot[black] coordinates{(2.16,0)[2.16]};%shows up well but not 'center'.
\addplot[black] coordinates{(3.67,0)[3.67]};%shows up well but not 'center'.
\addplot[black] coordinates{(2.27,0)[2.27]};%shows up well but not 'center'.
%
%all other \addplot entries from table.
%
\addplot[black] coordinates{(0,20)};
\addplot[black] coordinates{(-0.81,20)[-0.81]};%shows up on the wrong position.
\addplot[black] coordinates{(0,20)};
\addplot[black] coordinates{(0,21)};
\addplot[black] coordinates{(-1.09,21)[-1.09]};%shows up on the wrong position.
\addplot[black] coordinates{(0,21)};
\addlegendentry{\hspace{-0.85cm}\textbf\small{Metals}},
\addlegendentry{Cr(VI)},
\addlegendentry{Cd(II)},
\addlegendentry{Ni(II)},
\end{axis}
\end{tikzpicture}
\end{document}
答案1
在你提出的两个问题中,我认为其中一个问题可以相当容易地解决。问题是这些堆叠图。但是,您似乎只想显示值x
。然后,如果条太小,您似乎想抑制节点。这可以通过样式来实现
\pgfplotsset{my meta/.style={point meta=\thisrow{#1},
every node near coord/.append style={%
opacity={ifthenelse(abs(\csname my#1\endcsname)><threshold>,1,0)}}}}
在哪里<threshold>
0.5。对于水平居中,我使用了以下变体此解决方案。
\documentclass[border={0pt,0pt,0pt,0pt}]{standalone}
\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={-15}},
every axis label = {font=\sansmath\sffamily},
legend style = {font=\sansmath\sffamily},
label style = {font=\sansmath\sffamily},
}
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(10^\pgfplots@data@scale@trafo@EXPONENT@x)*%
\csname my#1\endcsname/2*\pgfplots@x@veclength)}
\pgfkeys{/pgf/fpu=false}
},
nodes near coords horizontally centered/.style={
every node near coord/.append style={
/pgfplots/calculate offset=#1,
xshift=-\testmacro
},
}
}
\makeatother
\begin{document}
\pgfplotstableread{
Species Cd Cr Ni
V.\space c.\space cochiensis 3.67 2.16 2.27
L.\space teres 2.76 0 0
L.\space s.\space claibornensis 2.52 0 1.94
A.\space pectorosa 2.34 0 1.34
L.\space subviridis 2.34 0 0
U.\space imbecillis 2.26 0.76 1.44
V.\space vibex 2.21 0 1.58
C.\space fluminalis 1.39 0 0
P.\space casertanum 1.17 0 0
P.\space compressum 1.08 0 0
C.\space rhizophorae 0.40 0 0
P.\space favidens 0.24 0 0
L.\space marginalis 0.10 0 0
M.\space nervosa 0 1.08 1.82
R.\space cuneata 0 0.90 0
A.\space plicata 0 0.85 1.69
L.\space siliquoidea 0 0.80 1.42
M.\space falcata 0 0.42 1.63
H.\space perovalis 0 0 1.56
V.\space nebulosa 0 0 1.35
H.\space cumingii 0 -0.81 0
D.\space chilensis 0 -1.09 0
}\testdata
\begin{tikzpicture}
\pgfplotsset{my meta/.style={point meta=\thisrow{#1},
nodes near coords horizontally centered=#1,
every node near coord/.append style={%
opacity={ifthenelse(abs(\csname my#1\endcsname)>0.5,1,0)}}}}
\begin{axis}[
xbar stacked,
xmin=-3,
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=11cm,
width=11.9cm,
enlarge y limits={abs=0.725},
enlarge x limits={abs=0.725},
xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$},
ytick=\empty,
legend style={at={(0.8,0.6)},anchor=north west, draw=none},
nodes near coords,
visualization depends on=x\as\myx,
visualization depends on=\thisrow{Cr}\as\myCr,
visualization depends on=\thisrow{Cd}\as\myCd,
visualization depends on=\thisrow{Ni}\as\myNi,
nodes near coords align={center},
every node near coord/.append style={%anchor={180-sign(\myx)*180},
font=\tiny},
]
\addlegendimage{empty legend},
\addplot [fill=black!90!black!45,my meta=Cr] table [x=Cr, y expr=\coordindex] {\testdata}; % "First" column against the data index
\addplot [fill=black!90!black!20,my meta=Cd] table [x=Cd, y expr=\coordindex] {\testdata};
\addplot [fill=black!5,my meta=Ni] table [x=Ni, y expr=\coordindex] {\testdata};
\addplot [only marks,mark size=0pt,point meta=explicit symbolic,
nodes near coords,nodes near coords style={anchor=east,font=\small\itshape,opacity=1}]
table[x expr={-1*ifthenelse(\thisrow{Cd}+\thisrow{Cr}+\thisrow{Ni}>0,
\thisrow{Cd}+\thisrow{Cr}+\thisrow{Ni},0)},y expr=\coordindex,meta=Species] {\testdata};
\addlegendentry{\hspace{-0.85cm}\textbf\small{Metals}},
\addlegendentry{Cr(VI)},
\addlegendentry{Cd(II)},
\addlegendentry{Ni(II)},
\end{axis}
\end{tikzpicture}
\end{document}