以下代码中的选项skip
仅适用于散点图,不适用于箱线图和直方图。
\begin{tikzpicture}[scale=1.3]
\begin{axis}[ytick={0,0.5,1}, ymax=2, ymin=0, axis y line=left, axis x line=bottom,xmin = 0, xmax=
8, small, boxplot/draw position=1.5, boxplot/box extend=.2, skip coords between index={0}{140}]
\addplot+[mark size=0.5pt, boxplot]table[x index = 0, y index=\INDEX]{Graphics/iris.dat}
\addplot [hist=density, fill=orange!75, draw=orange!50!black] table [x index = 0, y index=\INDEX]{Graphics/iris.dat};
\addplot[scatter, only marks, mark=|, mark size=2pt, thick] table[x index=\INDEX ,y expr=0.02]{Graphics/iris.dat};
\end{axis}
\end{tikzpicture}
答案1
第一部分是部分答案,仅针对hist
。请参阅下文了解 的可能解决方法boxplot
。作为参考,iris.dat
我在以下所有内容中使用的结构如下:
$ head -n5 iris.dat
sl sw pl pw
5.10 3.50 1.40 0.20
4.90 3.00 1.40 0.20
4.70 3.20 1.30 0.20
4.60 3.10 1.50 0.20
在技术文档在 1.15 版手册第 390 页的pgfplots
描述下skip coords between index
这种风格通常适用于X坐标(即X坐标)。如果你想将它应用于
hist/data
或 之类的东西quiver/u
,你可以
- 在样式名称后附加星号“*”并
- 提供目标坐标的名称作为第一个参数。
例如,
skip coords between index*={hist/data}{2}{4}
适用于hist/data
。
因此,如果您添加skip coords between index*={hist/data}{0}{140}
选项axis
,过滤似乎对 有效hist
。我不知道 的等效项boxplot
是什么,如果有的话(boxplot/data
至少hist/data
在这里不起作用)。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\begin{axis}[
ytick={0,0.5,1},
ymax=2, ymin=0,
axis y line=left,
axis x line=bottom,
xmin = 0, xmax=8,
small,
boxplot/draw position=1.5,
boxplot/box extend=.2,
skip coords between index*={hist/data}{0}{140},
skip coords between index={0}{140}
]
\newcommand\INDEX{2}
\addplot+[mark size=0.5pt,boxplot] table[x index = 0, y index=\INDEX]{iris.dat};
\addplot [hist=density, fill=orange!75, draw=orange!50!black] table [x index = 0, y index=\INDEX]{iris.dat};
\addplot[scatter, only marks, mark=|, mark size=2pt, thick] table[x index=\INDEX ,y expr=0.02]{iris.dat};
\end{axis}
\end{tikzpicture}
\end{document}
箱线图的可能解决方法
这相当复杂,但乍一看确实能提供有用的输出。下面我要做的是定义一个宏,\nanrows{i}{j}
它复制 iris 表,但将 和 之间的行号的所有值替换为i
。j
这样nan
做还会添加一个包含行号的新列。这个新表保存为\IRISnan
添加新列意味着使用的列索引\addplot
必须增加 1。
(很可能有更优雅的方式来做同样的事情。)
\documentclass{article}
\usepackage{pgfplotstable,longtable}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.15}
\pgfplotstableread{iris.dat}\IRIS
\pgfplotstablegetrowsof{\IRIS}
\newcommand\NUMROWS\pgfplotsretval
\newcommand\nancol[4]{%
% #1: column name
% #2: table name
% #3: start index for nans
% #4: end index for nans
\pgfplotstablecreatecol[
create col/assign/.code={
\pgfmathtruncatemacro\tmp{#3-1} % only have ">" comparison, not ">=", so subtract 1
\ifnum \thisrow{index}>\tmp
\ifnum \thisrow{index}<#4
\edef\entry{nan} % if "lower value" <= index < "upper value", set value to nan
\else
\pgfplotstablegetelem{\thisrow{index}}{#1}\of\IRIS %otherwise use value from original iris-table
\edef\entry{\pgfplotsretval}
\fi
\else
\pgfplotstablegetelem{\thisrow{index}}{#1}\of\IRIS
\edef\entry{\pgfplotsretval}
\fi
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{#1}{#2}}
\newcommand\nanrows[3][\IRISnan]{
% #1: name of new table, optional
% #2: start index for nans, passed to \nancol
% #3: end index for nans, passed to \nancol
\pgfplotstablenew[
columns={index},
create on use/index/.style={create col/set list={0,...,\NUMROWS}}
]{\NUMROWS}{#1}
\nancol{sl}{#1}{#2}{#3}
\nancol{sw}{#1}{#2}{#3}
\nancol{pl}{#1}{#2}{#3}
\nancol{pw}{#1}{#2}{#3}
}
% set value in rows 0-99 to nan
% resulting table saved in \IRISnan
\nanrows{0}{140}
\begin{document}
% if you want to look at the table (requires longtable package, see preamble)
%\pgfplotstabletypeset[begin table=\begin{longtable},end table=\end{longtable}]\IRISnan
\begin{tikzpicture}[scale=1.3]
\begin{axis}[
ytick={0,0.5,1},
ymax=2, ymin=0,
axis y line=left,
axis x line=bottom,
xmin = 0, xmax=8,
small,
boxplot/draw position=1.5,
boxplot/box extend=.2,
filter discard warning=false % removes a lot of warnings about unbounded coordinates
]
\newcommand\INDEX{3}
\addplot+[mark size=0.5pt,boxplot] table[x index = 1, y index=\INDEX]{\IRISnan};
\addplot [hist=density, fill=orange!75, draw=orange!50!black] table [x index = 1, y index=\INDEX]{\IRISnan};
\addplot[scatter, only marks, mark=|, mark size=2pt, thick] table[x index=\INDEX ,y expr=0.02]{\IRISnan};
\end{axis}
\end{tikzpicture}
\end{document}