如何使用 python matplotlib 绘制离散数据的直方图?
How can I plot Histogram for discrete data using python matplotlib?
这是数据,如何使用 matplotlib 在此离散数据上绘制直方图。
X F
3 3
4 3
5 5
6 3
7 4
8 4
9 3
10 4
11 2
12 2
13 3
14 1
15 2
16 5
17 2
18 1
19 2
20 1
您的数据已经是直方图(不需要在这里对任何观察结果进行分类,对吗?),所以我想您只需要绘制它。
# First read in the data into a dataframe in this case.
data = """
3 3
4 3
5 5
6 3
7 4
8 4
9 3
10 4
11 2
12 2
13 3
14 1
15 2
16 5
17 2
18 1
19 2
20 1""".split();
data = list(map(int, data))
data = pd.DataFrame({'X': data[::2], 'F': data[1::2]})
# Use matplotlib to draw a bar chart. Draw it with histogram style.
plt.bar(data.X, data.F, align='edge', width=1, edgecolor='k');
这是数据,如何使用 matplotlib 在此离散数据上绘制直方图。
X F
3 3
4 3
5 5
6 3
7 4
8 4
9 3
10 4
11 2
12 2
13 3
14 1
15 2
16 5
17 2
18 1
19 2
20 1
您的数据已经是直方图(不需要在这里对任何观察结果进行分类,对吗?),所以我想您只需要绘制它。
# First read in the data into a dataframe in this case.
data = """
3 3
4 3
5 5
6 3
7 4
8 4
9 3
10 4
11 2
12 2
13 3
14 1
15 2
16 5
17 2
18 1
19 2
20 1""".split();
data = list(map(int, data))
data = pd.DataFrame({'X': data[::2], 'F': data[1::2]})
# Use matplotlib to draw a bar chart. Draw it with histogram style.
plt.bar(data.X, data.F, align='edge', width=1, edgecolor='k');