我有一个(希望)不错的 pgfplot(如果您对布局有任何优化建议,请随时告诉我 :)),我会反复使用它。因此,我想创建一个新命令,该命令仅输入数据并自动生成 pgfplot。要输入的数据是每个点的值加上置信区间的值。其余的都应该保持不变。我可以设法创建一个新命令,将每个有问题的值放在 {} 中,并将其放入 pgfplot 的代码中 - 但这不是一个好的解决方案。更漂亮的解决方案是将数据输入一个 {} 中,并用空格分隔各个条目。例如:
\makemypgfplot{
4 3 2.5
5 4 2
7 3 2.5
...
}
这是相应的 pgfplot:
\documentclass{article}
\usepackage{tikz} %<-- pgfplots loads TiKZ
\usepackage{pgfplots}
\definecolor{CG}{RGB}{127, 127, 127}
\definecolor{MC}{RGB}{23, 110, 178}
\definecolor{TV}{RGB}{208, 43, 139}
\definecolor{BP}{RGB}{27, 175, 147}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\begin{axis}[
axis lines=left,
xlabel= , xmin=0.5, xmax=3.5,
ylabel=Metacognition,
axis line style={-},
xtick = {1,2,3},
xticklabels = {$t_1$,$t_2$,$t_3$},
legend style={at={(axis cs:3.8,2)},anchor=south west,font=\small,mark size=2pt}
]
\addplot[
smooth,
thick,
solid,
mark=*,
mark size=2.5pt,
color=CG,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(0.91, 4)+=(0, 3)-=(0, 2.5)
(1.91, 5)+=(0, 4)-=(0, 2)
(2.91, 7)+=(0, 3)-=(0, 2.5)
};
\addlegendentry{CG}
\addplot[smooth,
thick,
dashed,
color=MC,
mark=square*,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(0.97, 1)+=(0, 3)-=(0, 2.5)
(1.97, 2)+=(0, 4)-=(0, 2)
(2.97, 4)+=(0, 3)-=(0, 2.5)
};
\addlegendentry{MC}
\addplot[smooth,
thick,
dotted,
color=TV,
mark=diamond*,
mark size=3pt,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(1.03, 2)+=(0, 3)-=(0, 2.5)
(2.03, 5)+=(0, 4)-=(0, 2)
(3.03, 3)+=(0, 3)-=(0, 2.5)
};
\addlegendentry{TV}
\addplot[smooth,
thick,
dashdotted,
color=BP,
mark=triangle*,
mark size=3pt,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(1.07, 2.4)+=(0, 3)-=(0, 2.5)
(2.07, 3.1)+=(0, 4)-=(0, 2)
(3.07, 1.1)+=(0, 3)-=(0, 2.5)
};
\addlegendentry{BP}
\end{axis}
\end{tikzpicture}
\end{document}
我很难让新命令识别单个值。有人知道怎么做吗?
感谢您提供任何想法!Mil
答案1
Kjell Magne Fauske 发布了一个用于绘制线图的宏TeXample.com他在外部 txt 文档中定义数据行。(实际上,他将它们存储在变量中,并在宏中调用该变量。)使用替换功能,可以使用常用的文本编辑器轻松创建 txt 文档。
答案2
所以,这是我自己想出的解决方案。虽然不是很漂亮,但可以很顺利地解决我提出的问题。这个解决方案还有一个积极的副作用,那就是始终清楚一行变量属于哪一组。
\documentclass{article}
%\usepackage{tikz} %<-- pgfplots loads TiKZ
\usepackage{pgfplots}
\definecolor{CG}{RGB}{127, 127, 127}
\definecolor{MC}{RGB}{23, 110, 178}
\definecolor{TV}{RGB}{208, 43, 139}
\definecolor{BP}{RGB}{27, 175, 147}
\pgfplotsset{%compat=1.9,
width=6cm,
height=6cm,
scale only axis,
axis lines=left,
xlabel= , xmin=0.5, xmax=3.5,
ylabel=Caption,
ylabel style={yshift=-0.4cm},
axis line style={-},
xtick = {1,2,3},
xticklabels = {$t_1$,$t_2$,$t_3$},
legend style={at={(axis cs:3.8,2)},anchor=south west,font=\small},
x tick label style={major tick length=3pt},
xtick align=inside,
ytick align=outside}
\newcommand{\plotCG}[9]{
\addplot[
smooth,
thick,
solid,
mark=*,
mark size=2.5pt,
color=CG,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(0.91, #1)+=(0, #2)-=(0, #3)
(1.91, #4)+=(0, #5)-=(0, #6)
(2.91, #7)+=(0, #8)-=(0, #9)
};
\addlegendentry{CG}
}
\newcommand{\plotMC}[9]{
\addplot[smooth,
thick,
dashed,
color=MC,
mark=square*,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(0.97, #1)+=(0, #2)-=(0, #3)
(1.97, #4)+=(0, #5)-=(0, #6)
(2.97, #7)+=(0, #8)-=(0, #9)
};
\addlegendentry{MC}
}
\newcommand{\plotTV}[9]{
\addplot[smooth,
thick,
dotted,
color=TV,
mark=diamond*,
mark size=3pt,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(1.03, #1)+=(0, #2)-=(0, #3)
(2.03, #4)+=(0, #5)-=(0, #6)
(3.03, #7)+=(0, #8)-=(0, #9)
};
\addlegendentry{TV}
}
\newcommand{\plotBP}[9]{
\addplot[smooth,
thick,
dashdotted,
color=BP,
mark=triangle*,
mark size=3pt,
mark options=solid,
error bars/.cd, y dir=both, y explicit,
error bar style={solid},
] plot coordinates {
(1.07, #1)+=(0, #2)-=(0, #3)
(2.07, #4)+=(0, #5)-=(0, #6)
(3.07, #7)+=(0, #8)-=(0, #9)
};
\addlegendentry{BP}
}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\begin{axis}[ylabel=Metacognition]
\plotCG{4}{3}{2.5}{5}{4}{2}{7}{3}{2.5}
\plotMC{1}{3}{2.5}{2}{4}{2}{4}{3}{2.5}
\plotTV{2}{3}{2.5}{5}{4}{2}{3}{3}{2.5}
\plotBP{2.4}{3}{2.5}{3.1}{4}{2}{1.1}{3}{2.5}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale=1.3]
\begin{axis}[ylabel=Motivation]
\plotCG{3}{3}{2.5}{3}{4}{2}{4}{3}{2.5}
\plotMC{4}{3}{2.5}{4}{4}{2}{4}{3}{2.5}
\plotTV{2}{3}{2.5}{2}{4}{2}{3}{3}{2.5}
\plotBP{5}{3}{2.5}{4}{4}{2}{3}{3}{2.5}
\end{axis}
\end{tikzpicture}
\end{document}