使用循环的多个Boxplot

时间:2019-07-02 17:35:18

标签: python-3.x pandas for-loop boxplot

我有一个大数据集,并且已将数据集分为0.1个小子集(从0到100)。我想为此绘制一个箱形图(与线形图相同,但带有箱形图)从0到100。

为了生成/减少数据(最初是十亿),我运行了一个循环,如下所示:

a = np.array(Treecover.where(low)).flatten()   # Treecover less than 25 only/low
b = np.array(RZSC.where(low)).flatten()        
Low_TC_data = pd.DataFrame({'Treecover': a, 'RZSC': b})
Low_TC_data_NaN = Low_TC_data.dropna()

Tree = []
Mean = []
Max = []
Min = []
Median = []
for i in np.arange(0,1,0.1):
    Tree.append(i)
    a = 0
    a = Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i)) & (Low_TC_data_NaN['Treecover']<(i+0.1))).dropna()
    Mean.append(a.mean())
    Min.append(a.min())
    Max.append(a.max())  
    Median.append(a.median())

然后我绘制了一个散点图以对数据进行可视化。

fig = plt.figure(figsize=(10, 7))

plt.scatter(Tree, Mean, s = 500)
plt.scatter(Tree, Median,color = 'red', s = 500)
plt.fill_between(Tree, Min, Max, alpha = 0.3)

enter image description here

我尝试通过如下方式绘制前11个箱形图:

fig = plt.figure(figsize=(20, 10))
i = 0
green_diamond = dict(markerfacecolor='w', marker='D')
plt.boxplot((Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i)) & (Low_TC_data_NaN['Treecover']<(i+0.1))).dropna(),
            Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.1)) & (Low_TC_data_NaN['Treecover']<(i+0.2))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.2)) & (Low_TC_data_NaN['Treecover']<(i+0.3))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.3)) & (Low_TC_data_NaN['Treecover']<(i+0.4))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.4)) & (Low_TC_data_NaN['Treecover']<(i+0.5))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.5)) & (Low_TC_data_NaN['Treecover']<(i+0.6))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.6)) & (Low_TC_data_NaN['Treecover']<(i+0.7))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.7)) & (Low_TC_data_NaN['Treecover']<(i+0.8))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.8)) & (Low_TC_data_NaN['Treecover']<(i+0.9))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+0.9)) & (Low_TC_data_NaN['Treecover']<(i+1))).dropna(),
           Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+1)) & (Low_TC_data_NaN['Treecover']<(i+1.1))).dropna()),
           flierprops=green_diamond);
# and this code can go on to 
# ....Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i+24.9)) & (Low_TC_data_NaN['Treecover']<(i+25))).dropna()
# For first 250 boxplots

enter image description here

上方X轴为0、0.1、0.2、0.3 ....... 1。

如果可能的话,我希望有一个100个这样的高度(或什至1000个高度)的箱形图。

我尝试使用以下代码执行此操作失败:

fig = plt.figure(figsize=(5, 10))

data = [(Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i)) & (Low_TC_data_NaN['Treecover']<(i+0.1))).dropna()) for i in np.arange(0,1,0.1)]
[plt.boxplot(np.array(data[i]).ravel()) for i in range(5)];

enter image description here

其中的值似乎也有些奇怪。并且不遵循第二个数字。

我想要所有数据步骤的箱线图。基本上,我希望第二个数字代码要短(简洁),以得到与图2相同的结果,并且可以在x轴上大约有100个值时使用。

1 个答案:

答案 0 :(得分:-1)

我只是进行了一些调整,而不是循环框线图,而是循环了数据本身。

代码看起来像这样:

data = [[Low_TC_data_NaN['RZSC'].where((Low_TC_data_NaN['Treecover']>=(i)) & 
                                      (Low_TC_data_NaN['Treecover']<(i+0.1))).dropna()] for i in np.arange(0,25,0.1)]
fig = plt.figure(figsize=(20, 10))
green_diamond = dict(markerfacecolor='w', marker='D')
plt.boxplot(np.array(data).squeeze(), flierprops=green_diamond);
plt.ylim(0,4000)

结果是这样的: enter image description here

X轴的值似乎在0-25之间(共250个箱形图)。