python - 在seaborn中可视化直方图

标签 python seaborn distribution

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

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

  3. 有些列是 float 的,点后有太多 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

如您所见,我有一些空地 block ,而且垃圾箱看起来就像一个。

最佳答案

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

关于python - 在seaborn中可视化直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029227/

相关文章:

python - 如何在Python中设置 basemap 的偏移量?

python - 使用 python 'ipaddress' 以及 _methods 与直接值的目的

python - 带子图的猫图有限制吗?

c++ - 在 C++ 中设置离散分布

python - 直方图未显示在 f 分布图中

python - 在pygame中点击按钮

python - 无衬线直立 mu

python - Seaborn 热图在更新后抛出 "isnan"类型的错误

python - 如何在seaborn jointgrid/jointplot中注释边际图/分布图

scipy - 评估分布拟合的优度