Pgfplots:建立長條圖

Pgfplots:建立長條圖

下圖是google docs的調查結果。我正在嘗試使用 LaTeX (背面)創建一個類似的條形圖,但到目前為止我還無法獲得相同的結果。 Google文檔

背頁 輸出圖 aboce 是我迄今為止得到的結果。我遇到的問題是:

  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}

在此輸入影像描述

相關內容