
對於下面的 MWE:
\documentclass{report}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[%
scaled y ticks = true,
width=1*\textwidth,
height=8cm,
xlabel={Query $\#$},
ylabel={Elapsed Time (in seconds)},
grid=both,
minor x tick num=5,
minor y tick num=5,
enlarge x limits=0,
legend entries={Original, byHalf, Random, Hybrid},
scaled x ticks = true
]
\addplot [
color=blue,mark=x,
solid,
line width=1.0pt
]
coordinates{
(0,39.0819) (1,2292) (2,4484.82) (3,6057.37) (4,38.3475) (5,15.4616) (6,38.5286) (7,15.411) (8,38.3627) (9,14.8773) (10,15.4079) (11,38.8762) (12,20.9027) (13,35.6657)
};
\addplot [
color=red,mark=x,
solid,
line width=1.0pt
]
coordinates{
(0,38.9575) (1,2720.19) (2,1929.3) (3,1677.22) (4,111.165) (5,68.6833) (6,94.4457) (7,76.0591) (8,94.0201) (9,109.844) (10,78.6465) (11,91.7805) (12,12.9179) (13,78.6684)
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
如果你編譯它,你會發現情節的最後一部分看起來好像線條互相接觸,它們看起來是一樣的,但實際上它們之間有大約 50 秒的時間。發生這種情況是因為 y 軸以 1000 為步長(自動)前進。
我怎麼能控制這個?是否可以先讓它以 100 步進行,然後當達到 1000 時,切換到 1000 步?
答案1
您目前使用的軸是線性的。這意味著,如果您在 0 到 1000 之間添加更多點,則只會在相同距離內添加更多線條和標籤(0 到 1000 之間的距離仍然相同)。
若要反白顯示 0 和 1000 之間的差異,可以使用對數刻度。使用對數刻度時,0 和 10 之間的距離將與 10 和 100 或 100 和 1000(依此類推)之間的距離相同。
您的範例將如下所示,帶有對數 y 軸:
\documentclass{report}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}
\begin{semilogyaxis}[%
width=1*\textwidth,
height=8cm,
xlabel={Query $\#$},
ylabel={Elapsed Time (in seconds)},
grid=both,
enlarge x limits=0,
legend entries={Original, byHalf, Random, Hybrid},
scaled x ticks = true
]
\addplot [color=blue,mark=x,solid,line width=1.0pt]
coordinates{
(0,39.0819) (1,2292) (2,4484.82) (3,6057.37) (4,38.3475) (5,15.4616) (6,38.5286) (7,15.411) (8,38.3627) (9,14.8773) (10,15.4079) (11,38.8762) (12,20.9027) (13,35.6657)
};
\addplot [color=red,mark=x,solid,line width=1.0pt]
coordinates{
(0,38.9575) (1,2720.19) (2,1929.3) (3,1677.22) (4,111.165) (5,68.6833) (6,94.4457) (7,76.0591) (8,94.0201) (9,109.844) (10,78.6465) (11,91.7805) (12,12.9179) (13,78.6684)
};
\end{semilogyaxis}
\end{tikzpicture}
\end{figure}
\end{document}
而不是使用
\begin{semilogyaxis}
...
\end{semilogyaxis}
你也可以使用
\begin{axis}[xmode=normal,ymode=log]
...
\end{axis}
有關更多設定和信息,請查看pgf圖文件(第 16-19 頁)。