可视化Seaborn中的直方图 [英] Visualize histograms in seaborn

查看:209
本文介绍了可视化Seaborn中的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code that create 16 histograms. My problems are:


  1. 我认为代码过于重复,并且有一些方法可以使代码更短。
    2.我有13个字段可以创建直方图,而13是素数,我面临一个问题,即如何很好地显示所有这些而没有空白图。

  2. 我想显示分布,但是即使将dit更改为0、100和500(我有1000多个观测值),我也只能得到1 bar。

  1. I think the code is too much repetative and that there are ways to write it shorter. 2.I have 13 fields to create histograms, and as 13 is prime number, I face a problem how to show all of them nicely but without blank plots.
  2. I want to show the distribution but I get only 1 bar, even though I have change dit to 0, 100 and 500 (I have more than 1000 observations).

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

some columns are float and have too many 0 after the dot and I can't change it.

这是我的代码:

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)

,结果如下:

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

as you can see I have some empty plots and the bins look like one.

推荐答案

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

图将如下所示:

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

To get rid of the empty plot, sub plots must be added in a slightly different way, like so:

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 函数几个生成了数据框值次,同时尝试解决方案):

Then the plot will look like this (N.B. the graphs will look slightly different to the version above, since the data frame values were generated, using np.random.randint function several times, whilst experimenting with the solution):

这篇关于可视化Seaborn中的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆