Visualize histograms in seaborn

我们两清 提交于 2019-12-06 16:41:05
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])

The plot will look like this:

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

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

I found the solution:

I had to change the sharex and sharey :

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

This way the don't share the same axes and it works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!