我编写的带有汇总箱线图的水平条形图代码正在整理中,但如果我尝试绘制箱线图值,则仍然存在一个困扰我的 PGF 数学问题,这些值已在代码底部注释掉:
! Package PGF Math Error: Could not parse input 'auto' as a floating point numb
er, sorry. The unreadable part was near 'auto'..
See the PGF Math package documentation for explanation.
Type H <return> for immediate help.
...
l.139 \end{tikzpicture} }
我的 MikTeX 有 PGFPLOTS 1.10 并且正在使用它。我一直在查阅 PGFPLOTS 和 PGFPLOTSTABLE 的手册,以及这里的论坛,寻找并找到了很多有用的信息。我的代码如下:
\documentclass[margin=1cm]{standalone}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsmath,relsize}
\usepackage{filecontents}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{statistics}
\pgfplotsset{every axis/.append style={
semithick,tick style={
major tick length=4pt,thick,black}},
compat=1.10,
%
discard if not/.style 2 args={
x filter/.code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\ifx\tempa\tempb
\else
\def\pgfmathresult{inf}
\fi
}
},
}
\pgfkeys{/pgfplots/x axis shift up/.style={
x axis line style={yshift=#1},
xtick style={yshift=#1},
xticklabel shift={#1}}}
\pgfkeys{/pgfplots/y axis shift left/.style={
y axis line style={xshift=-#1},
ytick style={xshift=-#1},
yticklabel shift={#1}}}
\begin{document}
\sffamily
\begin{filecontents}{data.csv}
Miles, Ride, Bike, rdate
12.5, 1, Yel-11, {$\stackrel{\hbox{Yel-11}}{\hbox{\tiny 03/30/14}}$}
16.5, 2, Fuji, {$\stackrel{\hbox{Fuji}}{\hbox{\tiny 04/23/14}}$}
21.0, 3, 8000SHX, {$\stackrel{\hbox{8000SHX}}{\hbox{\tiny 05/18/14}}$}
24.2, 4, Yel-11, {$\stackrel{\hbox{Yel-11}}{\hbox{\tiny 05/24/14}}$}
23.4, 5, YFoil, {$\stackrel{\hbox{YFoil 77}}{\hbox{\tiny 05/29/14}}$}
21.1, 6, Y-22, {$\stackrel{\hbox{Y-22}}{\hbox{\tiny 05/30/14}}$}
23.9, 7, YFoil, {$\stackrel{\hbox{YFoil 77}}{\hbox{\tiny 06/01/14}}$}
32.5, 8, Fuji, {$\stackrel{\hbox{Fuji}}{\hbox{\tiny 06/05/14}}$}
\end{filecontents}
\pgfplotstablegetrowsof{data.csv}
\edef\numberofrows{\pgfplotsretval}
\pgfmathsetmacro\numberofticks{\numberofrows-1}
\fbox{
\begin{tikzpicture}
\begin{axis}[clip=false, domain=0:50, unbounded coords=jump,
width=0.90\linewidth,height=8.5cm,
xbar,/pgf/bar shift=0pt,
title={\centering Bike Miles for 2014},
title style={yshift=1cm, text width=0.90\linewidth,
font=\rmfamily\itshape\Large},
xmin=0, xmax=50, xticklabel pos=upper,
tick align=outside,
xticklabel style={major tick length=20pt,
color=black, tickwidth={10pt}},
axis x line*=top, axis y line*=left,
y axis line style={opacity=0},
xmajorgrids, major grid style=black, axis on top,
xtick={0,10,15,20,25,30,35,40,50},
separate axis lines, x axis shift up=15pt,
y axis shift left=10pt, enlarge y limits=0.0,
ytick={0,...,\numberofticks}, y dir=reverse,
y tick label style={align=right, major tick length=5pt},
yticklabels from table={data.csv}{[index]3},
nodes near coords, nodes near coords align=horizontal,
]
\addplot [draw,fill=blue!75!black, discard if not={Bike}{Fuji}]
table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};
\addplot [draw,fill=blue, discard if not={Bike}{YFoil}]
table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};
\addplot [draw,fill=yellow, discard if not={Bike}{Yel-11}]
table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};
\addplot [draw,fill=gray, discard if not={Bike}{8000SHX}]
table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};
\addplot [draw,fill=red, discard if not={Bike}{Y-22}]
table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};
\draw [red,dotted,thick] (axis cs:21.9,-0.5) -- (axis cs:21.9,\numberofrows);
\draw [black] (axis cs:12.5,-0.5) -- (axis cs:32.5,-0.5);
\draw [red,thick=4mm] (axis cs:17.9,-0.5) -- (axis cs:25.9,-0.5)
node[anchor=north east] at (axis cs:21.9,-0.5){\small $ \mu$}
node[anchor=north east] at (axis cs:17.9,-0.5){\small $-\sigma$}
node[anchor=north west] at (axis cs:25.9,-0.5){\small $+\sigma$};
\addplot+[
boxplot prepared={ draw position=\numberofrows,
lower whisker =12.50,
lower quartile=19.88,
median=22.25,
upper quartile=23.98,
upper whisker =32.50,
box extend=0.75, % height of box
whisker extend=0.50, % height of whiskers
every box/.style={thick,dashed,draw=blue,fill=gray!50},
every whisker/.style={black,thick},
every median/.style={densely dotted,cyan,thick},
},
] coordinates{};
%%[above]
%%node[left] at
%%(boxplot box cs: \boxplotvalue{lower whisker},1)
%%{\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
%%node[left] at
%%(boxplot box cs: \boxplotvalue{lower quartile},1)
%%{\pgfmathprintnumber{\boxplotvalue{lower quartile}}}
%%node[left] at
%%(boxplot box cs: \boxplotvalue{median},0.5)
%%{\pgfmathprintnumber{\boxplotvalue{median}}}
%%node[right] at
%%(boxplot box cs: \boxplotvalue{average},0.5)
%%{\pgfmathprintnumber{\boxplotvalue{average}}}
%%node at
%%(boxplot box cs: \boxplotvalue{upper quartile},1)
%%{\pgfmathprintnumber{\boxplotvalue{upper quartile}}}
%%node[right] at
%%(boxplot box cs: \boxplotvalue{upper whisker},0.5)
%%{\pgfmathprintnumber{\boxplotvalue{upper whisker}}};
\draw node[anchor=center] at (axis cs:40.0,9) {\numberofrows\ rides for 175.1 miles as of 06/05/14};
\end{axis}
\end{tikzpicture} }%fbox
\end{document}
生成:
我尝试将手册和此处示例中的数据放入宏中,但这只会使情况恶化,而且不起作用。表格的读取方式肯定存在根本问题。我将添加到domain=0:50, unbounded coords=jump,
轴选项中,并且不再有关于未绘制每条数据线、每次 addplot 调用的超出范围点的投诉。
NOTE: coordinate (12.5,0Y0.0e0]) has been dropped because it is unbounded (in x
). (see also unbounded coords=jump).
似乎x=Miles, y expr=\coordindex
有问题,尽管它仍绘制了条形图。无论哪种情况,尝试打印/标记(lw、q1、q2、q3、uw)的箱线图值都会导致编译崩溃(上述错误消息)。
另外,如何将主网格线沿 x 轴向上移动并阻止它们延伸到底部栏?它们通常是白色的,因此只能通过栏看到。
我正在尝试使用这个简单/示例数据集来学习 PGFPLOTS。我的 iPad 上的 iBooks 中有两本 PGF 手册,但 pdf 格式导致 iBooks 经常崩溃。iPad 的 LaTeX Writer 应用程序可以很好地运行 PGFPLOTS,尽管 PGFPLOTS 必须手动更新到 1.10 版。
我将非常感激任何帮助。提前致谢。问候,戴夫