2017-05-15 148 views
1

我发现DataFrame.plot.hist非常方便,但在这种情况下我找不到解决方案。如何在熊猫的DataFrame.plot.hist中使用不同的坐标轴?

我想绘制数据集中许多列的分布。问题在于大熊猫在所有x轴上都保持相同的比例,使得大部分图表无用。下面是我使用的代码:

X.plot.hist(subplots=True, layout=(13, 6), figsize=(20, 45), bins=50, sharey=False, sharex=False) 
plt.show() 

而这里的结果的一部分: plots

看来,问题是,熊猫上使用的所有列在同一箱,而不管他们的值。熊猫有没有一个方便的解决方案,还是我被迫用手做?我以数据为中心(零均值和单位方差),结果有所改善,但仍不能接受。

回答

2

有一对夫妇的选择,这里是代码和输出:

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

# Dummy data - value ranges differ a lot between columns 
X = pd.DataFrame() 
for i in range(18): 
    X['COL0{0}'.format(i+38)]=(2**i)*np.random.random(1000) 

# Method 1 - just using the hist function to generate each plot 
X.hist(layout=(3, 6), figsize=(20, 10), sharey=False, sharex=False, bins=50) 
plt.title('Method 1') 
plt.show() 

# Method 2 - generate each plot separately 
cols = plt.cm.spectral(np.arange(1,255,13)) 
fig, axes = plt.subplots(3,6,figsize=(20,10)) 
for index, column in enumerate(X.columns): 
    ax = axes.flatten()[index] 
    ax.hist(X[column],bins=50, label=column, fc=cols[index]) 
    ax.legend(loc='upper right') 
    ax.set_ylim((0,1.2*ax.get_ylim()[1])) 
fig.suptitle('Method 2') 
fig.show() 

第一个情节: enter image description here

第二个情节: enter image description here

我肯定会推荐第二种方法,因为你有更多的控制更多的控制个别情节,例如你可以改变轴scal es,标签,网格参数,以及其他任何东西。

我找不到任何可以让您修改原始plot.hist分档以接受单独计算的分档。

我希望这有助于!

+0

非常感谢!我采用了第二种方法,但由于列数不符合网格,我必须删除一些子图。我用'fig.delaxes'来删除未使用的子图。我不知道是否还有其他方法,因为使用第一种方法显示的轴没有多余,但没有提及Pandas代码库中的“delaxes”。 – rubik

+0

您也可以使用** fig.add_subplot **向图中添加轴,请参阅https://pythonprogramming.net/subplot2grid-add_subplot-matplotlib-tutorial/。很高兴帮助! – Robbie