![在 groupplot 环境内的散点图中显示类别](https://linux22.com/image/463095/%E5%9C%A8%20groupplot%20%E7%8E%AF%E5%A2%83%E5%86%85%E7%9A%84%E6%95%A3%E7%82%B9%E5%9B%BE%E4%B8%AD%E6%98%BE%E7%A4%BA%E7%B1%BB%E5%88%AB.png)
可以在 groupplots 中的组内进行以下绘图吗?
到目前为止我可以做到这一点:
\documentclass{article}
\begin{filecontents*}{example_data.dat}
total_bill tip sex
16.99 1.01 Female
10.34 1.66 Male
21.01 3.50 Male
23.68 3.31 Male
24.59 3.61 Female
25.29 4.71 Male
8.77 2.00 Male
26.88 3.12 Male
15.04 1.96 Male
14.78 3.23 Female
10.27 1.71 Male
35.26 5.00 Female
15.42 1.57 Male
18.43 3.00 Female
14.83 3.02 Male
21.58 3.92 Female
10.33 1.67 Female
16.29 3.71 Male
16.97 3.50 Female
20.65 3.35 Male
\end{filecontents*}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 2,
vertical sep=2pt,
},
grid=both,
]
\nextgroupplot[
xticklabel=\empty,
yticklabel=\empty,
]
\nextgroupplot[xlabel=Total bill, ylabel=Tip, yticklabel pos=left,]
\addplot[
scatter,
only marks,
mark size=2,
]
table[x=total_bill, y=tip, meta=sex]{example_data.dat};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}
其结果是
第一个图的代码是
\documentclass{article}
\begin{filecontents*}{example_data.dat}
total_bill tip sex
16.99 1.01 Female
10.34 1.66 Male
21.01 3.50 Male
23.68 3.31 Male
24.59 3.61 Female
25.29 4.71 Male
8.77 2.00 Male
26.88 3.12 Male
15.04 1.96 Male
14.78 3.23 Female
10.27 1.71 Male
35.26 5.00 Female
15.42 1.57 Male
18.43 3.00 Female
14.83 3.02 Male
21.58 3.92 Female
10.33 1.67 Female
16.29 3.71 Male
16.97 3.50 Female
20.65 3.35 Male
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=0.8\linewidth,
height=6cm,
xlabel={Total bill},
ylabel={Tip},
axis x line=bottom,
axis y line=left,
xmin=0, xmax=60,
ymin=0, ymax=10,
scatter/classes={
Female={mark=square*,red},
Male={mark=triangle*,blue}
},
legend entries={Female,Male},
legend pos=north east,
]
\addplot[scatter,only marks,scatter src=explicit symbolic]
table[x=total_bill, y=tip, meta=sex]{example_data.dat};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我不知道这是否是您想要的,但也许您想使用过滤器将图分成两部分,以便第一个图仅显示 的值,Male
第二个图仅显示 的值Female
。如果这不是您想要的,下面的代码至少应该向您展示如何定位两个图,它们的样式与您在 中显示的样式相同groupplot
:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\begin{filecontents*}{example_data.dat}
total_bill tip sex
16.99 1.01 Female
10.34 1.66 Male
21.01 3.50 Male
23.68 3.31 Male
24.59 3.61 Female
25.29 4.71 Male
8.77 2.00 Male
26.88 3.12 Male
15.04 1.96 Male
14.78 3.23 Female
10.27 1.71 Male
35.26 5.00 Female
15.42 1.57 Male
18.43 3.00 Female
14.83 3.02 Male
21.58 3.92 Female
10.33 1.67 Female
16.29 3.71 Male
16.97 3.50 Female
20.65 3.35 Male
\end{filecontents*}
\pgfplotstableread{example_data.dat}{\datatable}
\ExplSyntaxOn
\pgfplotsset{
filter~explicit/.style~args={#1~is~#2~in~#3}{
x~filter/.code={
\pgfplotstablegetelem{\coordindex}{#1}\of#3
\str_if_eq:NNF \pgfplotsretval { #2 } {
\tl_clear:N \pgfmathresult
}
}
}
}
\ExplSyntaxOff
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 2,
vertical sep=4em,
},
grid=both,
width=0.8\linewidth,
height=6cm,
axis x line=bottom,
axis y line=left,
xmin=0, xmax=60,
ymin=0, ymax=10,
scatter/classes={
Female={mark=square*, red},
Male={mark=triangle*, blue}
},
]
\nextgroupplot[xlabel=Total bill, ylabel=Tip, yticklabel pos=left]
\addplot[
scatter,
only marks,
mark size=2,
scatter src=explicit symbolic,
filter explicit={sex is Female in \datatable}
]
table[x=total_bill, y=tip, meta=sex] \datatable;
\legend{Female,}
\nextgroupplot[xlabel=Total bill, ylabel=Tip, yticklabel pos=left]
\addplot[
scatter,
only marks,
mark size=2,
scatter src=explicit symbolic,
filter explicit={sex is Male in \datatable}
]
table[x=total_bill, y=tip, meta=sex] \datatable;
\legend{,Male}
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}