pandas 熊猫直方图:将每列的直方图绘制为大图的子图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39646070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas histogram: plot histogram for each column as subplot of a big figure
提问by Edamame
I am using the following code, trying to plot the histogram of every column of a my pandas data frame df_in as subplot of a big figure.
我正在使用以下代码,尝试将我的 Pandas 数据框 df_in 的每一列的直方图绘制为大图的子图。
%matplotlib notebook
from itertools import combinations
import matplotlib.pyplot as plt
fig, axes = plt.subplots(len(df_in.columns) // 3, 3, figsize=(12, 48))
for x in df_in.columns:
df_in.hist(column = x, bins = 100)
fig.tight_layout()
However, the histogram didn't show in the subplot. Any one knows what I missed? Thanks!
但是,直方图没有显示在子图中。有谁知道我错过了什么?谢谢!
回答by Pascal Antoniou
I can't comment burhan's answer because I don't have enough reputation points. The problem with his answer is that axes
isn't one-dimensional, it contains axes triads, so it needs to be unrolled:
我无法评论 burhan 的回答,因为我没有足够的声望点。他的答案的问题在于axes
它不是一维的,它包含轴三元组,因此需要展开:
%matplotlib notebook
from itertools import combinations
import matplotlib.pyplot as plt
fig, axes = plt.subplots(len(df_in.columns)//3, 3, figsize=(12, 48))
i = 0
for triaxis in axes:
for axis in triaxis:
df_in.hist(column = df_in.columns[i], bins = 100, ax=axis)
i = i+1
回答by burhan
You need to specify which axis you are plotting to. This should work:
您需要指定要绘制到哪个轴。这应该有效:
fig, axes = plt.subplots(len(df_in.columns)//3, 3, figsize=(12, 48))
for col, axis in zip(df_in.columns, axes):
df_in.hist(column = col, bins = 100, ax=axis)