簡體   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