我正在尝试使用pgfplots
绘制数据的线性回归图。如果我的数据集中有nan
,则不会生成拟合,并显示警告:
NOTE: coordinate (1,nan) has been dropped because it is unbounded (in y).
(see also unbounded coords=jump).
以及针对该集合中其他点的类似警告。
如果我设置一个ignore chars
并将更改nan
为该字符,则编译会失败并出现错误:
! Package pgfplots Error: input table '<inline_table>' has an unbalanced number
of columns in row '2' (expected '2' cols; got '1'). Maybe the input table is c
orrupted? If you need unbalanced data, consider using 'nan' in empty cells (per
haps combined with 'unbounded coords=jump').
以下 MWE 重现了该问题:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table[x=X,y={create col/linear regression={y=Y}}]{
X Y
1 nan
2 1
3 2
4 3
};
\end{axis}
\end{tikzpicture}
\end{document}
我怎样才能nan
在我的数据集中生成一个拟合,或者我怎样才能忽略一个字符?
答案1
目前,PGFPlots 无法nan
在计算回归线时处理值。不过,添加此功能非常简单,只需在(相当长的)内部宏中添加两行即可。
如果你在前言中放入以下代码\usepackage{pgfplots}
,你的代码将会起作用:
\makeatletter
% #1: keys
\def\pgfplotstable@linear@regression#1{%
\begingroup
\pgfqkeys{/pgfplots/table/create col/linear regression}{/pgf/fpu,#1}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/x}{\pgfplotstable@xsrc}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/y}{\pgfplotstable@ysrc}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/table}{\pgfplotstable@table}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/xmode}{\pgfplotstable@xmode}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/ymode}{\pgfplotstable@ymode}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/variance}{\pgfplotstable@variance@colname}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/variance list}{\pgfplotstable@variance@list}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/variance src}{\pgfplotstable@variance@table}%
%
\ifx\pgfplotstable@table\pgfutil@empty
\pgfutil@ifundefined{pgfplotstablename}{}{% query the name of the actual table struct
\let\pgfplotstable@table=\pgfplotstablename
}%
\fi
\ifx\pgfplotstable@table\pgfutil@empty
\pgfplots@error{Sorry, I couldn't determine a value for create col/linear regression/table. Which table should I load?}%
\fi
\ifx\pgfplotstable@xsrc\pgfutil@empty
\pgfplotsifinaddplottablestruct{%
\pgfutil@ifundefined{pgfplots@plot@tbl@x}{}{%
\let\pgfplotstable@xsrc=\pgfplots@plot@tbl@x
\ifx\pgfplotstable@ysrc\pgfutil@empty
\pgfplotstablegetcolsof\pgfplots@table
\ifnum\pgfplotsretval=2
\else
\pgfplotsthrow{invalid argument}{\pgfplotstable@ysrc}{Sorry, I don't which column should be used as `y' for the linear regression. Please provide 'linear regression={y=<colname>}'}\pgfeov%
\fi
\fi
}%
}{}%
\fi
\ifx\pgfplotstable@xsrc\pgfutil@empty
\def\pgfplotstable@xsrc{[index]0}%
\fi
\ifx\pgfplotstable@ysrc\pgfutil@empty
\def\pgfplotstable@ysrc{[index]1}%
\fi
%
\t@pgfplots@toka=\expandafter{\pgfplotstable@table}%
\t@pgfplots@tokb=\expandafter{\pgfplotstable@xsrc}%
\t@pgfplots@tokc=\expandafter{\pgfplotstable@ysrc}%
\edef\pgfplots@loc@TMPa{{\the\t@pgfplots@tokb}\noexpand\of{\the\t@pgfplots@toka}}%
\edef\pgfplots@loc@TMPb{{\the\t@pgfplots@tokc}\noexpand\of{\the\t@pgfplots@toka}}%
\expandafter\pgfplotstablegetcolumn\pgfplots@loc@TMPa\to\pgfplotstable@X
\expandafter\pgfplotstablegetcolumn\pgfplots@loc@TMPb\to\pgfplotstable@Y
%
\edef\pgfplotstable@xmode{\pgfplotstable@xmode}%
\expandafter\pgfplotstable@linear@regression@prepare@mode\expandafter{\pgfplotstable@xmode}{x}%%
\edef\pgfplotstable@ymode{\pgfplotstable@ymode}%
\expandafter\pgfplotstable@linear@regression@prepare@mode\expandafter{\pgfplotstable@ymode}{y}%%
%
\ifx\pgfplotstable@variance@list\pgfutil@empty
% check 'variance' key (loaded from table)
\pgfplotslistnewempty\pgfplotstable@VARIANCE
\ifx\pgfplotstable@variance@colname\pgfutil@empty
\else
\ifx\pgfplotstable@variance@table\pgfutil@empty
\t@pgfplots@toka=\expandafter{\pgfplotstable@table}%
\t@pgfplots@tokb=\expandafter{\pgfplotstable@variance@colname}%
\edef\pgfplots@loc@TMPa{{\the\t@pgfplots@tokb}\noexpand\of{\the\t@pgfplots@toka}}%
\expandafter\pgfplotstablegetcolumn\pgfplots@loc@TMPa\to\pgfplotstable@VARIANCE
\else
\t@pgfplots@toka=\expandafter{\pgfplotstable@variance@colname}%
\t@pgfplots@tokb=\expandafter{\pgfplotstable@variance@table}%
\edef\pgfplotstable@loc@TMPa{%
\noexpand\pgfplotstablegetcolumn{\the\t@pgfplots@toka}\noexpand\of{\the\t@pgfplots@tokb}\noexpand\to\noexpand\pgfplotstable@VARIANCE}%
\pgfplotstable@loc@TMPa
\fi
\fi
\else
% load from list:
\expandafter\pgfplotslistnew\expandafter\pgfplotstable@VARIANCE\expandafter{\pgfplotstable@variance@list}%
\fi
%
\pgfplotslistnewempty\pgfplotstable@Xparsed
%
\pgfmathfloatcreate{0}{0.0}{0}%
\let\pgfplotstable@S=\pgfmathresult
\let\pgfplotstable@Sxx=\pgfmathresult
\let\pgfplotstable@Sx=\pgfmathresult
\let\pgfplotstable@Sy=\pgfmathresult
\let\pgfplotstable@Sxy=\pgfmathresult
\pgfutil@loop
\pgfplotslistcheckempty\pgfplotstable@X
\ifpgfplotslistempty
\pgfplots@loop@CONTINUEfalse
\else
\pgfplots@loop@CONTINUEtrue
\fi
\ifpgfplots@loop@CONTINUE
\pgfplotslistpopfront\pgfplotstable@X\to\pgfplotstable@x
\pgfplotslistpopfront\pgfplotstable@Y\to\pgfplotstable@y
%
\pgfplotstableparsex{\pgfplotstable@x}%
\let\pgfplotstable@x=\pgfmathresult
\expandafter\pgfplotslistpushback\pgfmathresult\to\pgfplotstable@Xparsed
\pgfplotstableparsey{\pgfplotstable@y}%
\let\pgfplotstable@y=\pgfmathresult
\pgfmathfloatifflags{\pgfplotstable@y}{3}{}{% <---- This is new. The "3" stands for "nan"
%
\pgfplotslistcheckempty\pgfplotstable@VARIANCE
\ifpgfplotslistempty
\pgfmathfloatcreate{1}{1.0}{0}%
\let\pgfplotstable@invsqr=\pgfmathresult
\else
\pgfplotslistpopfront\pgfplotstable@VARIANCE\to\pgfplotstable@variance
\pgfmathfloatparsenumber{\pgfplotstable@variance}%
\let\pgfplotstable@variance=\pgfmathresult
\pgfmathfloatmultiply@{\pgfplotstable@variance}{\pgfplotstable@variance}%
\let\pgfplotstable@sqr=\pgfmathresult
\pgfmathfloatreciprocal@{\pgfplotstable@sqr}%
\let\pgfplotstable@invsqr=\pgfmathresult
\fi
%
\pgfmathfloatadd@{\pgfplotstable@S}{\pgfplotstable@invsqr}%
\let\pgfplotstable@S=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplotstable@invsqr}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sx}{\pgfplots@table@accum}%
\let\pgfplotstable@Sx=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplots@table@accum}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sxx}{\pgfplots@table@accum}%
\let\pgfplotstable@Sxx=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@y}{\pgfplotstable@invsqr}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sy}{\pgfplots@table@accum}%
\let\pgfplotstable@Sy=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplots@table@accum}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sxy}{\pgfplots@table@accum}%
\let\pgfplotstable@Sxy=\pgfmathresult
}% <---- This is new.
\pgfutil@repeat
%
\pgfmathparse{\pgfplotstable@S * \pgfplotstable@Sxx - \pgfplotstable@Sx *\pgfplotstable@Sx}%
\let\pgfplotstable@delta=\pgfmathresult
%
\pgfmathparse{(\pgfplotstable@S * \pgfplotstable@Sxy - \pgfplotstable@Sx * \pgfplotstable@Sy) / \pgfplotstable@delta}%
\let\pgfplotstable@a=\pgfmathresult
%
\pgfmathparse{(\pgfplotstable@Sxx * \pgfplotstable@Sy - \pgfplotstable@Sx * \pgfplotstable@Sxy) / \pgfplotstable@delta}%
\let\pgfplotstable@b=\pgfmathresult
%
\pgfplotslistnewempty\pgfplotstable@RESULT
\pgfplotslistforeachungrouped\pgfplotstable@Xparsed\as\pgfplotstable@x{%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplotstable@a}%
\let\pgfplotstable@tmp=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@tmp}{\pgfplotstable@b}%
\ifx\pgfplotstableparseylogbase\pgfutil@empty
\else
\pgfplotstableparseyinv@{\pgfmathresult}%
\fi
\pgfmathfloattosci{\pgfmathresult}%
\expandafter\pgfplotslistpushback\pgfmathresult\to\pgfplotstable@RESULT
}%
\pgfmathfloattosci\pgfplotstable@a
\let\pgfplotstable@a=\pgfmathresult
%
\pgfmathfloattosci\pgfplotstable@b
\let\pgfplotstable@b=\pgfmathresult
%
\global\let\pgfplotstableregressiona\pgfplotstable@a%
\global\let\pgfplotstableregressionb\pgfplotstable@b%
\let\pgfplotsretval=\pgfplotstable@RESULT
\pgfmath@smuggleone\pgfplotsretval
\endgroup
}%
\makeatother
完整代码:
\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}
\makeatletter
% #1: keys
\def\pgfplotstable@linear@regression#1{%
\begingroup
\pgfqkeys{/pgfplots/table/create col/linear regression}{/pgf/fpu,#1}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/x}{\pgfplotstable@xsrc}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/y}{\pgfplotstable@ysrc}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/table}{\pgfplotstable@table}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/xmode}{\pgfplotstable@xmode}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/ymode}{\pgfplotstable@ymode}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/variance}{\pgfplotstable@variance@colname}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/variance list}{\pgfplotstable@variance@list}%
\pgfkeysgetvalue{/pgfplots/table/create col/linear regression/variance src}{\pgfplotstable@variance@table}%
%
\ifx\pgfplotstable@table\pgfutil@empty
\pgfutil@ifundefined{pgfplotstablename}{}{% query the name of the actual table struct
\let\pgfplotstable@table=\pgfplotstablename
}%
\fi
\ifx\pgfplotstable@table\pgfutil@empty
\pgfplots@error{Sorry, I couldn't determine a value for create col/linear regression/table. Which table should I load?}%
\fi
\ifx\pgfplotstable@xsrc\pgfutil@empty
\pgfplotsifinaddplottablestruct{%
\pgfutil@ifundefined{pgfplots@plot@tbl@x}{}{%
\let\pgfplotstable@xsrc=\pgfplots@plot@tbl@x
\ifx\pgfplotstable@ysrc\pgfutil@empty
\pgfplotstablegetcolsof\pgfplots@table
\ifnum\pgfplotsretval=2
\else
\pgfplotsthrow{invalid argument}{\pgfplotstable@ysrc}{Sorry, I don't which column should be used as `y' for the linear regression. Please provide 'linear regression={y=<colname>}'}\pgfeov%
\fi
\fi
}%
}{}%
\fi
\ifx\pgfplotstable@xsrc\pgfutil@empty
\def\pgfplotstable@xsrc{[index]0}%
\fi
\ifx\pgfplotstable@ysrc\pgfutil@empty
\def\pgfplotstable@ysrc{[index]1}%
\fi
%
\t@pgfplots@toka=\expandafter{\pgfplotstable@table}%
\t@pgfplots@tokb=\expandafter{\pgfplotstable@xsrc}%
\t@pgfplots@tokc=\expandafter{\pgfplotstable@ysrc}%
\edef\pgfplots@loc@TMPa{{\the\t@pgfplots@tokb}\noexpand\of{\the\t@pgfplots@toka}}%
\edef\pgfplots@loc@TMPb{{\the\t@pgfplots@tokc}\noexpand\of{\the\t@pgfplots@toka}}%
\expandafter\pgfplotstablegetcolumn\pgfplots@loc@TMPa\to\pgfplotstable@X
\expandafter\pgfplotstablegetcolumn\pgfplots@loc@TMPb\to\pgfplotstable@Y
%
\edef\pgfplotstable@xmode{\pgfplotstable@xmode}%
\expandafter\pgfplotstable@linear@regression@prepare@mode\expandafter{\pgfplotstable@xmode}{x}%%
\edef\pgfplotstable@ymode{\pgfplotstable@ymode}%
\expandafter\pgfplotstable@linear@regression@prepare@mode\expandafter{\pgfplotstable@ymode}{y}%%
%
\ifx\pgfplotstable@variance@list\pgfutil@empty
% check 'variance' key (loaded from table)
\pgfplotslistnewempty\pgfplotstable@VARIANCE
\ifx\pgfplotstable@variance@colname\pgfutil@empty
\else
\ifx\pgfplotstable@variance@table\pgfutil@empty
\t@pgfplots@toka=\expandafter{\pgfplotstable@table}%
\t@pgfplots@tokb=\expandafter{\pgfplotstable@variance@colname}%
\edef\pgfplots@loc@TMPa{{\the\t@pgfplots@tokb}\noexpand\of{\the\t@pgfplots@toka}}%
\expandafter\pgfplotstablegetcolumn\pgfplots@loc@TMPa\to\pgfplotstable@VARIANCE
\else
\t@pgfplots@toka=\expandafter{\pgfplotstable@variance@colname}%
\t@pgfplots@tokb=\expandafter{\pgfplotstable@variance@table}%
\edef\pgfplotstable@loc@TMPa{%
\noexpand\pgfplotstablegetcolumn{\the\t@pgfplots@toka}\noexpand\of{\the\t@pgfplots@tokb}\noexpand\to\noexpand\pgfplotstable@VARIANCE}%
\pgfplotstable@loc@TMPa
\fi
\fi
\else
% load from list:
\expandafter\pgfplotslistnew\expandafter\pgfplotstable@VARIANCE\expandafter{\pgfplotstable@variance@list}%
\fi
%
\pgfplotslistnewempty\pgfplotstable@Xparsed
%
\pgfmathfloatcreate{0}{0.0}{0}%
\let\pgfplotstable@S=\pgfmathresult
\let\pgfplotstable@Sxx=\pgfmathresult
\let\pgfplotstable@Sx=\pgfmathresult
\let\pgfplotstable@Sy=\pgfmathresult
\let\pgfplotstable@Sxy=\pgfmathresult
\pgfutil@loop
\pgfplotslistcheckempty\pgfplotstable@X
\ifpgfplotslistempty
\pgfplots@loop@CONTINUEfalse
\else
\pgfplots@loop@CONTINUEtrue
\fi
\ifpgfplots@loop@CONTINUE
\pgfplotslistpopfront\pgfplotstable@X\to\pgfplotstable@x
\pgfplotslistpopfront\pgfplotstable@Y\to\pgfplotstable@y
%
\pgfplotstableparsex{\pgfplotstable@x}%
\let\pgfplotstable@x=\pgfmathresult
\expandafter\pgfplotslistpushback\pgfmathresult\to\pgfplotstable@Xparsed
\pgfplotstableparsey{\pgfplotstable@y}%
\let\pgfplotstable@y=\pgfmathresult
\pgfmathfloatifflags{\pgfplotstable@y}{3}{}{% <---- This is new. The "3" stands for "nan"
%
\pgfplotslistcheckempty\pgfplotstable@VARIANCE
\ifpgfplotslistempty
\pgfmathfloatcreate{1}{1.0}{0}%
\let\pgfplotstable@invsqr=\pgfmathresult
\else
\pgfplotslistpopfront\pgfplotstable@VARIANCE\to\pgfplotstable@variance
\pgfmathfloatparsenumber{\pgfplotstable@variance}%
\let\pgfplotstable@variance=\pgfmathresult
\pgfmathfloatmultiply@{\pgfplotstable@variance}{\pgfplotstable@variance}%
\let\pgfplotstable@sqr=\pgfmathresult
\pgfmathfloatreciprocal@{\pgfplotstable@sqr}%
\let\pgfplotstable@invsqr=\pgfmathresult
\fi
%
\pgfmathfloatadd@{\pgfplotstable@S}{\pgfplotstable@invsqr}%
\let\pgfplotstable@S=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplotstable@invsqr}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sx}{\pgfplots@table@accum}%
\let\pgfplotstable@Sx=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplots@table@accum}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sxx}{\pgfplots@table@accum}%
\let\pgfplotstable@Sxx=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@y}{\pgfplotstable@invsqr}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sy}{\pgfplots@table@accum}%
\let\pgfplotstable@Sy=\pgfmathresult
%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplots@table@accum}%
\let\pgfplots@table@accum=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@Sxy}{\pgfplots@table@accum}%
\let\pgfplotstable@Sxy=\pgfmathresult
}% <---- This is new.
\pgfutil@repeat
%
\pgfmathparse{\pgfplotstable@S * \pgfplotstable@Sxx - \pgfplotstable@Sx *\pgfplotstable@Sx}%
\let\pgfplotstable@delta=\pgfmathresult
%
\pgfmathparse{(\pgfplotstable@S * \pgfplotstable@Sxy - \pgfplotstable@Sx * \pgfplotstable@Sy) / \pgfplotstable@delta}%
\let\pgfplotstable@a=\pgfmathresult
%
\pgfmathparse{(\pgfplotstable@Sxx * \pgfplotstable@Sy - \pgfplotstable@Sx * \pgfplotstable@Sxy) / \pgfplotstable@delta}%
\let\pgfplotstable@b=\pgfmathresult
%
\pgfplotslistnewempty\pgfplotstable@RESULT
\pgfplotslistforeachungrouped\pgfplotstable@Xparsed\as\pgfplotstable@x{%
\pgfmathfloatmultiply@{\pgfplotstable@x}{\pgfplotstable@a}%
\let\pgfplotstable@tmp=\pgfmathresult
\pgfmathfloatadd@{\pgfplotstable@tmp}{\pgfplotstable@b}%
\ifx\pgfplotstableparseylogbase\pgfutil@empty
\else
\pgfplotstableparseyinv@{\pgfmathresult}%
\fi
\pgfmathfloattosci{\pgfmathresult}%
\expandafter\pgfplotslistpushback\pgfmathresult\to\pgfplotstable@RESULT
}%
\pgfmathfloattosci\pgfplotstable@a
\let\pgfplotstable@a=\pgfmathresult
%
\pgfmathfloattosci\pgfplotstable@b
\let\pgfplotstable@b=\pgfmathresult
%
\global\let\pgfplotstableregressiona\pgfplotstable@a%
\global\let\pgfplotstableregressionb\pgfplotstable@b%
\let\pgfplotsretval=\pgfplotstable@RESULT
\pgfmath@smuggleone\pgfplotsretval
\endgroup
}%
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [only marks] table {
X Y
1 nan
2 1
3 2.5
4 3
};
\addplot [no markers] table[x=X,y={create col/linear regression={y=Y}}]{
X Y
1 nan
2 1
3 2.5
4 3
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
使用xelatex
.运行nan
被定义为删除 x 值的 PostScript 函数:
\RequirePackage{filecontents}
\documentclass[pstricks]{standalone}
\usepackage{pst-plot}
\begin{filecontents*}{LSM.data}
X Y
1 nan
1 1
3 1.8
4 3.3
\end{filecontents*}
\begin{document}
\begin{pspicture}(-1,-2)(5,4)
\psaxes{->}(4.5,3.5)
\readdata[ignoreLines=1]{\data}{LSM.data}
\pstVerb{/nan { pop } def }
\listplot[plotstyle=LSM,PstDebug=1,linecolor=blue]{\data}% PstDebug to print the equation
\listplot[plotstyle=dots,linecolor=blue]{\data}
\end{pspicture}
\end{document}