提问人:user19888430 提问时间:7/20/2023 最后编辑:user19888430 更新时间:7/21/2023 访问量:45
如何使用 matplotlib 的 hist() 函数绘制“binned data”的图形
How to plot the graph using hist() function of matplotlib for "binned data"
问:
我是matplotlib的新手。我有数据分箱数据。
Level Quantity
0 (199.533, 271.74] (10.213, 39.4]
1 (199.533, 271.74] (10.213, 39.4]
2 (54.903, 127.327] (10.213, 39.4]
3 (127.327, 199.533] (10.213, 39.4]
4 (127.327, 199.533] (10.213, 39.4]
... ... ...
5105 (54.903, 127.327] (10.213, 39.4]
5106 (54.903, 127.327] (39.4, 68.5]
5107 (54.903, 127.327] (10.213, 39.4]
5108 (127.327, 199.533] (10.213, 39.4]
5109 (54.903, 127.327] (10.213, 39.4]
我想为每个“等宽”的箱制作 2 个名为“水平”和“数量”的直方图。 我试过了
import pandas as pd
import matplotlib.pyplot as plt
# Sample binned data for the two variables (Level and Quantity)
data = {
'Level': ['(199.533, 271.74]', '(199.533, 271.74]', '(54.903, 127.327]', '(127.327, 199.533]', '(127.327, 199.533]'],......
'Quantity': ['(10.213, 39.4]', '(10.213, 39.4]', '(10.213, 39.4]', '(10.213, 39.4]', '(10.213, 39.4]'],......
}
df_binned = pd.DataFrame(data)
plt.hist([df_binned['Level'], df_binned['Quantity']], bins=10, edgecolor='black', alpha=0.7, label=df_binned.columns)
plt.legend()
plt.title('Stacked Histograms of Binned Variables')
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.show()
它没有读取函数中的 binplt.hist()
df_binned['级别']
df_binned['数量']
我如何绘制分箱数据? 如何准备制作直方图的数据?
答:
评论
bar
hist