如何使用matplotlib的hist()函数绘制“装箱数据”的图形

x9ybnkn6  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(71)

我是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()


未阅读plt.hist()函数中的bin
df_binned['Level']
df_binned['Quantity']
我如何绘制分箱数据?如何准备制作直方图的数据?

mnemlml8

mnemlml81#

需要数据准备

df_binned = pd.DataFrame(data)
df_binned['Level']   = df_binned['Level'].dtype(str)
df_binned['Quantity']=df_binned['Quantity'].dtype(str)

字符串

相关问题