PGFPLOTS:每隔一个刻度显示 x 个标签

PGFPLOTS:每隔一个刻度显示 x 个标签

如何显示此数据集中每个第二数据的 xaxis 标签。我想要每个刻度都有一个条形图,而不是标签。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}% http://ctan.org/pkg/filecontents
\usepackage{silence}% http://ctan.org/pkg/silence
\WarningFilter{latex}{Overwriting file}% Remove LaTeX warnings starting with "Overwriting file"
\begin{filecontents*}{data.csv}
_MINPT_,_OBSPCT_
0,99.198665453
10,11.622687291
20,7.279344859
30,4.6982104944
40,3.4728541098
50,1.5862905672
60,2.0837124659
70,1.0433727631
80,1.313315135
90,0.8462238399
100,0.7703973309
110,0.5125872005
120,0.8826205641
130,0.4458598726
140,0.4185623294
150,0.4519259933
160,0.5065210798
170,0.2183803458
180,0.3973309069
190,0.2274795268
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar interval,
    enlarge x limits=false,
    xmin=0.1,ymax=100,ymin=0.1,
    xtick=data
]
\addplot+ table [x, y, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

我不想手动指定 xticks,因为我要生成一系列直方图!最后一个观测值 (190) 也缺失了。我如何确保它包含在内?

答案1

我试了一下。我设置了次要 xtick 来绘制所有缺失的刻度,并设置了几个值,特别是 xmin、x 上限 +5、ymin 和 ymax。主要和次要 xtick 的上限设置为一个巨大的数字,以便能够绘制所有必要的刻度。我更喜欢基本 ybar 图而不是 ybar 间隔,它在设置参数时为我节省了一些时间。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}
\usepackage{silence}\pagestyle{empty}
\WarningFilter{latex}{Overwriting file}
\begin{filecontents*}{data.csv}
_MINPT_,_OBSPCT_
0,99.198665453
10,11.622687291
20,7.279344859
30,4.6982104944
40,3.4728541098
50,1.5862905672
60,2.0837124659
70,1.0433727631
80,1.313315135
90,0.8462238399
100,0.7703973309
110,0.5125872005
120,0.8826205641
130,0.4458598726
140,0.4185623294
150,0.4519259933
160,0.5065210798
170,0.2183803458
180,0.3973309069
190,0.2274795268
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  enlarge x limits={abs value=5, upper}, 
  enlarge y limits=false, 
  xmin=-5, ymax=100, ymin=0,
  xtick pos=left, ytick pos=left,
  grid=both, ymajorgrids=false,
  minor xtick={10, 30, ..., 1000},
  xtick={0, 20, ..., 1000},
  x tick label style={black},
  width=10cm, height=7cm,
 ] \addplot+ table [x, y, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容