将条形图与 Y 轴上的误差线分组对齐

将条形图与 Y 轴上的误差线分组对齐

我正在尝试将其对齐到 3 个组中,但AAAAAAAA组中的一个条一直偏离对齐。

注:我已检查“使用 pgfplots 制作的分组条形图”“带有组的条形图(组内条彼此相邻)和组之间的空白(包括误差线)[重复]”“如何在 pgfplots 中创建不同大小的条形图组?”但他们没有回答我的问题——我们的数据输入格式不同。

梅威瑟:

标签

\definecolor{bblue}{HTML}{82B1FF}
\definecolor{rred}{HTML}{FF8A80}

\begin{filecontents}{testdata.csv}
ALB,VAL,ERR,COR
AA,20.61,0.32,bblue
AA,20.88,0.55,bblue
AA,20.41,0.29,bblue
AAA,26.22,0.94,bblue
AAA,26.43,0.45,bblue
AAA,26.86,0.61,bblue
AAAA,29.91,0.76,bblue
AAAA,30.39,0.11,bblue
AAAA,30.11,0.49,bblue
AAAAA,34.71,0.43,rred
AAAAA,34.87,0.99,rred
AAAAA,34.81,0.26,rred
AAAAAA,40.33,0.64,rred
AAAAAA,40.18,0.03,rred
AAAAAA,40.4,0.83,rred
AAAAAAA,45.77,0.47,rred
AAAAAAA,45.93,0.95,rred
AAAAAAA,45.35,0.3,rred
AAAAAAAA,50.35,0.13,rred
AAAAAAAA,50.34,0.85,rred
AAAAAAAA,50.66,0.55,rred
\end{filecontents}
\pgfplotstableread[col sep=comma]{testdata.csv}\datatable
\makeatletter
\pgfplotsset{
    /pgfplots/flexible yticklabels from table/.code n args={3}{%
        \pgfplotstableread[#3]{#1}\coordinate@table
        \pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@yticklabels
        \let\pgfplots@yticklabel=\pgfplots@user@ticklabel@list@y
    }
}
\makeatother
\begin{figure}

\begin{tikzpicture}
\begin{axis}[
    ylabel=A labels,  y label style={at={(-0.1,0.5)}}, 
    y = -0.3cm,
    ybar, %=2*\pgflinewidth,
    draw opacity=0.5,
    bar width=6pt,
    enlarge y limits={abs=7pt},
    xbar, 
    width=.36\textwidth,
    xlabel= unit,
    %flexible xticklabels from table={testdata.csv}{ALB}{col sep=comma}, 
    flexible yticklabels from table={testdata.csv}{ALB}{col sep=comma},
    yticklabel style={font=\small,xshift=0.5ex,  anchor=east},  
    %xtick=data
    ytick=data,
    xmajorgrids
]
%\pgfplotsinvokeforeach {0}{
    \addplot
    %style={color=cyan,fill=bblue,mark=none};
    plot [error bars/.cd, x dir=both,x explicit ] 
        table[y expr={(\coordindex) - mod(\coordindex,3)/8},x=VAL, x error=ERR ] {\datatable};

\end{axis}

\end{tikzpicture} 
\end{figure}

答案1

(更新 21.10.17。)该问题是由浮点库中的一个错误引起的:https://sourceforge.net/p/pgfplots/bugs/187/该问题已由 Christian Feuersänger 修复,当的下一个版本发布时,pgf代码pgfplots应该可以正常工作,而无需采用下面描述的解决方法。


可能是被18视为17.99999或其他东西,因此mod给出了错误的输出。

如果更改为,mod(\coordindex+0.001,3)输出将符合预期。

无关评论:

  • 您拥有两者,ybar并且xbar在选项中可以将axis其删除。ybar
  • \addplot plot[..正在将 TikZ 语法(\draw plot)与pgfplots语法混合,\addplot +[..我想你想要。
  • 如图所示,你实际上不需要这些flexible yticklabels东西,yticklabels from table={\datatable}{ALB}已经足够好了。

代码部分输出

\documentclass{article}
\usepackage{pgfplotstable,filecontents}

\begin{document}

\definecolor{bblue}{HTML}{82B1FF}
\definecolor{rred}{HTML}{FF8A80}

\begin{filecontents}{testdata.csv}
ALB,VAL,ERR,COR
AA,20.61,0.32,bblue
AA,20.88,0.55,bblue
AA,20.41,0.29,bblue
AAA,26.22,0.94,bblue
AAA,26.43,0.45,bblue
AAA,26.86,0.61,bblue
AAAA,29.91,0.76,bblue
AAAA,30.39,0.11,bblue
AAAA,30.11,0.49,bblue
AAAAA,34.71,0.43,rred
AAAAA,34.87,0.99,rred
AAAAA,34.81,0.26,rred
AAAAAA,40.33,0.64,rred
AAAAAA,40.18,0.03,rred
AAAAAA,40.4,0.83,rred
AAAAAAA,45.77,0.47,rred
AAAAAAA,45.93,0.95,rred
AAAAAAA,45.35,0.3,rred
AAAAAAAA,50.35,0.13,rred
AAAAAAAA,50.34,0.85,rred
AAAAAAAA,50.66,0.55,rred
\end{filecontents}
\pgfplotstableread[col sep=comma]{testdata.csv}\datatable

\begin{figure}

\begin{tikzpicture}
\begin{axis}[
    ylabel=A labels,
    y label style={at={(-0.1,0.5)}}, 
    y = -0.3cm,
    xbar, %=2*\pgflinewidth,
    draw opacity=0.5,
    bar width=6pt,
    enlarge y limits={abs=7pt},
    width=.36\textwidth,
    xlabel= unit,
    yticklabels from table={\datatable}{ALB},
    yticklabel style={font=\small,xshift=0.5ex,  anchor=east},  
    ytick=data,
    xmajorgrids
]
%\pgfplotsinvokeforeach {0}{
    \addplot
    %style={color=cyan,fill=bblue,mark=none};
    +[error bars/.cd, x dir=both,x explicit] 
        table[y expr={(\coordindex) - mod(\coordindex+0.001,3)/8},x=VAL, x error=ERR ] {\datatable};

\end{axis}

\end{tikzpicture} 
\end{figure}
\end{document}

相关内容