繁体   English   中英

如何 plot 这些直方图彼此相邻

[英]How to plot these histograms next to each other

我想看看数据分布。 问题是我想制作单独显示每个 dataframe 列的循环。 使用这种方法,每一列的直方图都绘制在一个图形区域上。

for x in df.columns.to_list():
  df[x].hist(bins=120)

histogram_image

如何将这些直方图划分为单独的图像?

具有 3 列的样本数据:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

d = {'col1': [1, 2, 3, 4, 5, 6, 3, 5], 
     'col2': [1, 2, 2, 2, 5, 6, 3, 5], 
     'col3': [1, 2, 2, 2, 2, 2, 2, 6], }
df = pd.DataFrame(data=d)

df

在此处输入图像描述

您可以使用 matplotlib 中的子图来查看单独直方图上的每一列:

fig, axs = plt.subplots(len(df.columns), figsize=(8,12))
i=0

for x in df.columns.to_list():
    axs[i].hist(df[x], bins=10)
    i=i+1

在此处输入图像描述

为什么不只是df.hist(bins=120) 无需 go 通过循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM