我对 pgfplot 还很陌生,现在我已经学了几个小时,正在尝试构建这样的图表:
https://www.dropbox.com/s/o3yq97oc9rwmthh/mcgreedy.csv?dl=0 这是.csv 文件!
x 轴是实验尝试次数,y 轴有 2 种不同的算法以及它们在每次尝试中产生的分数
我遇到了一些错误,这些错误与我尝试绘制一条简单的线 {x+1} 有关,目前,我只是想直观地了解我正在做的事情
感谢大家!
\begin{tikzpicture}
\begin{axis}[
width=10cm,
grid = major,
grid style = {dashed, gray!30},
xmin = 0,
ymin = 0,
xmax = 105,
ymax = 280,
/pgfplots/xtick = {0,5,...,100},
axis background/.style = {fill=white},
ylabel = {Trial Number},
xlabel = {Score},
tick align = outside,
]
\addplot table {mcgreedy.csv}
%\addplot[domain=0:100, red, thick] ({x+1});
\end{axis}
\end{tikzpicture}
答案1
您只是在行尾少了一个分号\addplot
。默认情况下,每个文件只添加一个图,因此您需要两个图,每个系列一个。
其他一些评论:
- 如果您希望图例条目显示在一行中,如您的屏幕截图所示,请添加
legend columns=-1
。 - 我认为 之间应该留出更多空间
xticklabels
。例如,您可以稍微减小字体大小(ticklabel style={font=\small}
),增加轴的宽度(如果有空间),或者减少刻度。每 10 个点一个刻度可能就足够了。 /pgfplots/
请注意,您之前不需要xtick
。
\documentclass{article}
\usepackage{pgfplots,showframe}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=0.95\linewidth,
height=0.6\linewidth,
grid = major,
grid style = {dashed, gray!30},
xmin = 0,
ymin = 0,
xmax = 105,
ymax = 280,
xtick = {0,10,...,100},
ylabel = {Trial Number},
xlabel = {Score},
tick align = outside,
legend pos=south east,
legend columns=-1
]
\addplot table[x index=0,y index=1] {mcgreedy.csv};
\addplot table[x index=0,y index=2] {mcgreedy.csv};
\legend{ECTS, E.G.}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=0.95\linewidth,
height=0.6\linewidth,
grid = major,
grid style = {dashed, gray!30},
xmin = 0,
ymin = 0,
xmax = 105,
ymax = 280,
xtick = {0,5,...,100},
ticklabel style={font=\small},
ylabel = {Trial Number},
xlabel = {Score},
tick align = outside,
legend pos=south east,
legend columns=-1
]
\addplot table[x index=0,y index=1] {mcgreedy.csv};
\addplot table[x index=0,y index=2] {mcgreedy.csv};
\legend{ECTS, E.G.}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}