pgfplots-如何在一个图中使用来自多个数据文件的相同单一坐标?

pgfplots-如何在一个图中使用来自多个数据文件的相同单一坐标?

我有多个 .csv 文件,其组织方式如下force-stats.txt(请参阅下面的 MWE)。我想根据这些数据文件中的几个单点绘制一个图。这可能吗?到目前为止,使用“单个/相同/数据/点/坐标”等术语的变体进行搜索并没有得到任何有用的结果。

下面的 MWE 显示数据文件中整个列的数据,只是我将它与标签和勾选混合在一起,以按照我想象的方式使用它们。

我想要实现的目标的图片(糟糕的画法)

然后是草图。我希望​​下面的图片能够解释我如何想象数据按它们自己的刻度对齐。注意:为了简洁起见,我省略了所有轴标签,以防万一。:)

在此处输入图片描述

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
amsmath,
tikz,
pgfplots,
pgfplotstable
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}


\begin{filecontents}{force-stats.txt}
Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
Fx,-7.35,2,0.36,6,1,0.4
Fy,-4,0.95,0.76,5,1.91,0.5
Fz,-2,1.42,0.5,4,4,0.5
\end{filecontents}

\begin{filecontents}{force-stats-fake-new.txt}
Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
Fx,-8.35,2,0.36,7,1,0.4
Fy,-4,0.95,0.76,5,1.91,0.5
Fz,-2,1.42,0.5,4,4,0.5
\end{filecontents}

\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}
\pgfplotstableread[col sep=comma]{force-stats-fake-new.txt}{\tableqwertyz}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xlabel={Maximum Fx for A and B},
ylabel={Value stuff},
%
ybar,
%
ymin=0,
%
enlarge x limits={0.3},
%enlarge y limits={0.3},
%
symbolic x coords={Fx,Fy,Fz},
%
xtick=data,
nodes near coords,
xticklabels={{A unit},{B unit}}
]
\addplot+[nodes near coords align={above}] table [col sep=comma, x=Stats, y=Max] {\tableabcdef};
\addlegendentry{\( Fxmax^{\text{A}} \)}
\addplot+[nodes near coords align={above}] table [col sep=comma, x=Stats, y=Max] {\tableqwertyz};
\addlegendentry{\( Fxmax^{\text{B}} \)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案1

以下是使用的建议

x filter/.code={\ifnum\coordindex>0\def\pgfmathresult{}\fi}% use only the first coord

过滤每个表中的第一个坐标并

x expr=\coordindex

对于第一个图,

x expr=\coordindex+1

对于第二个图。对于第三个图...+2必须使用 ,依此类推。请注意\coordindex以 开头0

在此处输入图片描述

代码:

\documentclass[margin=10pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}

%\begin{filecontents}{force-stats.txt}
%Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
%Fx,-7.35,2,0.36,6,1,0.4
%Fy,-4,0.95,0.76,5,1.91,0.5
%Fz,-2,1.42,0.5,4,4,0.5
%\end{filecontents}
%\begin{filecontents}{force-stats-fake-new.txt}
%Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
%Fx,-8.35,2,0.36,7,1,0.4
%Fy,-4,0.95,0.76,5,1.91,0.5
%Fz,-2,1.42,0.5,4,4,0.5
%\end{filecontents}
\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}
\pgfplotstableread[col sep=comma]{force-stats-fake-new.txt}{\tableqwertyz}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xlabel={Maximum $Fx$ for A and B},
    ylabel={Value stuff},
    legend pos=outer north east,
    ybar=-10pt,% -10pt if bar width=10pt
    ymin=0,
    enlarge x limits={0.3},
    xtick={0,1},
    nodes near coords,
    xticklabels={{A unit},{B unit}},
    x filter/.code={\ifnum\coordindex>0\def\pgfmathresult{}\fi}% use only the first coord
  ]
  \addplot table [ x expr=\coordindex, y=Max] {\tableabcdef};
  \addlegendentry{\( Fxmax^{\text{A}} \)}
  \addplot table [x expr=\coordindex+1, y=Max] {\tableqwertyz};
  \addlegendentry{\( Fxmax^{\text{B}} \)}
  \end{axis}
\end{tikzpicture}
\end{document}

相关内容