Pgfplots:创建条形图

Pgfplots:创建条形图

下图是来自 google docs 的调查结果。我正在尝试使用 LaTeX (overleaf) 创建类似的条形图,但到目前为止我还没有得到相同的结果。 谷歌文档

背页 上面的输出图是我目前得到的结果。我遇到的问题是:

  1. 我不知道如何在每个条形图和数字旁边显示百分比(仅显示数字)。

  2. “非字母数字符号(标点符号、#、&、@ 等)”也必须进行编辑,因为当我尝试保留它时,其他 3 个条目(数字、大写字母、小写字母)及其条消失了。

  3. 我正在使用,\documentclass[sigconf, nonacm, 12 pt, a4paper]{acmart}所以它是双列的。如何让图表横跨两列?我知道 {figure*} 应该可以解决问题,但图表看起来仍然有点奇怪。

这是我用来创建图表的代码:

\documentclass[sigconf, nonacm, 12 pt, a4paper]{acmart}
\usepackage{amsmath} 
\usepackage{pgf-pie}  
\usepackage{pgfplots} 
\pgfplotsset{compat=1.8} 
\usepackage{graphicx} 
\usepackage{xcolor} 
\usepackage{tikz}

\begin{document}

Text...

\begin{figure*} 
\centering
\caption{Characters Included in a Password}
\label{fig:Chara}
\begin{tikzpicture}
\begin{axis}[
xbar, xmin=0,
symbolic y coords={Nonalphanumeric symbols "punctuation marks etc.",Numbers, Uppercase Letters, Lowercase Letters},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
]
\addplot coordinates {(64,Nonalphanumeric symbols "punctuation marks etc.") (85,Numbers) (62,Uppercase Letters) (83,Lowercase Letters)};
\end{axis}
\end{tikzpicture}
\end{figure*}

\bibliographystyle{ACM-Reference-Format}
\bibliography{sample}


\end{document}

如果我遗漏了什么请告诉我。

答案1

这可能有点复杂,但看起来更好。我清空了 ytick 标签,并\node在轴环境之外手动添加它们。我在acmart课堂上没有这样做,所以我没有测试两列的行为。我希望它能正常工作。我还更改了图下方的标题位置。我认为这是标准的。如果不是你的情况,只需将其移回。这是代码:

\documentclass[sigconf,nonacm,12pt,a4paper]{acmart}
\usepackage{amsmath} 
\usepackage{pgf-pie}  
\usepackage{pgfplots} 
\pgfplotsset{compat=1.8} 
\usepackage{graphicx} 
\usepackage{xcolor} 
\usepackage{tikz}

\begin{document}
Text...

\begin{figure*}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[%
width=0.81\textwidth, height=3.5in,
xbar, bar width=20pt,
xmin=0, xmax=100,
symbolic y coords={Nonalphanumeric, Numbers, Uppercase Letters, Lowercase Letters},
ytick=\empty,
xmajorgrids
]
\addplot [fill=blue!30,draw=blue] coordinates {(64,Nonalphanumeric)  (85,Numbers) (62,Uppercase Letters) (83,Lowercase Letters)}
node [minimum size=0pt,inner sep=0pt,right,pin={[font=\footnotesize,pin distance=0.3cm]0:64 (52.5\%)}] at (axis cs:64,Nonalphanumeric) {}
node [minimum size=0pt,inner sep=0pt,right,pin={[font=\footnotesize,pin distance=0.3cm]0:85 (69.7\%)}] at (axis cs:85,Numbers) {}
node [minimum size=0pt,inner sep=0pt,right,pin={[font=\footnotesize,pin distance=0.3cm]0:62 (50.8\%)}] at (axis cs:62,Uppercase Letters) {}
node [minimum size=0pt,inner sep=0pt,right,pin={[font=\footnotesize,pin distance=0.3cm]0:83 (68\%)}] at (axis cs:83,Lowercase Letters) {}
coordinate (symb) at (axis cs:0,Nonalphanumeric)
coordinate (num) at (axis cs:0,Numbers)
coordinate (uprcase) at (axis cs:0,Uppercase Letters)
coordinate (lwrcase) at (axis cs:0,Lowercase Letters);
\end{axis}
\node [font=\small,above left] at (symb) {Nonalphanumeric symbols};
\node [font=\small,left] at (symb) {(punctuation marks,};
\node [font=\small,below left] at (symb) {\#, \&, @, etc.)};
\node [font=\small,left] at (num) {Numbers};
\node [font=\small,left] at (uprcase) {Uppercase Letters};
\node [font=\small,left] at (lwrcase) {Lowercase Letters};
\end{tikzpicture}
\caption{Characters Included in a Password}
\label{fig:Chara}
\end{figure*}

\bibliographystyle{ACM-Reference-Format}
\bibliography{sample}
\end{document}

在此处输入图片描述

答案2

我需要添加另一个答案。这是一种更简单的方法。大大缩短了代码。并且不需要任何额外的\node命令。我还使轴框架和条形颜色接近谷歌文档。以下是代码:

\documentclass[sigconf,nonacm,12pt,a4paper]{acmart}
\usepackage{amsmath} 
\usepackage{pgf-pie}  
\usepackage{pgfplots} 
\pgfplotsset{compat=1.8} 
\usepackage{graphicx} 
\usepackage{xcolor}
\usepackage{tikz}

\begin{document}
Text...
\begin{figure*}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[%
width=0.85\textwidth, height=3.5in,
xbar, bar width=20pt,
xmin=0, xmax=100,
symbolic y coords={symb, num, uprcase, lwrcase},
ytick=data,
yticklabels={{Nonalphanumeric symbols (punctuation marks, \#, \&, @, etc.)},Numbers,Uppercase Letters,Lowercase Letters},
y tick label style={align=right,text width=3.2cm},
xtick={0,20,40,60,80,100},
xmajorgrids,
axis line style={lightgray},
major tick style={draw=none},
nodes near coords,
point meta=explicit symbolic,
node near coords style={font=\footnotesize,right=1em,pin={[pin distance=1em]180:}}
]
\addplot [fill={rgb:red,23;green,105;blue,170},draw=none] coordinates {(64,symb) [64 (52.5\%)]  (85,num) [85 (69.7\%)] (62,uprcase) [62 (50.8\%)] (83,lwrcase) [83 (68\%)]};
\draw [line width=1.5pt] (current axis.south west) -- (current axis.north west);
\end{axis}
\end{tikzpicture}
\caption{Characters Included in a Password}
\label{fig:Chara}
\end{figure*}

\bibliographystyle{ACM-Reference-Format}
\bibliography{sample}
\end{document}

在此处输入图片描述

相关内容