我需要根据文件中的数据绘制条形图。数据看起来像这样
\begin{filecontents*}{tls.dat}
Name Abbreviation percentage
Bod-Kin LS1 60
Intraper LS2 40
Verb-Ling LS3 20
Spat-Vis LS4 10
Natur LS5 5
Interper LS6 0
Mus LS7 0
Log-Math LS8 0
\end{filecontents*}
条形图仅涉及百分比值高于 0 的行,并且它看起来像这样。
到目前为止,我已经设法读取数据,并根据百分比对其进行排序,并且可以手动绘制动态条形图,但我无法根据数据的最高值缩放条形的长度。
这是我的 MWE。
\documentclass[a4paper]{article}
\usepackage[left=43px,bottom=45.4px,right=46px,top=35px]{geometry}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{color}
\usepackage{tikz}
\usepackage{afterpage}
\usetikzlibrary{spy}
\usetikzlibrary{fit,calc,positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\usepackage{readarray}
\usepackage{array}
\begin{filecontents*}{tls.dat}
Name Abbreviation percentage
Bod-Kin LS1 60
Intraper LS2 40
Verb-Ling LS3 20
Spat-Vis LS4 10
Natur LS5 5
Interper LS6 0
Mus LS7 0
Log-Math LS8 0
\end{filecontents*}
\definecolor{backgroundgray}{HTML}{EDEDED}
\definecolor{hashc}{HTML}{00A3E5}
\begin{document}
\newgeometry{top=35px, left=44px, right=43.7px,bottom=60.4px}
\pagecolor{backgroundgray}\afterpage{\nopagecolor}
\newcounter{mya}
%% calculate how many attributes have positive value
\pgfplotstableread[columns={Name,Abbreviation,percentage}]{tls.dat}\datatableA
\pgfplotstablesort[sort key={percentage}, sort cmp=int >]{\datatablesorted}{\datatableA}
\pgfplotstablegetrowsof{\datatablesorted}
\pgfmathtruncatemacro{\RowsInTable}{\pgfplotsretval-1}
\pgfmathtruncatemacro{\TotalRows}{\pgfplotsretval}
\foreach \k in {0,...,\RowsInTable}{
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\pgfmathtruncatemacro{\itest}{sign(\pgfplotsretval)}
\ifnum\itest=1
\stepcounter{mya}
\fi}
\noindent\fcolorbox{white}{white}{
\begin{minipage}[t][]{230px}
\vspace*{15px}
\begin{center}
\fontsize{16px}{30px}\color{gray}\textsl{TLS}
\end{center}
\addtocounter{mya}{-1}
\begin{tikzpicture}
%% draw names of bar plot
\pgfplotstablegetelem{0}{percentage}\of{\datatablesorted}
\def\maxlength{\pgfplotsretval}
\foreach \k in {0,...,\value{mya}}{
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\pgfmathtruncatemacro{\itest}{sign(\pgfplotsretval)}
\pgfplotstablegetelem{\k}{Abbreviation}\of{\datatablesorted}
\node at (0,-\k) {\fontsize{12px}{16px}\selectfont\color{gray}\pgfplotsretval};
%draw bar plots
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\pgfmathsetmacro\curlength{\pgfplotsretval}
\pgfmathsetmacro\lengthpernodeA{0.9*\curlength / \maxlength}
\fill [hashc] (0.5,-\k-0.25) rectangle (\lengthpernodeA\textwidth,0.25-\k);
}
\end{tikzpicture}
\end{minipage}
}
\end{document}
MWE 给了我这个
答案1
抱歉低估了这个问题!首先让我解释一下你的代码出了什么问题。当你说更新\def\maxlength{\pgfplotsretval}
时\maxlength
将会更新\pgfplotsretval
。但是,如果你扩展值,即使用\edef\maxlength{\pgfplotsretval}
(不,e
不代表 egreg ;-) 那么它就可以工作。这就是以下代码的作用,其中我删除了这里不需要的包。
\documentclass[a4paper]{article}
\usepackage[left=43px,bottom=45.4px,right=46px,top=35px]{geometry}
\usepackage{tikz}
\usepackage{afterpage}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\begin{filecontents*}{tls.dat}
Name Abbreviation percentage
Bod-Kin LS1 60
Intraper LS2 40
Verb-Ling LS3 20
Spat-Vis LS4 10
Natur LS5 5
Interper LS6 0
Mus LS7 0
Log-Math LS8 0
\end{filecontents*}
\definecolor{backgroundgray}{HTML}{EDEDED}
\definecolor{hashc}{HTML}{00A3E5}
\begin{document}
\newgeometry{top=35px, left=44px, right=43.7px,bottom=60.4px}
\pagecolor{backgroundgray}\afterpage{\nopagecolor}
\newcounter{mya}
%% calculate how many attributes have positive value
\pgfplotstableread[columns={Name,Abbreviation,percentage}]{tls.dat}\datatableA
\pgfplotstablesort[sort key={percentage}, sort cmp=int >]{\datatablesorted}{\datatableA}
\pgfplotstablegetrowsof{\datatablesorted}
\pgfmathtruncatemacro{\RowsInTable}{\pgfplotsretval-1}
\pgfmathtruncatemacro{\TotalRows}{\pgfplotsretval}
\foreach \k in {0,...,\RowsInTable}{
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\pgfmathtruncatemacro{\itest}{sign(\pgfplotsretval)}
\ifnum\itest=1
\stepcounter{mya}
\fi}
\noindent\fcolorbox{white}{white}{
\begin{minipage}[t][]{230px}
\vspace*{15px}
\begin{center}
\fontsize{16px}{30px}\color{gray}\textsl{TLS}
\end{center}
\addtocounter{mya}{-1}
\begin{tikzpicture}
%% draw names of bar plot
\pgfplotstablegetelem{0}{percentage}\of{\datatablesorted}
\edef\maxlength{\pgfplotsretval} % <----
\foreach \k in {0,...,\value{mya}}{
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\pgfmathtruncatemacro{\itest}{sign(\pgfplotsretval)}
\pgfplotstablegetelem{\k}{Abbreviation}\of{\datatablesorted}
\node at (0,-\k) {\fontsize{12px}{16px}\selectfont\color{gray}\pgfplotsretval};
%draw bar plots
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\edef\curlength{\pgfplotsretval}
\pgfmathsetmacro\lengthpernodeA{(0.9*\curlength /\maxlength)}
\fill [hashc] (0.5,-\k-0.25) rectangle (\lengthpernodeA\textwidth,0.25-\k);
}
\end{tikzpicture}
\end{minipage}
}
\end{document}
这是使用 pgfplots 的 xbar 图的替代方法。它可能更容易定制。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{tls.dat}
Name Abbreviation percentage
Bod-Kin LS1 60
Intraper LS2 40
Verb-Ling LS3 20
Spat-Vis LS4 10
Natur LS5 5
Interper LS6 0
Mus LS7 0
Log-Math LS8 0
\end{filecontents*}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.16}
\definecolor{backgroundgray}{HTML}{EDEDED}
\definecolor{hashc}{HTML}{00A3E5}
\newcounter{mya}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[columns={Name,Abbreviation,percentage}]{tls.dat}\datatableA
\pgfplotstablesort[sort key={percentage}, sort cmp=int <]{\datatablesorted}{\datatableA}
\pgfplotstablegetrowsof{\datatablesorted}
\pgfmathtruncatemacro{\RowsInTable}{\pgfplotsretval-1}
\pgfmathtruncatemacro{\TotalRows}{\pgfplotsretval}
\setcounter{mya}{0}
\foreach \k in {0,...,\RowsInTable}{
\pgfplotstablegetelem{\k}{percentage}\of{\datatablesorted}
\pgfmathtruncatemacro{\itest}{ifthenelse(\pgfplotsretval>0,1,0)}
\ifnum\itest=1
\typeout{\k,\pgfplotsretval}
\pgfplotstablegetelem{\k}{Abbreviation}\of{\datatablesorted}
\typeout{\pgfplotsretval}
\ifnum\number\value{mya}=0
\xdef\LstAbbr{"\pgfplotsretval"}
\else
\xdef\LstAbbr{\LstAbbr,"\pgfplotsretval"}
\fi
\stepcounter{mya}
\fi}
\begin{axis}[xbar,hide axis,clip=false]
\addplot [draw=none,fill=hashc,
y filter/.expression={
x<=0 ? nan : y
}] table[y expr=\coordindex,x=percentage] {\datatablesorted};
\pgfplotsinvokeforeach{1,...,\number\value{mya}}{
\node[left,gray] at (axis cs:0,#1-\number\value{mya}+\RowsInTable){\pgfmathparse{{\LstAbbr}[#1-1]}\pgfmathresult};}
\end{axis}
\end{tikzpicture}
\end{document}
具有不同分配的非平凡测试:
\begin{filecontents*}{tls.dat}
Name Abbreviation percentage
Bod-Kin LS1 60
Intraper LS3 40
Verb-Ling LS4 20
Spat-Vis LS2 10
Natur LS5 5
Interper LS6 0
Mus LS7 0
Log-Math LS8 0
\end{filecontents*}
导致