pgfplots:缩放 x 轴(手动设置 x 轴限制、最小值/最大值)

pgfplots:缩放 x 轴(手动设置 x 轴限制、最小值/最大值)

我创建了几个带有百分比的水平条形图。有没有办法强制 x 轴从 0 到 100,以便更容易与其他图表进行比较?

\begin{tikzpicture}
\begin{axis}[xbar,
y axis line style = { opacity = 0 },
axis x line       = none,
tickwidth         = 0pt,
enlarge y limits  = 0.33,
enlarge x limits  = 0.02,
symbolic y coords = {A, B, C},
nodes near coords, nodes near coords align={horizontal}]
\addplot[fill = black] coordinates { (43.0,A)   (30.1,B) (41.7,C)};
\end{axis}
\end{tikzpicture}

答案1

  • 使用选项对xmin和可以轻松完成此操作xmax
  • 看起来你的代码基于tikz xbar 额外坐标。如果是,那么下次请指出这一点。

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\section*{Original Code from Question}
\begin{tikzpicture}
\begin{axis}[xbar,
y axis line style = { opacity = 0 },
axis x line       = none,
tickwidth         = 0pt,
enlarge y limits  = 0.33,
enlarge x limits  = 0.02,
symbolic y coords = {A, B, C},
nodes near coords, nodes near coords align={horizontal}]
\addplot[fill = black] coordinates { (43.0,A)   (30.1,B) (41.7,C)};
\end{axis}
\end{tikzpicture}

\section*{Modified Code}

\begin{tikzpicture}
    \begin{axis}
        [
            xbar,
            y axis line style = {opacity = 0},
            axis x line  = none,            
            tickwidth = 0pt,
            %enlarge y limits  = 0.33, % <-- removed due to suggestion of user https://tex.stackexchange.com/users/38080/rmano 
            %enlarge x limits  = 0.02, % <-- removed due to suggestion of user https://tex.stackexchange.com/users/38080/rmano 
            xmin = 0, % <-- added
            xmax = 100, % <-- added
            symbolic y coords = {A, B, C},
            nodes near coords, 
            nodes near coords align = {horizontal},
        ]
    % Plot
    \addplot
        [
            fill = black
        ] 
        coordinates 
            {
                (43.0,A)   
                (30.1,B) 
                (41.7,C)
            };
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

奇怪的东西

  1. 在我修改之后出现了额外的 y 刻度。--> ytick = data,有帮助(见tikz xbar 额外坐标例如)。
  2. 使用(43.0, A)而不是(43.0,A)(额外空间)会导致错误消息。

    ! 包 pgfplots 错误:抱歉,输入坐标 `A' 未定义为 'symbolic y coords={A, B, C}... 可能是拼写错误?或者您指的是 [normalized] A? 之类的东西。


更新了“Strang(er) Things 1”的代码(删除了不需要的额外 y 刻度)

\begin{tikzpicture}
    \begin{axis}
        [
            xbar,
            y axis line style = {opacity = 0},
            axis x line  = none,            
            tickwidth = 0pt,
            %enlarge y limits  = 0.33, % <-- removed due to suggestion of user https://tex.stackexchange.com/users/38080/rmano 
            %enlarge x limits  = 0.02, % <-- removed due to suggestion of user https://tex.stackexchange.com/users/38080/rmano 
            xmin = 0, % <-- added
            xmax = 100, % <-- added
            ytick = data, % <-- added
            symbolic y coords = {A, B, C},
            nodes near coords, 
            nodes near coords align = {horizontal},
        ]
    % Plot
    \addplot
        [
            fill = black
        ] 
        coordinates 
            {
                (43.0,A)   
                (30.1,B) 
                (41.7,C)
            };
    \end{axis}
\end{tikzpicture}

Stefan Pinnow 评论后的更新

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \begin{axis}
        [
            height = 30mm, % added after Stefan Pinnow's comment
            width = \axisdefaultwidth, % added after Stefan Pinnow's comment
            xbar,
            % y axis line style = {opacity = 0}, removed after Stefan Pinnow's comment
            draw = none, % added after Stefan Pinnow's comment
            axis x line  = none,            
            tickwidth = 0pt,
            %enlarge y limits  = 0.33, % <-- removed due to suggestion of user https://tex.stackexchange.com/users/38080/rmano 
            %enlarge x limits  = 0.02, % <-- removed due to suggestion of user https://tex.stackexchange.com/users/38080/rmano 
            xmin = 0, % <-- added
            xmax = 100, % <-- added
            % ytick = data, % <-- added and later removed after Stefan Pinnow's comment
            ytick distance = 1, % <-- added instead after Stefan Pinnow's comment
            symbolic y coords = {A, B, C},
            nodes near coords, 
            nodes near coords align = {horizontal},
        ]
    % Plot
    \addplot
        [
            fill = black
        ] 
        coordinates 
            {
                (43.0,A)   
                (30.1,B) 
                (41.7,C)
            };
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容