在 seaborn 中可视化直方图

Visualize histograms in seaborn

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

  1. 我认为代码重复太多,有办法把它写得更短。 2.I 有 13 个字段来创建直方图,由于 13 是质数,我面临一个问题如何很好地显示所有这些但没有空白图。
  2. 我想显示分布但我只得到 1 个柱,即使我将 dit 更改为 0、100 和 500(我有超过 1000 个观察值)。

  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)

这是结果:

如您所见,我有一些空地块,箱子看起来像一个。

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])

情节将如下所示:

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

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])

然后绘图将如下所示(N.B。图表看起来与上面的版本略有不同,因为数据框值是生成的,多次使用 np.random.randint 函数,同时进行实验与解决方案):

我找到了解决方案:

我不得不更改 sharex 和 sharey :

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

这种方式不共享相同的轴并且有效