由于缺乏真正的零,使用对数尺度的条形图可能不是一个好主意。
然而,考虑到数据偏差,似乎没有太多其他选择……否则较小的值会变得过于压缩而无法进行比较。
使用xbar
,pgfplots
我的想法是使用对数刻度X-轴,但使用轴上的不连续性来表示零问题。
代码:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{semilogxaxis}[
axis x discontinuity=crunch,
log basis x=2,
log origin=infty,
y post scale=0.4,
legend style={at={(0.5,1.1)},anchor=south},
legend columns=4,
ytick={one,two,eight,sixty-four},
symbolic y coords={one,two,eight,sixty-four},
bar width=7pt,
enlarge y limits=0.5
]
\addplot+[xbar] coordinates {(1,one)};
\addlegendentry{one}
\addplot+[xbar] coordinates {(2,two)};
\addlegendentry{two}
\addplot+[xbar] coordinates {(8,eight)};
\addlegendentry{eight}
\addplot+[xbar] coordinates {(64,sixty-four)};
\addlegendentry{sixty-four}
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
输出:
输出看起来不错(经过一些非 MWE 调整),但我在构建时收到两条错误消息:
错误:
! Missing number, treated as zero.
<to be read again>
\pgfplots@data@scale@trafo@SHIFT@x
l.30 \end{semilogxaxis}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Illegal unit of measure (pt inserted).
<to be read again>
\pgfplots@data@scale@trafo@SHIFT@x
l.30 \end{semilogxaxis}
我猜这是因为缺少“真实原点”来转移轴上的不连续性。但是,输出实现了我想要的。因此...
有人能建议一种方法来修复这个错误吗?否则,我该如何抑制这样的错误?
(如果有人有建议的话我想我也会想用“0”标记 x 原点。)
答案1
您可以使用装饰来放置轴不连续性。下面示例中的装饰改编自使用 tikz 绘制不连续符号。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfdeclaredecoration{discontinuity}{start}{
\state{start}[width=0.04\pgfdecoratedinputsegmentremainingdistance-0.5\pgfdecorationsegmentlength,next state=up from center]
{}
\state{up from center}[width=+.5\pgfdecorationsegmentlength, next state=big down]
{
\pgfpathlineto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
}
\state{big down}[next state=center finish]
{
\pgfpathlineto{\pgfqpoint{.25\pgfdecorationsegmentlength}{-\pgfdecorationsegmentamplitude}}
}
\state{center finish}[width=0.5\pgfdecoratedinputsegmentremainingdistance, next state=do nothing]{
\pgfpathlineto{\pgfpointorigin}
\pgfpathlineto{\pgfpointdecoratedinputsegmentlast}
}
\state{do nothing}[width=\pgfdecorationsegmentlength,next state=do nothing]{
\pgfpathlineto{\pgfpointdecoratedinputsegmentlast}
}
\state{final}
{
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
\begin{document}
\begin{tikzpicture}
\begin{semilogxaxis}[
log basis x=2,
log origin=infty,
y=0.5cm,
legend style={at={(0.5,1.1)},anchor=south},
legend columns=-1,
ytick={one,two,eight,sixty-four},
symbolic y coords={one,two,eight,sixty-four},
bar width=7pt,
enlarge y limits=0.5,
enlarge x limits={0.15},
separate axis lines,
every outer x axis line/.append style=
{decoration={discontinuity, segment length=3mm}, decorate},
]
\addplot+[xbar] coordinates {(1,one)};
\addlegendentry{one}
\addplot+[xbar] coordinates {(2,two)};
\addlegendentry{two}
\addplot+[xbar] coordinates {(8,eight)};
\addlegendentry{eight}
\addplot+[xbar] coordinates {(64,sixty-four)};
\addlegendentry{sixty-four}
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
在我看来,对数轴上的不连续符号(这在数学上确实没有任何意义)的更好替代方案是使用散点图。为了显示数据本质上是一维的,您可以使用 y 网格:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{semilogxaxis}[
log basis x=2,
log origin=infty,
y=0.5cm,
legend style={at={(0.5,1.1)},anchor=south},
legend columns=-1,
ytick={one,two,eight,sixty-four},
symbolic y coords={one,two,eight,sixty-four},
bar width=7pt,
enlarge y limits=0.5,
enlarge x limits={0.15},
ymajorgrids=true
]
\addplot coordinates {(1,one)};
\addlegendentry{one}
\addplot coordinates {(2,two)};
\addlegendentry{two}
\addplot coordinates {(8,eight)};
\addlegendentry{eight}
\addplot coordinates {(64,sixty-four)};
\addlegendentry{sixty-four}
\end{semilogxaxis}
\end{tikzpicture}
\end{document}