我有以下文檔,其中是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}