我想绘制一段数据并创建带有标记的图表。一些“随机”标记(例如不是每 5 个)应该使用不同的颜色,以便更好地与其他标记区分开来。
如何为第三个测量点 (3/6) 设置一个“红色 X”并为 (8/16) 设置一个黑色圆圈?
\begin{filecontents*}{data.txt}
b c
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\pgfplotscreateplotcyclelist{mycolorlist}{
blue!50!green,every mark/.append style={fill=blue!10!black},mark=x\\
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
grid=both,
width=12cm,
height=9cm,
xtick pos=left,
ytick pos=left,
xlabel={R}, ylabel={Z},
legend style={at={(0.02,0.98)},anchor=north west,cells={anchor=west}},
legend style={font=\footnotesize},
cycle list name=mycolorlist,
]
\addplot table [x=b,y=c]{data.txt};
\legend{Z}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
如果找不到更好的解决方案,您可以随时针对所需的不同坐标重复该图:
\begin{filecontents*}{data.txt}
b c
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\pgfplotscreateplotcyclelist{mycolorlist}{
blue!50!green,every mark/.append style={fill=blue!10!black},mark=x\\
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
grid=both,
width=12cm,
height=9cm,
xtick pos=left,
ytick pos=left,
xlabel={R}, ylabel={Z},
legend style={at={(0.02,0.98)},anchor=north west,cells={anchor=west}},
legend style={font=\footnotesize},
cycle list name=mycolorlist,
]
\addplot table [x=b,y=c]{data.txt};
\addplot[red, mark=x] coordinates {(3,6)};
\addplot[mark=*] coordinates {(8,16)};
\legend{Z}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案2
您可以添加scatter
以修改标记外观,scatter/classes
例如
\begin{filecontents*}{data.txt}
A C label
1 2 a
2 4 a
3 6 b
4 8 a
5 10 a
6 12 a
7 14 a
8 16 c
9 18 a
10 20 a
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\pgfplotscreateplotcyclelist{mycolorlist}{
blue!50!green,every mark/.append style={fill=blue!10!black},mark=x\\
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
width=12cm,
height=9cm,
xtick pos=left,
ytick pos=left,
xlabel={R}, ylabel={Z},
legend style={at={(0.02,0.98)},
anchor=north west,cells={anchor=west}},
legend style={font=\footnotesize},
cycle list name=mycolorlist,
scatter,
point meta=explicit symbolic,
scatter/classes={
a={},% empty argument means: use the default style
b={red},
c={mark=*,black}
},
]
\addplot table [x=A,y=C,meta=label] {data.txt};
\legend{Z}
\end{axis}
\end{tikzpicture}
\end{document}