我想将每个点都涂成红色,对应的数据第二列设置为 1。
这是我的最小工作示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[header=false, col sep=comma]{
Label 1,1,0.0443
Label 2,0,0.0371
Label 3,1,0.0191
Label 4,0,0.0638
Label 5,0,0.0149
}\data
\begin{figure}
\begin{tikzpicture}
\centering
\begin{axis}[
axis x line=bottom,
axis y line=left,
xmin=0,
width=10cm,
ymajorgrids,
bar width=2ex, y=3ex,
enlarge y limits={abs=0.75},
ytick=data,
scaled ticks=false,
yticklabels from table={\data}{0},
xticklabel style={/pgf/number format/fixed},
]
\addplot[only marks] table [
y expr=-\coordindex,
x index=2
]{\data};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
第一个和第三个点应该是数据中定义的红色。
我还没有找到任何基于数据定义颜色(或形状!)的方法。使用 TikZ/PGFPlots 可以实现吗?
答案1
您可以用于scatter/classes
此。
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[header=false, col sep=comma]{
Label 1,1,0.0443
Label 2,0,0.0371
Label 3,1,0.0191
Label 4,0,0.0638
Label 5,0,0.0149
}\data
\begin{tikzpicture}
\centering
\begin{axis}[
axis x line=bottom,
axis y line=left,
xmin=0,
width=10cm,
ymajorgrids,
bar width=2ex, y=3ex,
enlarge y limits={abs=0.75},
ytick=data,
scaled ticks=false,
yticklabels from table={\data}{0},
xticklabel style={/pgf/number format/fixed},
scatter src=explicit,
scatter/classes={
0={mark=*,black},
1={mark=*,red}
}
]
\addplot[scatter,only marks] table [
y expr=-\coordindex,
x index=2,
meta index=1 % specify meta column
]{\data};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
在 \addplot[only marks,red] 选项中添加红色
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[header=false, col sep=comma]{
Label 1,1,0.0443
Label 2,0,0.0371
Label 3,1,0.0191
Label 4,0,0.0638
Label 5,0,0.0149
}\data
\begin{figure}
\begin{tikzpicture}
\centering
\begin{axis}[
axis x line=bottom,
axis y line=left,
xmin=0,
width=10cm,
ymajorgrids,
bar width=2ex, y=3ex,
enlarge y limits={abs=0.75},
ytick=data,
scaled ticks=false,
yticklabels from table={\data}{0},
xticklabel style={/pgf/number format/fixed},
]
\addplot[only marks,red] table [
y expr=-\coordindex,
x index=2
]{\data};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}