2017-12-21 20 views
1

データセットには、クラスタイプ、近傍、可視性という3つのデータセットがあります。 積み重ね用の列値を含むパンダの積み上げ棒グラフ

sample table

は私が ある棒グラフを作成しようとしています両方積み重ね、積み重ね - 可視性によって積み重ねられ、近所で並びました。だから、基本的に、私はこのチャートの積み重ねネスの組み合わせを探しています:

sample chart 1

nbvis_gb = nbvis.sort_values(by=['visibility'],ascending=False).groupby(by='visibility',sort=False) 
fig, ax = plt.subplots(nrows=1,ncols=2,figsize=(14,8),sharey=True) 

for (i, j), ax,color in zip(nbvis_gb,ax.flatten(),colors_hood): 
    print(j['class'].values) 
    title = str(i) 
    j.plot.bar(ax=ax,colors=colors_hood) 
    ax.set_title(title, fontsize=20) 
    #ax.set_ylim(0,1.05) 
    ax.tick_params(labelsize=16) 
    ax.set_xticklabels(j['class'].values) 
    ax.legend_.remove() 


ax.legend(loc=8,fontsize=20,ncol=4,bbox_to_anchor=(0,-.45)) 
fig.tight_layout(h_pad=2) 
fig.suptitle('Visibility of containers by class and neighborhood',y=1.03,fontsize=24) 

とこのチャートの積み重ねネス:

enter image description here

nbvis.unstack()['Neighborhood 1'].plot.bar(stacked=True) 

ご協力いただければ幸いです!

乾杯、 エリザベス

答えて

0

ここにあなたがこれを行うことが一つの方法です。私はいくつかのダミーデータを使用:

df = pd.DataFrame({"class":['bucket', 'pot', 'tarp', 'trash', 'toy', 'tubing', 'other','bucket', 'pot', 'tarp', 'trash', 'toy', 'tubing', 'other',], 
        "visability":["visable", "visable","visable","visable","visable","visable","visable", "not visable","not visable","not visable","not visable","not visable","not visable","not visable",], 
        "n1":np.random.random(size=14), 
        "n2":np.random.random(size=14), 
        "n3":np.random.random(size=14), 
        "n4":np.random.random(size=14)}) 

私はトリックがbottomを使用することであると思う:

N=7 
width = 0.095 
w = 0 
ind = np.arange(N) + .15 
classes = ['bucket', 'pot', 'tarp', 'trash', 'toy', 'tubing', 'other'] 
neighborhoods = ['n1', 'n2', 'n3', 'n4'] 
fig, ax = plt.subplots() 

top_colors = ['#ff9999', '#9999ff', '#e6b3ff', '#66ff66'] 
bottom_colors = ['#b30000', '#000066', '#7700b3', '#004d00'] 

for i, n in enumerate(neighborhoods): 
    vis = df[(df.visability == "visable")][n] 
    non_vis = df[df.visability == "not visable"][n] 

    rect1 = ax.bar(ind+w, vis, float(width), color=top_colors[i]) 
    rect2 = ax.bar(ind+w, non_vis, width, color=bottom_colors[i], bottom=vis) 
    w += 0.15 

extra_space = 0.05 
ax.set_xticks(ind+width+xtra_space) 
ax.set_xticklabels(('bucket', 'pot', 'tarp', 'trash', 'toy', 'tubing', 'other',)) 

ax.set_title('Visability of container types by class') 

plt.show() 

enter image description here

+0

ナイス!私は必ずしも幅/ティックを設定するのが大好きではありませんが、これは私が探しているものです。 –

+0

@ E.Caseありがとう!私はそれがあなたにとって有益だとうれしいです。はい、おそらくダニと幅を設定するより良い方法があります。ラベルや凡例の追加にはいくつかの方法があるようですので、スタッキング/グループ化の部分に取り掛かりました。 – briancaffey

0

に合わせたマルチインデックスdatafameを作成するためにあなたのデータフレームのmeltpivot_tableを考えてみましょうグラフの寸法。下の出力を画面に表示し、seabornの配色を使用して同じフォルダ内のFigureをpngに保存します。もちろん、必要に応じてグラフの設定を調整してください。

データ

import numpy as np 
import pandas as pd 
from itertools import product 

from matplotlib import pyplot as plt 
import seaborn 

np.random.seed(444) 
df = pd.DataFrame(list(product(['bucket (1)', 'flower pot (2)', 'tarp (3)', 'trash (6)', 'toy (7)', 
           'piping/tubing (9)', 'other (10)'], 
           ['visible containers', 'partial or not visible containers'])), 
        columns=['class', 'visibility']).assign(Neighborhood1 = abs(np.random.randn(14)), 
                  Neighborhood2 = abs(np.random.randn(14)), 
                  Neighborhood3 = abs(np.random.randn(14)), 
                  Neighborhood4 = abs(np.random.randn(14))) 

グラフ

seaborn.set() 

def runplot(pvtdf):   
    fig, axes = plt.subplots(nrows=1, ncols=len(mdf['Neighborhood'].unique())) 

    for i, n in enumerate(mdf['Neighborhood'].unique()): 
     pvtdf.xs(n).plot(ax=axes[i], kind='bar', stacked=True, edgecolor='w', 
       figsize=(20,8), width=0.5, fontsize = 12, 
       title='{} - Visibility of containers \n by class and neighborhood'.format(n)) 
     axes[i].title.set_size(16) 

    plt.tight_layout() 
    fig.savefig('Output.png') 
    plt.show() 
    plt.clf() 

# MELT LONG 
mdf = pd.melt(df, id_vars = ['class', 'visibility'], var_name='Neighborhood') 

# PIVOT WIDE 
pvtdf = mdf.pivot_table(index= ['Neighborhood', 'class'], columns='visibility', values='value') 

runplot(pvtdf, n) 

plt.close() 

出力

Neighborhood Graphs

+0

ちょっとパフェ、これはかなり近いです&私はこれを自動的に行いたいと思っています(例えば、バーの位置を設定する必要はありません)。しかし、私はブライアンカフェーのような同じグラフでそれらをすべて取得しようとしています –

関連する問題