如何在 \pgfplotstableread 中添加小数?

如何在 \pgfplotstableread 中添加小数?

如何在中添加小数\pgfplotstableread

以下代码无需小数即可运行......

\pgfplotstableread[col sep=comma]{
label,startyear,endyear
D, 2003, 2008
E, 2008, 2011
F, 2012, 2014
G, 2013, 2017
}\events

...但没有小数(我也添加了precision=1)...

\pgfplotstableread[col sep=comma,precision=1]{
label,startyear,endyear
D, 2003.8, 2008.2
E, 2008.9, 2011.7
F, 2012.3, 2014.5
G, 2013.5, 2017.4
}\events

....在下面的代码中:

\documentclass{article}
\usepackage{tikz, pgfplotstable}
\usetikzlibrary{arrows.meta}
\usepackage[utf8]{inputenc}

% read in table
\pgfplotstableread[col sep=comma]{
label,startyear,endyear
A, 2011, 2012
B, 2016, 2018
C, 2018, 2022
}\authors


\pgfplotstableread[col sep=comma,precision=1]{
label,startyear,endyear
D, 2003.8, 2008.2
E, 2008.9, 2011.7
F, 2012.3, 2014.5
G, 2013.5, 2017.4
}\events


% add color columns
\pgfplotstablecreatecol[create col/set={blue!30}]{color}{\authors}
\pgfplotstablecreatecol[create col/set={red!30}]{color}{\events}

% append events table to authors table
\pgfplotstablevertcat{\authors}{\events}
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\authors

\begin{document}
\noindent\begin{tikzpicture}[x=7mm]
\pgfmathsetmacro\yscale{0.3} % scaling for y-direction 
                             % -- effectively vertical distance between bars
\pgfmathsetmacro\barheight{0.25} % height of bars

\pgfplotstablegetrowsof{\sortedtable}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}

\foreach [count=\i,evaluate=\i as \ylevel using \i*\yscale] \row in {0,...,\M}{
  % get entries from table row
  \pgfplotstablegetelem{\row}{[index]0}\of\sortedtable
  \let\txt\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]1}\of\sortedtable
  \let\xstart\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]2}\of\sortedtable
  \let\xend\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]3}\of\sortedtable
  \let\barcolor\pgfplotsretval
  \xdef\TheLastY{\ylevel}
  
  \ifnum \xend=-1
    % single day entry, draw circle
    \filldraw [draw=black!99!\barcolor,fill=\barcolor] (\xstart-2000,-\ylevel)
      circle[radius=\barheight/2.5*1cm]
      node[black,right=3pt,font=\scriptsize] {\txt};
  \else
    % interval, draw a rectangle
    \fill [\barcolor] (\xstart-2000,-\ylevel-\barheight/2) rectangle
                        (\xend-2000,-\ylevel+\barheight/2);
     % add text label right of the rectangle
    \node [right,font=\scriptsize] at (\xend-2000,-\ylevel) {\txt};
  \fi
}

\draw [Stealth-Stealth] (2003-2000,0) -- (2003-2000,-\TheLastY-\yscale)
                                      -- (2023-2000,-\TheLastY-\yscale);

% add ticks on x-axis
\foreach \year in {2003, 2004, ..., 2022}
  \draw (\year-2000,-\TheLastY-\yscale) ++(0,3pt)
     -- ++(0,-6pt) node[rotate=45,anchor=east] {\year};
\end{tikzpicture}
\end{document}

答案1

如果使用小数,则不能再使用\ifnum,因为它不接受小数。实际上应该不需要precision=1

一个简单的解决方案是使用\ifdim。您只需将要比较的数字转换为尺寸,例如通过添加pt它们即可。

另一个解决方案是先使用 PGF/Ti 进行计算Z 肯定只返回整数。因此,您可以先定义\pgfmathsetmacro{\xendsign}{sign(\xend)}– ,它对-1任何负数、1任何正数和0零都返回 – 然后用 进行测试\ifnum\xendsign=-1。那么,此测试对任何负数都成立,而不仅仅是对 -1 成立。或者您可以计算类似的东西\pgfmathsetmacro{\xendint}{int(\xend)},并用它来进行测试。

\pgfplotsset{compat=1.18} 
\usetikzlibrary{arrows.meta}

% read in table
\pgfplotstableread[col sep=comma]{
label, startyear, endyear
A, 2011, 2012
B, 2016, 2018
C, 2018, 2022
}\authors

\pgfplotstableread[col sep=comma]{
label, startyear, endyear
D, 2003.8, 2008.2
E, 2008.9, 2011.7
F, 2012.3, 2014.5
G, 2013.5, 2017.4
H, 2011.5, -1       % added to test functionality
}\events

% add color columns
\pgfplotstablecreatecol[create col/set={blue!30}]{color}{\authors}
\pgfplotstablecreatecol[create col/set={red!30}]{color}{\events}

% append events table to authors table
\pgfplotstablevertcat{\authors}{\events}
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\authors

\begin{document}
\noindent\begin{tikzpicture}[x=7mm]
\pgfmathsetmacro\yscale{0.3} % scaling for y-direction 
                             % -- effectively vertical distance between bars
\pgfmathsetmacro\barheight{0.25} % height of bars

\pgfplotstablegetrowsof{\sortedtable}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}

\foreach [count=\i, evaluate=\i as \ylevel using \i*\yscale] \row in {0,...,\M} {
  % get entries from table row
  \pgfplotstablegetelem{\row}{[index]0}\of\sortedtable
  \let\txt\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]1}\of\sortedtable
  \let\xstart\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]2}\of\sortedtable
  \let\xend\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]3}\of\sortedtable
  \let\barcolor\pgfplotsretval
  \xdef\TheLastY{\ylevel}
  
  \ifdim\xend pt=-1pt
    % single day entry, draw circle
    \filldraw [draw=black!99!\barcolor, fill=\barcolor] (\xstart-2000,-\ylevel)
      circle[radius=\barheight/2.5*1cm]
      node[black, right=3pt, font=\scriptsize] {\txt};
  \else
    % interval, draw a rectangle
    \fill [\barcolor] (\xstart-2000,-\ylevel-\barheight/2) rectangle
                        (\xend-2000,-\ylevel+\barheight/2);
     % add text label right of the rectangle
    \node [right, font=\scriptsize] at (\xend-2000,-\ylevel) {\txt};
  \fi
}

\draw [Stealth-Stealth] (2003-2000,0) -- (2003-2000,-\TheLastY-\yscale)
                                      -- (2023-2000,-\TheLastY-\yscale);

% add ticks on x-axis
\foreach \year in {2003, 2004, ..., 2022}
  \draw (\year-2000,-\TheLastY-\yscale) ++(0,3pt)
     -- ++(0,-6pt) node[rotate=45,anchor=east] {\year};
\end{tikzpicture}
\end{document}

输出结果为:

在此处输入图片描述

相关内容