如何根据数据框中的所有值制作直方图?
How to make a histogram from all of the values in a dataframe?
假设一个数据框如下所示:
a b c d e f g
b 0 0 1 2 0 5
c 1 3 2 0 2 5
d 12 0 0 1 3 9
e 3 4 7 8 9 0
f 0 0 0 0 1 1
g 3 4 4 5 1 0
我想生成每个值(在每个单元格上)出现在数据帧上的频率的直方图。
请注意,在我的实际数据框中,我有一个 260 行 x 260 列的矩阵,其值范围从 0 到 30,包括浮点数。
任何帮助将不胜感激:)
这将根据所有值制作直方图:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame([
[0, 0, 1, 2, 0, 5],
[1, 3, 2, 0, 2, 5],
[12, 0, 0, 1, 3, 9],
[3, 4, 7, 8, 9, 0],
[0, 0, 0, 0, 1, 1],
[3, 4, 4, 5, 1, 0],
])
plt.hist(df.to_numpy().flatten())
plt.show()
或者您可以使用:
df.stack().hist(grid=False)
plt.show()
得到完全相同的结果。
假设一个数据框如下所示:
a b c d e f g
b 0 0 1 2 0 5
c 1 3 2 0 2 5
d 12 0 0 1 3 9
e 3 4 7 8 9 0
f 0 0 0 0 1 1
g 3 4 4 5 1 0
我想生成每个值(在每个单元格上)出现在数据帧上的频率的直方图。
请注意,在我的实际数据框中,我有一个 260 行 x 260 列的矩阵,其值范围从 0 到 30,包括浮点数。
任何帮助将不胜感激:)
这将根据所有值制作直方图:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame([
[0, 0, 1, 2, 0, 5],
[1, 3, 2, 0, 2, 5],
[12, 0, 0, 1, 3, 9],
[3, 4, 7, 8, 9, 0],
[0, 0, 0, 0, 1, 1],
[3, 4, 4, 5, 1, 0],
])
plt.hist(df.to_numpy().flatten())
plt.show()
或者您可以使用:
df.stack().hist(grid=False)
plt.show()
得到完全相同的结果。