我想创建一个水平条形图,每个条形的 y 轴上都有不同的标签。此外,值应显示在每个条形的右侧。
效果:
“测试 A”...红色条...400
“测试 B”...橙色条...150 等等。
电流输出
当前代码
\documentclass{文章}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\newlength{\xdim}
\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
legend style={
legend columns=4,
at={(xticklabel cs:0.5)},
anchor=north,
draw=none
},
ytick=data,
axis y line*=none,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize},
xtick={0,100,200,300,400,500,600},
width=.9\textwidth,
bar width=6mm,
yticklabels={{Test A},
{Test B},
{Test C},
{Test D},
{Test E},
{Test F}},
xmin=0,
xmax=600,
area legend,
y=8mm,
enlarge y limits={abs=0.625},
]
\addplot[findOptimalPartition,fill=findOptimalPartition] coordinates
{(400,0) (0,0) (0,0) (0,0) (0,0) (0,0)};
\addplot[storeClusterComponent,fill=storeClusterComponent] coordinates
{(0,0) (150,1) (0,0) (0,0) (0,0) (0,0)};
\addplot[dbscan,fill=dbscan] coordinates
{(0,0) (0,0) (250,2) (0,0) (0,0) (0,0)};
\addplot[constructCluster,fill=constructCluster] coordinates
{(0,0) (0,0) (0,0) (50,3) (0,0) (0,0)};
\addplot[blue,fill=blue] coordinates
{(0,0) (0,0) (0,0) (0,0) (350,4) (0,0)};
\addplot[green,fill=green] coordinates
{(0,0) (0,0) (0,0) (0,0) (0,0) (100,5)};
\end{axis}
\end{tikzpicture}
\caption{X}
\label{fig:stats}
\end{figure}
\end{document}
答案1
我不知道如何使用 pgfplot 实现这一点,但使用简单的 tikz 您可以调整这一点(使用循环foreach
):
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\foreach \l/\x/\c[count=\y] in {find optimal partition/2/findOptimalPartition, store cluster component/3/storeClusterComponent, database scan/1.5/dbscan, construct cluster/2.2/constructCluster}
{\node[left] at (0,\y) {\l};
\draw[fill=\c] (0,\y-.4) rectangle (\x,\y+.4);}
\end{tikzpicture}
\end{figure}
\end{document}
更新
再次使用纯 tikz:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[x={(.01,0)}]
\foreach \l/\x/\c[count=\y] in {find optimal partition/400/findOptimalPartition,
store cluster component/150/storeClusterComponent,
database scan/250/dbscan,
construct cluster/50/constructCluster}
{\node[left] at (0,\y) {\l};
\fill[\c] (0,\y-.4) rectangle (\x,\y+.4);
\node[right] at (\x, \y) {\x};}
\draw (0,0) -- (600,0);
\foreach \x in {100, 200, ..., 600}
{\draw (\x,.2) -- (\x,0) node[below] {\x};}
\draw (0,0) -- (0,4.5);
\end{tikzpicture}
\end{figure}
\end{document}
如果您使用其他 pgfplots,这可能与这些图不一致。
使用 pgfplotsnodes near coords
可能是可行的方法。
答案2
首先设置compat
:
\pgfplotsset{compat=1.13}
xbar stacked
当前版本是 1.13。据我所知,如果您想与一起使用,您必须至少使用 1.11 版本nodes near coords
。
对于第一个图,最好使用所有坐标,或者改为ytick={0,...,5}
使用:ytick=data
y
\addplot[findOptimalPartition,fill] coordinates
{(400,0) (0,1) (0,2) (0,3) (0,4) (0,5)};
对于每个条形右侧的值,您可以使用nodes near coords
。但随后您必须更改其位置:
nodes near coords,
nodes near coords style={text=black, at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},
visualization depends on=y \as \pgfplotspointy,
代码:
\documentclass{article}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.13}% <- needed!!
\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
legend style={
legend columns=4,
at={(xticklabel cs:0.5)},
anchor=north,
draw=none
},
ytick=data,
axis y line*=none,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize},
xtick={0,100,...,600},
width=.9\textwidth,
bar width=6mm,
yticklabels={{Test A},
{Test B},
{Test C},
{Test D},
{Test E},
{Test F}},
xmin=0,
xmax=600,
area legend,
y=8mm,
enlarge y limits={abs=0.625},
nodes near coords,
nodes near coords style={text=black, at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},
visualization depends on=y \as \pgfplotspointy,
every axis plot/.append style={fill}
]
\addplot[findOptimalPartition] coordinates
{(400,0) (0,1) (0,2) (0,3) (0,4) (0,5)};
\addplot[storeClusterComponent] coordinates
{(0,0) (150,1) (0,2) (0,3) (0,4) (0,5)};
\addplot[dbscan,fill=dbscan] coordinates
{(0,0) (0,1) (250,2) (0,3) (0,4) (0,5)};
\addplot[constructCluster] coordinates
{(0,0) (0,1) (0,2) (50,3) (0,4) (0,5)};
\addplot[blue] coordinates
{(0,0) (0,1) (0,2) (0,3) (350,4) (0,5)};
\addplot[green] coordinates
{(0,0) (0,1) (0,2) (0,3) (0,4) (100,5)};
\end{axis}
\end{tikzpicture}
\caption{X}
\label{fig:stats}
\end{figure}
\end{document}
但@Jake 的建议https://tex.stackexchange.com/a/37660/43317更好:
使用
xbar=0pt,
/pgf/bar shift=0pt,
而是xbar stacked
。然后你只需要为每个图写下所需的坐标,每个条形的值的代码就简化为
nodes near coords,
nodes near coords style={text=black},
据我所知,以下代码也适用于旧版 pgfplots。
代码:
\documentclass{article}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.13}
\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar=0pt,
/pgf/bar shift=0pt,
legend style={
legend columns=4,
at={(xticklabel cs:0.5)},
anchor=north,
draw=none
},
ytick={0,...,5},
ytick style={draw=none},% <- added
axis y line*=none,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize},
xtick={0,100,...,600},
width=.9\textwidth,
bar width=6mm,
yticklabels={{Test A},
{Test B},
{Test C},
{Test D},
{Test E},
{Test F}},
xmin=0,
xmax=600,
area legend,
y=8mm,
enlarge y limits={abs=0.625},
nodes near coords,
nodes near coords style={text=black},
every axis plot/.append style={fill}
]
\addplot[findOptimalPartition] coordinates {(400,0)};
\addplot[storeClusterComponent] coordinates {(150,1)};
\addplot[dbscan,fill=dbscan] coordinates {(250,2)};
\addplot[constructCluster] coordinates {(50,3)};
\addplot[blue] coordinates {(350,4)};
\addplot[green] coordinates {(100,5)};
\end{axis}
\end{tikzpicture}
\caption{X}
\label{fig:stats}
\end{figure}
\end{document}
结果和上面一样。