轴上有百分比的 pgfplots

轴上有百分比的 pgfplots

我正在尝试绘制一个 x 轴为百分比、y 轴为类别的图表。我无法让图表显示%符号。这是我的MWE

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S\&P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=0.5, 
    nodes near coords,
    nodes near coords align={horizontal}
    ]

  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(0.17,Sales)  (0.24,Marketing) (0.31,Finance) (0.42,Operations)};
\end{axis}
\end{tikzpicture}
\end{document}

如您所见,我可以用十进制数制作图表,理想情况下我希望有%符号。有什么建议吗?

答案1

您可以使用point meta执行乘以 100 和

nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}

添加百分号:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S\&P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=0.5, 
    point meta={x*100},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords align={horizontal}
    ]
  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(0.17,Sales)  (0.24,Marketing) (0.31,Finance) (0.42,Operations)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

如果您还希望 x 刻度标签采用这种格式,那么您可以使用

xticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},

(无需明确给出标签):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S\&P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=0.5, 
    xticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},
    point meta={x*100},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords align={horizontal},
    ]
  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(0.17,Sales)  (0.24,Marketing) (0.31,Finance) (0.42,Operations)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您可以使用xticklabel={\pgfmathprintnumber\tick\%}。:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S \& P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=100, 
    nodes near coords,
    nodes near coords align={horizontal},
    xtick={20,40,60,80,100},
    xticklabel=
{\pgfmathprintnumber\tick\%}
    ]

  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(17,Sales)  (24,Marketing) (31,Finance) (42,Operations)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容