我正在尝试绘制水平直方图,但很难隐藏坐标附近的最后两个节点。我希望 y 轴在 >1000.0ms 处也显示一个刻度,但我不想显示它的值。
如何隐藏 y 轴上的两个顶部零?任何技巧都非常受欢迎,包括更改数据。
我迄今为止的代码:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{filecontents*}{data}
index;ytl;ms
1;$<1.0$\,ms;4.2
2;$1.1-100.9$\,ms;83.8
3;$101.0-200.8$\,ms;6.9
4;$200.9-300.7$\,ms;1.7
5;$300.8-400.6$\,ms;0.8
6;$400.7-500.5$\,ms;0.5
7;$500.6-600.4$\,ms;0.4
8;$600.5-700.3$\,ms;0.3
9;$700.4-800.3$\,ms;0.2
10;$800.3-900.1$\,ms;0.2
11;$900.2-1000.0$\,ms;1.1
12;$>1000.0$\,ms;0
13;;0
}
\end{filecontents*}
\pgfplotstableread[col sep=semicolon]{data}{\mytable}
\begin{tikzpicture}
\begin{axis}[
every node near coord/.append style={yshift=7pt},
grid=none,
axis lines=left,
xlabel=percentage,
xbar interval,
x=0.1cm,
xlabel style={yshift=-0.5cm},
x tick label style={%
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,
/tikz/.cd
},
xticklabel={$\pgfmathprintnumber{\tick}$\%},
xmax=100,
nodes near coords={\pgfkeys{/pgf/fpu}\pgfmathparse{\pgfplotspointmeta}\pgfmathprintnumber{\pgfmathresult}\,\%},
max space between ticks=100,
ymax=13,
ymin=1,
yticklabels from table={\mytable}{ytl},
title=distribution,
bar width=14pt,
]
\addplot table [x=ms, y=index] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}
我的结果:
答案1
您可以使用条件格式。由于没有text=none
(如draw=none
或fill=none
),所以我使用opacity=0
。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{filecontents*}{data}
index;ytl;ms
1;$<1.0$\,ms;4.2
2;$1.1-100.9$\,ms;83.8
3;$101.0-200.8$\,ms;6.9
4;$200.9-300.7$\,ms;1.7
5;$300.8-400.6$\,ms;0.8
6;$400.7-500.5$\,ms;0.5
7;$500.6-600.4$\,ms;0.4
8;$600.5-700.3$\,ms;0.3
9;$700.4-800.3$\,ms;0.2
10;$800.3-900.1$\,ms;0.2
11;$900.2-1000.0$\,ms;1.1
12;$>1000.0$\,ms;0
13;;0
}
\end{filecontents*}
\pgfplotstableread[col sep=semicolon]{data}{\mytable}
\begin{tikzpicture}
\begin{axis}[
every node near coord/.append style={yshift=7pt},
grid=none,
axis lines=left,
xlabel=percentage,
xbar interval,
x=0.1cm,
xlabel style={yshift=-0.5cm},
x tick label style={%
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,
/tikz/.cd
},
xticklabel={$\pgfmathprintnumber{\tick}$\%},
xmax=100,
nodes near coords={\pgfkeys{/pgf/fpu}\pgfmathparse{\pgfplotspointmeta}\pgfmathprintnumber{\pgfmathresult}\,\%},
max space between ticks=100,
ymax=13,
ymin=1,
yticklabels from table={\mytable}{ytl},
title=distribution,
bar width=14pt,
]
\addplot+[coordinate style/.condition={x==0}{opacity=0}] table [x=ms, y=index] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}