根据这个问题如何在 DATE x 轴上绘制回归图我发布了 Christian Feuersänger 的 MWE:
\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{filecontents}{data.dat}
date value1
2010-01-01 2
2010-01-02 10
2010-01-03 8
2010-01-04 15
\end{filecontents}
\usepackage{pgfplotstable}
% formats row \ticknum of the input table using '#3'
% #1: table name
% #2: column name
% #3: formatting instruction like \day.\month.\year
\newcommand\datelabelsfromtable[3]{%
\begingroup
\countdef\result=\count200 %
\pgfplotstablegetelem{\ticknum}{#2}\of{#1}%
\pgfcalendardatetojulian{\pgfplotsretval}{\result}%
\pgfcalendarjuliantodate{\result}\year\month\day
%
#3%
\endgroup
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x tick label style={yshift=-3pt,xshift=-4pt,rotate=-30,anchor=west},
xticklabel={\datelabelsfromtable{data.dat}{date}{\day.\month.\year}},
xtick=data,
]
\addplot table [x expr=\coordindex,y=value1] {data.dat};
\addplot table [x expr=\coordindex, y={create col/linear regression={y=value1}}] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}
回归线在这里起作用了,我希望我理解了他的命令。使用我的数据,我希望 PGF 使用 .dat 文件内我指定的所有数据点。但是,我不想使用
xtick=data,
但我想指定为哪些数据点写标签,因为图表的前半部分并不那么重要,但最后我想有更多的刻度。使用来自 MWE 的示例数据,我会想到这样的东西,跳过 2010-01-02:
xtick={2010-01-01, 2010-01-03, 2010-01-04}
我尝试了不同的版本,比如 {2010},但总是收到错误(可能是因为 x 日期没有声明为 DATE?这样做也会导致错误)。
总结一下:
我如何使用 Feuersänger 的方法(或者如果有人在进行回归时有另一种使用日期的想法)但可以指定打印哪些刻度而不仅仅是使用 xtick = data?
答案1
使用来自的代码https://tex.stackexchange.com/a/372049/95441(正如您建议的那样平行问题)使用xtick
那里的作品非常好......
% used PGFPlots(Table) v1.16
% (code from <https://tex.stackexchange.com/a/372049/95441>)
\begin{filecontents*}{testtable.csv}
date;value
2017-01-01;0
2017-01-02;1
2017-01-11;2
2017-01-12;3
2017-02-01;4
2017-02-02;5
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfcalendar} % <-- to convert the dates to Julian integers
\usepackage{pgfplots}
\usepackage{pgfplotstable} % <-- to manipulate the data file/table
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.14}
\pgfplotstableread[col sep=semicolon]{testtable.csv}\data
% add new column with Julian integer numbers
% therefore a counter is needed
\newcount\julianday
\pgfplotstablecreatecol[
create col/assign/.code={
% convert the number of the current row and save it to `\julianday'
\pgfcalendardatetojulian{\thisrow{date}}{\julianday}
% then give the entry of `\julianday' to `\entry' which is then
% given to the current cell
\edef\entry{\the\julianday}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
},
]{JulianDay}{\data}
% because the `dateplot' library shifts automatically all dates to 0 using
% the first found coordinate we can't use the created `JulianDay' data
% directly for `linear regression', but have to do the same first with
% the data
% get the first coordinate of the column ...
\pgfplotstablegetelem{0}{JulianDay}\of{\data}
% ... and store it in `\xmin'
\pgfmathtruncatemacro{\xmin}{\pgfplotsretval}
% now create another column with the shifted values
\pgfplotstablecreatecol[
expr={\thisrow{JulianDay}-\xmin},
]{JulianDayMod}{\data}
\begin{document}
% --------------------------------------------------------
%% for debugging purposes only
%% show resulting numbers, if you want
%\pgfplotstabletypeset[
% column type=l,
% columns={date,JulianDay,JulianDayMod,value},
% columns/date/.style={string type},
% columns/JulianDay/.style={/pgf/number format/fixed},
%]\data
% --------------------------------------------------------
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
% =====================================================================
% providing `xtick's works perfectly fine
xtick={2017-01-01,2017-01-31},
% =====================================================================
]
\addplot+ [only marks] table [x=date,y=value] {\data};
\addplot+ [mark=none] table [
x=date,
% now we can use the newly created column to do the linear regression
y={create col/linear regression={
x=JulianDayMod,
y=value,
}}
] {\data};
\end{axis}
\end{tikzpicture}
\end{document}