
如何修復 x 軸上的重疊值?
\documentclass{standalone}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{positioning}
\pgfplotsset{compat=1.16,width=0.98\textwidth}
%\pgfplotsset{compat=1.16,width=0.98\textwidth}
\usepackage{tikz}
\usepackage[english,spanish]{babel} % multilenguaje
% grid style
\pgfdeclareplotmark{mystar}{
\node[star,star point ratio=2.25,minimum size=6pt,
inner sep=0pt,draw=black,solid,fill=red] {};
}
\decimalpoint
\definecolor{color0}{rgb}{1,1,0}
\definecolor{color1}{rgb}{1,0.647058823529412,0}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis line style={black},
scaled x ticks = true,
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=black},
tick align=outside,
x grid style={dashed,black!60},
xlabel={$\lambda$},
xmajorticks=true,
xmin=0.0055, xmax=0.1045,
xtick style={color=black},
y grid style={dashed,black!60},
ylabel={RMSE},
ymajorticks=true,
ymin=0.912938021884183, ymax=1.01339429908853,
ytick style={black},
xtick align=inside,
ytick align=inside,
grid = none,
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=4
},
]
\addplot [mark = *,line width=0.8pt,blue, mark size=1pt]
table {%
0.01 0.97820029742643
0.02 0.962938632883304
0.03 0.944377590107486
0.04 0.934490513382265
0.05 0.926770707258887
0.09 0.921652885011797
0.1 0.922493653631392
};
\addlegendentry{20 f}
\addplot [mark = *,line width=0.8pt,black, mark size=1pt]
table {%
0.01 0.992629285376604
0.02 0.970555305088483
0.03 0.950769057081716
0.04 0.937408149963347
0.05 0.92888998569171
0.09 0.919757726792486
0.1 0.922265347358195
};
\addlegendentry{30 f}
\addplot [mark = *,line width=0.8pt,red, mark size=1pt]
table {%
0.01 1.00429685084556
0.02 0.976819790163343
0.03 0.955650609854943
0.04 0.939869451503815
0.05 0.927933873176105
0.09 0.917634061468206
0.1 0.920597626159782
};
\addlegendentry{50 f}
\addplot [mark = *,line width=0.8pt,yellow, mark size=1pt]
table {%
0.01 1.00882810467015
0.02 0.972771038904076
0.03 0.955085239938932
0.04 0.941800125612627
0.05 0.930788565993547
0.09 0.917504216302563
0.1 0.920804593468368
};
\addlegendentry{100 f}
\addplot [mark = *,line width=0.8pt,orange, mark size=1pt]
table {%
0.01 1.00130716487781
0.02 0.97125309142477
0.03 0.952896925523292
0.04 0.940895422722576
0.05 0.931374232049292
0.09 0.920535667968586
0.1 0.920701204441381
};
\addlegendentry{150 f}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
1. 縮放 x 刻度
scaled x ticks = base 10:2,
2.更改數字格式
xticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=2},
3.減少xticks的數量。
僅列出所需的 xtick 標籤
xtick={0.00,0.02,...,0.12},
或者,增加xtick distance
(如評論者斯特凡·平諾)
xtick distance=0.02,
程式碼
\documentclass[border=3mm]{standalone}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{positioning}
\usepgfplotslibrary{units}
\pgfplotsset{compat=1.16,width=0.98\textwidth}
%\pgfplotsset{compat=1.16,width=0.98\textwidth}
\usepackage{tikz}
\usepackage[english,spanish]{babel} % multilenguaje
% grid style
\pgfdeclareplotmark{mystar}{
\node[star,star point ratio=2.25,minimum size=6pt,
inner sep=0pt,draw=black,solid,fill=red] {};
}
\decimalpoint
\definecolor{color0}{rgb}{1,1,0}
\definecolor{color1}{rgb}{1,0.647058823529412,0}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis line style={black},
% scaled x ticks = base 10:2,
% xticklabel style={
% /pgf/number format/fixed,
% /pgf/number format/precision=2},
xtick={0.00,0.02,...,0.12},
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=black},
tick align=outside,
x grid style={dashed,black!60},
xlabel={$\lambda$},
xmajorticks=true,
xmin=0.0055, xmax=0.1045,
xtick style={color=black},
y grid style={dashed,black!60},
ylabel={RMSE},
ymajorticks=true,
ymin=0.912938021884183, ymax=1.01339429908853,
ytick style={black},
xtick align=inside,
ytick align=inside,
grid = none,
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=4
},
]
\addplot [mark = *,line width=0.8pt,blue, mark size=1pt]
table {%
0.01 0.97820029742643
0.02 0.962938632883304
0.03 0.944377590107486
0.04 0.934490513382265
0.05 0.926770707258887
0.09 0.921652885011797
0.1 0.922493653631392
};
\addlegendentry{20 f}
\addplot [mark = *,line width=0.8pt,black, mark size=1pt]
table {%
0.01 0.992629285376604
0.02 0.970555305088483
0.03 0.950769057081716
0.04 0.937408149963347
0.05 0.92888998569171
0.09 0.919757726792486
0.1 0.922265347358195
};
\addlegendentry{30 f}
\addplot [mark = *,line width=0.8pt,red, mark size=1pt]
table {%
0.01 1.00429685084556
0.02 0.976819790163343
0.03 0.955650609854943
0.04 0.939869451503815
0.05 0.927933873176105
0.09 0.917634061468206
0.1 0.920597626159782
};
\addlegendentry{50 f}
\addplot [mark = *,line width=0.8pt,yellow, mark size=1pt]
table {%
0.01 1.00882810467015
0.02 0.972771038904076
0.03 0.955085239938932
0.04 0.941800125612627
0.05 0.930788565993547
0.09 0.917504216302563
0.1 0.920804593468368
};
\addlegendentry{100 f}
\addplot [mark = *,line width=0.8pt,orange, mark size=1pt]
table {%
0.01 1.00130716487781
0.02 0.97125309142477
0.03 0.952896925523292
0.04 0.940895422722576
0.05 0.931374232049292
0.09 0.920535667968586
0.1 0.920701204441381
};
\addlegendentry{150 f}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
編輯:
現在我注意到很晚了,其他答案已經為xtick
標籤添加了與我嘗試建議的相同的解決方案:-(。所以只有一些偏離主題的建議...
我將使用該filecontents
套件來設計您的圖表,並將值的小數位數減少y
到四(在我看來,它們仍然提供令人滿意的準確性):
\documentclass[margin=3mm]{standalone}
\usepackage[english,spanish]{babel} % multilenguaje
\usepackage{pgfplots}
\usetikzlibrary{babel,
positioning}
\pgfplotsset{compat=1.16,width=0.98\linewidth}
\usepackage{filecontents}
\begin{filecontents}{performance.data}
x A B C D E
0.01 0.9782 0.99262 1.0042 1.0088 1.0013
0.02 0.9629 0.97055 0.9768 0.9727 0.9712
0.04 0.9344 0.93740 0.9398 0.9418 0.9408
0.05 0.9267 0.92888 0.9279 0.9307 0.9313
0.09 0.9216 0.91975 0.9176 0.9175 0.9205
0.1 0.9224 0.92226 0.9205 0.9208 0.9207
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid = none,
legend style={font=\small},
legend cell align=left,
legend pos=north east,
xmin=0.005, xmax=0.105,
ylabel={RMSE},
xlabel={$\lambda$},
%
xticklabel style={font=\small,
/pgf/number format/fixed},
yticklabel style={font=\small},
%
every axis plot post/.append style={line width=0.8pt},
%
mark size=1pt,
cycle list={
{blue,mark=*},
{black,mark=*},
{red,mark=*},
{yellow,mark=*},
{orange,mark=otimes*}% <-- don't add a comma here
},
]
\addplot table [y=A] {performance.data};
\addlegendentry{20 f}
\addplot table [y=B] {performance.data};
\addlegendentry{30 f}
\addplot table [y=C] {performance.data};
\addlegendentry{50 f}
\addplot table [y=D] {performance.data};
\addlegendentry{100 f}
\addplot table [y=E] {performance.data};
\addlegendentry{150 f}
\end{axis}
\end{tikzpicture}
\end{document}