【发布时间】:2021-11-07 09:06:20
【问题描述】:
我在一个文件夹中有一组 7 个 *xls 文件(直到现在),这些是每个月的统计数据,我用 Pandas 阅读了所有这些文件,然后我选择了名为“Gap”的列并进行了一些过滤以保留在职的。我试图在一个子图中绘制每个“间隙”的直方图(子图中每月的间隙)。 我做的代码是:
pato = r'D:\Inves\Pdoc\Cata_2021'
#os.chdir(pato)
file_list = glob.glob(pato + r"\*.xls")
print('file_list {}'.format(file_list))
print(file_list)
fig, axs = plt.subplots(4,3, figsize=(15, 6), sharex=True, sharey=True)
a = 4
b = 3
# this for loop read all files with Pandas and
for proce in file_list:
df_cat = pd.read_excel(proce)
#print(df_cat.head())
df_cat_sha = df_cat[(df_cat.Prof.between(0, 75))]
print(df_cat_sha)
m = 0
# Here I tried to create the subplot's and populate them
for i in range(a):
for j in range(b):
df_cat_sha.Gap.hist(bins=18, ax=axs[i, j],
color='green', alpha=0.75)
m +=1
plt.show()
我得到了以下情节(在Plotting two histograms from a pandas DataFrame in one subplot using matplotlib 的帮助下)
但是,您可以看到在每个子图中都有多个直方图,这是我不想要的。
您有什么提示可以在一个子图中仅从我的 DataFrame 中绘制一个直方图“Gap”吗?这里我也附上 xls 格式的文件 (https://drive.google.com/drive/folders/1MbuTMQpuc79nRYFGL-UAdmvo9r2AzhxA?usp=sharing)
提前致谢
托尼诺
【问题讨论】:
标签: python pandas dataframe subplot