如何从 CSV 文件加载额外的刻度

如何从 CSV 文件加载额外的刻度

我有以下文档,其中手动extra x ticks定义了。但是,我想从第二列为 1 的 CSV 文件生成刻度,因为实际数据集比此示例大得多。我尝试了一些方法,from table但没有成功。这可能吗?或者,在数据中的正确位置放置一个标记也是一个可接受的解决方案。

\documentclass[]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{filecontents}{data.csv}
value,isChange
-1.18349,1
1.29675,0
3.33266,0
3.22086,0
2.29151,0
2.46577,0
2.41649,0
2.11641,0
2.46207,0
2.41293,0
2.10606,1
4.77804,0
6.199,0
4.7366,0
3.56816,0
4.38747,0
4.15053,0
4.04366,0
4.27493,0
4.2202,0
4.67526,1
\end{filecontents}
\begin{document}
\begin{figure}[H]
    \begin{tikzpicture}
    \begin{axis}[
        width=1\textwidth,height=0.4\textheight,
        extra x ticks = {0, 10, 20},
        extra tick style={grid=major, major grid style={red,thick}},
    ]
    \addplot[] table[x expr=\coordindex, y index=0,col sep=comma] {data.csv};
    \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

结果如下图所示。 在此处输入图片描述

答案1

|以下是使用图书馆标记的建议plotmarks

\documentclass[]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{plotmarks}
\begin{filecontents}{data.csv}
value,isChange
-1.18349,1
1.29675,0
3.33266,0
3.22086,0
2.29151,0
2.46577,0
2.41649,0
2.11641,0
2.46207,0
2.41293,0
2.10606,1
4.77804,0
6.199,0
4.7366,0
3.56816,0
4.38747,0
4.15053,0
4.04366,0
4.27493,0
4.2202,0
4.67526,1
\end{filecontents}
\begin{document}
\begin{figure}[htb]
  \begin{tikzpicture}
    \begin{axis}[
        width=1\textwidth,height=0.4\textheight,
        set layers,mark layer=like plot,% markers clipped
        clip mode=individual
      ]
      \addplot[
          only marks,mark=|,
          mark options={red,thick,mark size=\pgfkeysvalueof{/pgfplots/height}}
        ]table[
          x expr=\coordindex,y index=0,col sep=comma,
          restrict expr to domain={\thisrow{isChange}}{1:1}
        ]{data.csv};
      \addplot[] table[x expr=\coordindex,y index=0,col sep=comma] {data.csv};
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容