如何缩放(tikz,pgfplots)条形图中的 y 轴?

如何缩放(tikz,pgfplots)条形图中的 y 轴?

我有一个条形图,其中有些条形比其他条形大,如下所示:

在此处输入图片描述

我的问题是我看不到小条(例如,点 2 或 4)。我们可以更改一些参数来缩放 y 轴吗?

我的代码如下:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}[ht!]
  \centering
\resizebox{.65\textwidth}{!}{%
    \begin{tikzpicture}
      \begin{axis}[
        xlabel={Number},
        ylabel={Percentage},
        set layers,
        ybar=1.2pt,
        bar width=6pt,
        symbolic x coords={2,4,6,8,10},
        grid=both,
        ymin=0,
        samples=5,
        legend cell align=left,
        ymax=1,
        x label style={font=\footnotesize},
        y label style={font=\footnotesize},
        ticklabel style={font=\footnotesize},
        legend style={at={(0,.82)},anchor=west,font=\scriptsize},
        ]
        %% 
        \addplot[black,fill=red,postaction={pattern=north east lines}] coordinates {
          (2, 0.005) (4, 0.001) (6, 0.2) (8, 0.2) (10, 0.4)
        };
        \addlegendentry{A}
        %% 
        \addplot[black,fill=brown,postaction={pattern=north west lines}] coordinates {
          (2, 0.002) (4, 0.004) (6, 0.2) (8, 0.5) (10, 0.8)
        };
        \addlegendentry{B}
        %% 
        \addplot[black,fill=violet,postaction={pattern=crosshatch}] coordinates {
          (2, 0.2) (4, 0.007) (6, 0.4) (8, 0.7) (10, 0.9)
        };
        \addlegendentry{C}
      \end{axis}
    \end{tikzpicture}
}
  \caption{The percentage.}
  \label{figure:1}
\end{figure}
\end{document}

答案1

演示

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}

\pgfplotsset{compat=newest}

\begin{document}

\begin{figure}[ht!]
  \centering
\resizebox{.65\textwidth}{!}{%
    \begin{tikzpicture}
      \begin{semilogyaxis}[
        xlabel={Number},
        ylabel={Percentage},
        set layers,
        ybar=1.2pt,
        bar width=6pt,
        symbolic x coords={2,4,6,8,10},
        grid=both,
        samples=5,
        legend cell align=left,
        ymax=1,
        x label style={font=\footnotesize},
        y label style={font=\footnotesize},
        ticklabel style={font=\footnotesize},
        legend style={at={(0,.82)},anchor=west,font=\scriptsize},
        log origin y=infty
        ]
        %% 
        \addplot[black,fill=red,postaction={pattern=north east lines}] coordinates {
          (2, 0.005) (4, 0.001) (6, 0.2) (8, 0.2) (10, 0.4)
        };
        \addlegendentry{A}
        %% 
        \addplot[black,fill=brown,postaction={pattern=north west lines}] coordinates {
          (2, 0.002) (4, 0.004) (6, 0.2) (8, 0.5) (10, 0.8)
        };
        \addlegendentry{B}
        %% 
        \addplot[black,fill=violet,postaction={pattern=crosshatch}] coordinates {
          (2, 0.2) (4, 0.007) (6, 0.4) (8, 0.7) (10, 0.9)
        };
        \addlegendentry{C}
      \end{semilogyaxis}
    \end{tikzpicture}
}
  \caption{The percentage.}
  \label{figure:1}
\end{figure}
\end{document}

相关内容