帶有 log y 軸的 Tikz 圖不顯示 1 值

帶有 log y 軸的 Tikz 圖不顯示 1 值

當我將 y 軸設定為對數時,不會渲染值範圍 [0,1) 的條形,如下所示:

渲染圖

這是為什麼?我該如何解決它?

\documentclass{article}
\usepackage{filecontents,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.18}

\begin{filecontents*}{data.dat}
1,3
2,2
3,1
4,1
5,0
6,1
6,2
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  ymode=log
]
\addplot [color=gray,fill] table [
    x index=0,
    y index=1,
    col sep=comma
] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

您詢問的是值範圍 [0,1),但在此範圍內沒有任何值。在對數軸上包含零沒有意義。

我已經更改了您的測試值,並log origin=infty假設您希望條形圖從軸的底部開始。

\begin{filecontents}{data.dat}
1,0.1
2,0.2
3,0.3
4,0.4
5,1
6,2
7,3
\end{filecontents}
\documentclass[tikz, border=1cm]{standalone}
\usepackage{filecontents, pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymode=log,
log origin=infty,
]
\addplot[gray, fill] table[
x index=0,
y index=1,
col sep=comma
] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}

對數 y 長條圖

沒有log origin=infty

從 1 開始的對數 y 長條圖

答案2

coordinates這是使用和 的解決方案ymin

\documentclass{article}
\usepackage{filecontents,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  ymode=log,
  ymin=1e-2
]
\addplot [color=gray,fill] coordinates {
    (1,3)
    (2,2)
    (3,1)
    (4,1)
    (5,0)
    (6,1)
    (7,0.1)    
};
\end{axis}
\end{tikzpicture}
\end{document}

酒吧

相關內容