可视化Seaborn中的直方图

时间:2019-11-25 09:59:16

标签: python seaborn distribution

我有一个创建16个直方图的代码。我的问题是:

  1. 我认为代码太多重复了,有很多方法可以缩短代码长度。 2.我有13个字段可创建直方图,由于13是质数,我面临一个问题,即如何很好地显示所有这些而没有空白图。
  2. 我想显示分布,但是即使将dit更改为0、100和500(我有1000多个观测值),我也只能得到1 bar。

  3. 一些列是浮点的,并且点后有太多0,我无法更改。

这是我的代码:

f, axes = plt.subplots(4, 4, figsize=(20,20), sharex=True)
sns.distplot(data['HR90'], color="skyblue", ax=axes[0,0],bins=500)
sns.distplot(data['HC90'], color="olive", ax=axes[0,1],bins=100)
sns.distplot(data['RD90'], color="gold", ax=axes[0,2],bins=100)
sns.distplot(data['PO90'], color="teal", ax=axes[0,3], bins=100)

sns.distplot(data['PS90'], color="red", ax=axes[1,0], bins=100)
sns.distplot(data['UE90'], color="green", ax=axes[1,1], bins=100)
sns.distplot(data['DV90'], color="blue", ax=axes[1,2], bins=100)
sns.distplot(data['MA90'], color="purple", ax=axes[1,3], bins=100)

sns.distplot(data['POL90'], color="orange", ax=axes[2,0], bins=100)
sns.distplot(data['DNL90'], color="green", ax=axes[2,1], bins=100)
sns.distplot(data['BLK90'], color="pink", ax=axes[2,2], bins=100)
sns.distplot(data['GI89'], color="silver", ax=axes[2,3], bins=100)

sns.distplot(data['FH90'], color="cyan", ax=axes[3,1], bins=100)

这是结果:

enter image description here

如您所见,我有一些空图,垃圾箱看起来像一个。

2 个答案:

答案 0 :(得分:1)

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

position = []
for x in range(0, 4):
    for y in range (0, 4):
        position.append([x, y])

groups = ['PO90', 'HC90', 'RD90', 'HR90', 'PS90', 'UE90', 'DV90', 'MA90', 'POL90', 'DNL90', 'BLK90', 'GI89','FH90']
graph_colors = ["skyblue", "olive", "gold", "teal", "red", "green", "blue", "purple", "orange", "green", "pink", "silver", "cyan"]
graph_bins = [500, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]

data = pd.DataFrame(np.random.randint(low=0, high=10, size=(100, 13)), columns=groups)

f, axes = plt.subplots(4, 4, figsize=(20,20), sharex=False, sharey=False)

for i in range(0, 13):
    sns.distplot(data[groups[i]], color=graph_colors[i], ax=axes[position[i][0], position[i][1]], bins=graph_bins[i])

情节看起来像这样:

enter image description here

要摆脱空白图,必须以稍微不同的方式添加子图,如下所示:

fig = plt.figure(figsize=(20,20))

# Generating 1st column.
for sp_index in range(1, 14, 4):
    ax = fig.add_subplot(4, 4, sp_index)
    sns.distplot(data[groups[sp_index-1]], color=graph_colors[sp_index-1], ax=ax, bins=graph_bins[sp_index-1])

# Generating 2nd column. 
for sp_index in range(2, 14, 4):
    ax = fig.add_subplot(4, 4, sp_index)
    sns.distplot(data[groups[sp_index-1]], color=graph_colors[sp_index-1], ax=ax, bins=graph_bins[sp_index-1])

# Generating 3rd column.
for sp_index in range(3, 14, 4):
    ax = fig.add_subplot(4, 4, sp_index)
    sns.distplot(data[groups[sp_index-1]], color=graph_colors[sp_index-1], ax=ax, bins=graph_bins[sp_index-1])

# Generating 4thcolumn.
for sp_index in range(4, 14, 4):
    ax = fig.add_subplot(4, 4, sp_index)
    sns.distplot(data[groups[sp_index-1]], color=graph_colors[sp_index-1], ax=ax, bins=graph_bins[sp_index-1])

然后,绘图将如下所示(注意,图形看起来与上面的版本略有不同,因为使用np.random.randint函数多次生成了数据帧值,并尝试了解决方案):

enter image description here

答案 1 :(得分:0)

我找到了解决方法:

我必须更改sharex和sharey:

f, axes = plt.subplots(4, 4, figsize=(60,60), sharex=False, sharey=False)

这样,它们不会共享相同的轴并且可以正常工作