
我有以下 csv 檔案:
1,36
2,22
3,13
4,8
5,7
6,2
7,1
8,7
9,12
10,5
11,6
12,2
13,7
14,4
15,4
16,1
17,2
18,1
19,1
20,2
21,2
22,1
23,1
24,1
25,1
26,1
27,2
28,0
29,0
30,0
31,0
32,1
33,0
34,0
35,0
36,1
誰的名字是file
.
下面的程式碼
\begin{tikzpicture}
\begin{axis}[black,ylabel=Frequency, legend pos=outer north east]
\addplot table[col sep=comma] {file.csv};
\end{axis}
\end{tikzpicture}
很好,但我想繪製一個分段常數圖。例如,如果我們看一下file
,我們有
1,36
對應於 1 和 2 之間的一條常數線,常數縱座標為 36,
2,22
對應於 2 和 3 之間的一條常數線,常數縱座標為 22,依此類推。
答案1
(我將我的評論延伸到此處的完整答案。)
您可以使用該const plot
選項\addplot
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{filecontents*}{file.csv}
1,36
2,22
3,13
4,8
5,7
6,2
7,1
8,7
9,12
10,5
11,6
12,2
13,7
14,4
15,4
16,1
17,2
18,1
19,1
20,2
21,2
22,1
23,1
24,1
25,1
26,1
27,2
28,0
29,0
30,0
31,0
32,1
33,0
34,0
35,0
36,1
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ylabel=Frequency]
\addplot+[const plot] table[col sep=comma] {file.csv};
\end{axis}
\end{tikzpicture}
\end{document}