繪製點列表,同時忽略缺失值或無限值

繪製點列表,同時忽略缺失值或無限值

我的問題與密切相關這個,這個問題尚未得到解答,並且可能會在這個更一般的表述中得到更多關注。

假設一堆(橫座標,縱座標)點包含缺失值或無限值,這些值可以以某種方式標記,例如透過直接nan在縱座標中進行標記,如下所示,或透過測試越界值,如上述問題。是否有一個簡單的修改\listplot可以繪製一個每組連續值的不同曲線,沒有缺失/無限值

下面是一個例子。首先,我用黑線繪製沒有缺失值的資料。然後,我給出相同的點,但缺少值;我使用重新定義的技巧nan(或任何用戶定義的標誌)來簡單地擺脫它們。然而,紅色圖並不令人滿意,因為線條連接了周圍的值。在藍色中,我“手動”(即通過調用幾個\listplot)繪製我想要自動獲得的結果;請注意,可以簡單地丟棄孤立點。

\begin{pspicture}
\psgrid[subgriddiv=1,griddots=10,gridlabels=7pt](0,0)(10,4)

\savedata{\data}[1 2  2 1  3 1  4 2    5 1    6 2  7 3  8 1    9 2  10 3]
\listplot[linecolor=black]{\data}

\savedata{\data}[1 2  2 1  3 1  4 nan  5 nan  6 2  7 3  8 nan  9 2  10 nan]
\pstVerb{/nan { pop } def} % nice trick to get rid of nan values
\pstScalePoints(1.,1.){}{.1 add} % some vertical shift to allow comparison
\listplot[linecolor=red]{\data}

\pstScalePoints(1.,1.){}{.2 add} % some vertical shift to allow comparison
\savedata{\data}[1 2  2 1  3 1]
\listplot[linecolor=blue]{\data}
\savedata{\data}[6 2  7 3]
\listplot[linecolor=blue]{\data}
% the following goes wrong because isolated point is completed with (0,0)
% \savedata{\data}[9 2]
% \listplot[linecolor=blue]{\data} 
\end{pspicture}

答案1

pgfplots內建此功能,並附有選項unbounded coords=jump

我不知道你的資料從哪裡來。如果它們位於data.dat以下格式的文字檔案中

1 2 
2 1
3 1
4 nan
etc.

那麼你可以直接在命令中使用它\addplot,如\addplot [unbounded coords=jump] table {data.dat};.如果文件中有多個列,您可以選擇要使用的列,例如\addplot table[x index=0,y index=2] {data.dat};.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotstableread{ % read in data
1 2 
2 1
3 1
4 nan
5 nan
6 2
7 3
8 nan
9 2
10 nan
}\data
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% axis limits
xmin=0,xmax=10,ymin=0,ymax=4,
% size of plot
% scale only axis means that 10cm is only the axis, not the ticks
width=10cm,height=4cm,scale only axis, 
% add grid
grid=major
]
% plot data with gaps in line
\addplot [blue,very thick,unbounded coords=jump] table {\data};
% default setting, no gaps
\addplot [red,dashed] table {\data};
\end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

\documentclass{scrartcl}
\usepackage{pst-plot}
\begin{document}

\begin{pspicture}[showgrid](10,4)
\savedata{\data}[1 2  2 1  3 1  4 2  5 1 6 2  7 3  8 1 9 2  10 3]
\listplot{\data}
\listplot[linecolor=blue,linewidth=2pt,nStart=1,nEnd=3]{\data}
\listplot[linecolor=blue,linewidth=2pt,nStart=6,nEnd=7]{\data}
\end{pspicture} 

\end{document}

在此輸入影像描述

相關內容