分别绘制所有 pandas 数据框列 [英] Plot all pandas dataframe columns separately

查看:103
本文介绍了分别绘制所有 pandas 数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只带有数字列的pandas数据框,并且我正在尝试为所有功能创建一个单独的直方图

I have a pandas dataframe who just has numeric columns, and I am trying to create a separate histogram for all the features

ind group people value value_50
 1      1    5    100    1
 1      2    2    90     1
 2      1    10   80     1
 2      2    20   40     0
 3      1    7    10     0
 3      2    23   30     0

但是在我的实际数据中,有50多个列,如何为所有列创建单独的图

but in my real life data there are 50+ columns, how can I create a separate plot for all of them

我尝试过

df.plot.hist( subplots = True, grid = True)

这给了我一个重叠的不清楚的情节.

It gave me an overlapping unclear plot.

如何使用pandas subplots = True来排列它们.下面的示例可以帮助我在(2,2)网格中获取四列的图形.但对于所有50列来说,这都是一个漫长的方法

how can I arrange them using pandas subplots = True. Below example can help me to get graphs in (2,2) grid for four columns. But its a long method for all 50 columns

fig, [(ax1,ax2),(ax3,ax4)]  = plt.subplots(2,2, figsize = (20,10))

推荐答案

熊猫subplots=True将在单个列中排列轴.

Pandas subplots=True will arange the axes in a single column.

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

df = pd.DataFrame(np.random.rand(7,20))

df.plot(subplots=True)

plt.tight_layout()
plt.show()

此处,未应用tight_layout,因为该图形太小而无法很好地排列轴.但是,可以使用更大的数字(figsize=(...)).

Here, tight_layout isn't applied, because the figure is too small to arange the axes nicely. One can use a bigger figure (figsize=(...)) though.

为了使轴在网格上,可以使用layout参数,例如

In order to have the axes on a grid, one can use the layout parameter, e.g.

df.plot(subplots=True, layout=(4,5))

如果通过plt.subplots()

fig, axes = plt.subplots(nrows=4, ncols=5)
df.plot(subplots=True, ax=axes)

这篇关于分别绘制所有 pandas 数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆