如何为 y 刻度标签添加单位?

如何为 y 刻度标签添加单位?

我想在此图表的 y 刻度标签(而不是 y 标签)中添加“%”作为单位:

我的第一张图表

这是我为这个情节编写的代码:

\begin{tikzpicture} 
    \begin{axis}[   
        ybar,
        ymajorgrids,
        axis lines*       = left,       
        width             = 11cm,
        xlabel            = {Jahr},
        ylabel            = {Zufriedenheit},
        enlargelimits     = 0.15,
        xtick             = {2016, 2017, 2018, 2019},
        symbolic x coords = {2016, 2017, 2018, 2019},
        legend style      = {at = {(0.5,-0.15)}, anchor = north, legend columns = -1},
    ]
    
        % Jest:
        \addplot[red!20!black,fill=red!80!white] coordinates
        {(2016,58.8) (2017,93.7) (2018,96.5) (2019,96.4)};
    
        % Mocha:
        \addplot[red!20!black,fill=blue!80!white]coordinates
        {(2016,93.8) (2017,87.3) (2018,82.3) (2019,77.7)};
    
        % Jasmine:
        \addplot[red!20!black,fill=orange!80!white] coordinates
        {(2016,83.3) (2017,79.6) (2018,74.8) (2019,66.9)};
    
        % Enzyme:
        \addplot[red!20!black,fill=yellow!80!white] coordinates
        {(2016,94.9) (2017,94.4) (2018,88.4) (2019,71.3)};
    
        % Ava
        \addplot[red!20!black,fill=green!80!white] coordinates 
        {(2016,85.4) (2017,75.0) (2018,71.6) (2019,64.4)};
    
       \legend{Jest, Mocha, Jasmine, Enzyme, Ava}
   \end{axis}
\end{tikzpicture}

我对 pgfplots 还很陌生,并尝试使用符号 y 刻度:

我的第二张图表

\begin{tikzpicture} 
    \begin{axis}[   
        ybar,
        ymajorgrids,
        axis lines*       = left,       
        width             = 11cm,
        xlabel            = {Jahr},
        ylabel            = {Zufriedenheit},
        enlargelimits     = 0.15,
        xtick             = {2016, 2017, 2018, 2019},
        symbolic x coords = {2016, 2017, 2018, 2019},
        ytick             = {58.8\%, 66.9\%, 74.8\%, 77.7\%, 79.6\%, 82.3\%,  83.3\%, 87.3\%, 93.7\%, 93.8\%, 96.4\%, 96.5\%},
        symbolic y coords = {58.8\%, 66.9\%, 74.8\%, 77.7\%, 79.6\%, 82.3\%,  83.3\%, 87.3\%, 93.7\%, 93.8\%, 96.4\%, 96.5\%},
        legend style      = {at = {(0.5,-0.15)}, anchor = north, legend columns = -1},
    ]
        
        % Jest:
        \addplot[red!20!black,fill=red!80!white] coordinates
        {(2016,58.8\%) (2017,93.7\%) (2018,96.5\%) (2019,96.4\%)};
        
        % Mocha:
        \addplot[red!20!black,fill=blue!80!white]coordinates
        {(2016,93.8\%) (2017,87.3\%) (2018,82.3\%) (2019,77.7\%)};
        
        % Jasmine:
        \addplot[red!20!black,fill=orange!80!white] coordinates
        {(2016,83.3\%) (2017,79.6\%) (2018,74.8\%) (2019,66.9\%)};
        
        % Enzyme:
        %\addplot[red!20!black,fill=yellow!80!white] coordinates
        %{(2016, 94.9) (2017, 94.4) (2018, 88.4) (2019, 71.3)};
        
        % Ava
        %\addplot[red!20!black,fill=green!80!white] coordinates 
        %{(2016, 85.4) (2017, 75.0) (2018, 71.6) (2019, 64.4)};
        
        %\legend{Jest, Mocha, Jasmine, Enzyme, Ava}
    \end{axis}
\end{tikzpicture}

这种方法可行,但问题是,y 轴上的 y 刻度太多了。理想情况下,我想使用 PGFPLOTS 的自动缩放功能来显示 y 轴上的刻度。我相信一定有更好的方法可以做到这一点,对吧?

答案1

使用yticklabel={$\pgfmathprintnumber{\tick}$\%},您的示例,我得到以下图像。 在此处输入图片描述

相关内容