2016-12-21 9 views
2
month cate_cnt1_members cate_cnt2_members cate_cnt3_members cate_cnt4_members cate_cnt5_members cate_cnt6_members cate_cnt7_members cate_cnt8_members cate_cnt9_members cate_cnt10_members cate_cnt11_members cate_cnt12_members cate_cnt13_members cate_cnt14_members 
201501 93.525692 5.989799 0.455098 0.027863 0.001548 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 
201502 90.515995 8.396707 0.971026 0.107892 0.008380 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 
201503 82.525162 14.066414 2.836065 0.505229 0.061750 0.005380 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 
201504 75.546295 18.279664 4.884050 1.102780 0.172282 0.013621 0.001199 0.000109 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 
201505 71.142107 20.954861 6.278794 1.401423 0.206386 0.015837 0.000593 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 
201506 63.783161 23.386509 9.241094 2.914457 0.601408 0.067921 0.005178 0.000273 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 
201507 62.361179 23.364693 9.888232 3.445630 0.812055 0.116408 0.010563 0.001240 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 

Показанный выше балл данных pandas показывает процентные доли разных категорий, которые варьируются от месяца к месяцу. Я хочу использовать морское дно, чтобы получить планку, чей брусок состоит из процента участников из 14 категорий от столбца 2 до конца. Вот мой код:Как предотвратить перекрытие в баррете с использованием морского дна?

flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"] 
f, ax = plt.subplots(figsize=(6, 15)) 
sns.barplot(x = df['month'], y = df['cate_cnt1_members'], label='cate_cnt1_members', color=sns.color_palette("Set2", 10)[0]) 
sns.barplot(x = df['month'], y = df['cate_cnt2_members'], label='cate_cnt2_members', color=sns.color_palette("Set2", 10)[1]) 
sns.barplot(x = df['month'], y = df['cate_cnt3_members'], label='cate_cnt3_members', color=sns.color_palette("Set2", 10)[2]) 
sns.barplot(x = df['month'], y = df['cate_cnt4_members'], label='cate_cnt4_members', color=sns.color_palette("Set2", 10)[3]) 
sns.barplot(x = df['month'], y = df['cate_cnt5_members'], label='cate_cnt5_members', color=sns.color_palette("Set2", 10)[4]) 
sns.barplot(x = df['month'], y = df['cate_cnt6_members'], label='cate_cnt6_members', color=sns.color_palette("Set2", 10)[5]) 
sns.barplot(x = df['month'], y = df['cate_cnt7_members'], label='cate_cnt7_members', color=sns.color_palette("Set2", 10)[6]) 
sns.barplot(x = df['month'], y = df['cate_cnt8_members'], label='cate_cnt8_members', color=sns.color_palette("Set2", 10)[7]) 
sns.barplot(x = df['month'], y = df['cate_cnt9_members'], label='cate_cnt9_members', color=sns.color_palette("Set2", 10)[8]) 
sns.barplot(x = df['month'], y = df['cate_cnt10_members'], label='cate_cnt10_members', color=sns.color_palette("Set2", 10)[9]) 
sns.barplot(x = df['month'], y = df['cate_cnt11_members'], label='cate_cnt11_members', color=sns.color_palette("Paired")[0]) 
sns.barplot(x = df['month'], y = df['cate_cnt12_members'], label='cate_cnt12_members', color=sns.color_palette("Paired")[1]) 
sns.barplot(x = df['month'], y = df['cate_cnt13_members'], label='cate_cnt13_members', color=sns.color_palette("Paired")[4]) 
sns.barplot(x = df['month'], y = df['cate_cnt14_members'], label='cate_cnt14_members', color=sns.color_palette(flatui)[0]) 
plt.ylabel("percentage of category scope count") 
plt.xlabel(" Month") 
ax.legend(ncol=7, loc="topper middle", frameon=True) 
sns.despine(left=True, bottom=True) 

И результат ниже. Но я не хочу, чтобы они пересекались друг с другом. Я хочу, чтобы 14 компонентов суммировались как 100 и заполняли 100 полностью. Итак, как я могу это достичь? enter image description here

ответ

3

Существует тривиально простой способ сделать это в панды. Во-первых, вы должны установить индекс, как месяц, а затем просто создать столбчатую участок

df = df.set_index('month') 
df.plot.bar(stacked=True) 

Для этого в Сиборн немного сложнее. Вы должны взять на себя накопленную сумму каждой строки, а затем построить что

# set the index if you haven't 
df = df.set_index('month') 
df = df.cumsum(axis=1) 

, а затем некоторые небольшие корректировки в исходном коде. Участок тогда в обратном порядке, так что 100% баров на первом месте.

flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"] 
f, ax = plt.subplots(figsize=(10, 15)) 
sns.barplot(x = df.index, y = df['cate_cnt14_members'], label='cate_cnt14_members', color=sns.color_palette("Set2", 10)[0]) 
sns.barplot(x = df.index, y = df['cate_cnt13_members'], label='cate_cnt13_members', color=sns.color_palette("Set2", 10)[1]) 
sns.barplot(x = df.index, y = df['cate_cnt12_members'], label='cate_cnt12_members', color=sns.color_palette("Set2", 10)[2]) 
sns.barplot(x = df.index, y = df['cate_cnt11_members'], label='cate_cnt11_members', color=sns.color_palette("Set2", 10)[3]) 
sns.barplot(x = df.index, y = df['cate_cnt10_members'], label='cate_cnt10_members', color=sns.color_palette("Set2", 10)[4]) 
sns.barplot(x = df.index, y = df['cate_cnt9_members'], label='cate_cnt9_members', color=sns.color_palette("Set2", 10)[5]) 
sns.barplot(x = df.index, y = df['cate_cnt8_members'], label='cate_cnt8_members', color=sns.color_palette("Set2", 10)[6]) 
sns.barplot(x = df.index, y = df['cate_cnt7_members'], label='cate_cnt7_members', color=sns.color_palette("Set2", 10)[7]) 
sns.barplot(x = df.index, y = df['cate_cnt6_members'], label='cate_cnt6_members', color=sns.color_palette("Set2", 10)[8]) 
sns.barplot(x = df.index, y = df['cate_cnt5_members'], label='cate_cnt5_members', color=sns.color_palette("Set2", 10)[9]) 
sns.barplot(x = df.index, y = df['cate_cnt4_members'], label='cate_cnt4_members', color=sns.color_palette("Paired")[0]) 
sns.barplot(x = df.index, y = df['cate_cnt3_members'], label='cate_cnt3_members', color=sns.color_palette("Paired")[1]) 
sns.barplot(x = df.index, y = df['cate_cnt2_members'], label='cate_cnt2_members', color=sns.color_palette("Paired")[4]) 
sns.barplot(x = df.index, y = df['cate_cnt1_members'], label='cate_cnt1_members', color=sns.color_palette(flatui)[0]) 
plt.ylabel("percentage of category scope count") 
plt.xlabel(" Month") 
ax.legend(ncol=7, loc="upper center", frameon=True) 
sns.despine(left=True, bottom=True) 

enter image description here

+0

Спасибо. Кажется, что панды легче визуализировать, чем морские. – yanachen

+0

Нет, не совсем. Они оба обертывают вокруг matplotlib, и обычно у морского берега очень простой способ сделать сложные сюжеты. Вы, случается, делали планку с уложенными штангами, которая является одним из немногих участков, которые морская пехота не производит для вас из коробки, насколько я могу судить. –

+0

Существует баланс между ними. Вы со временем узнаете, какой из них использовать для какой задачи ... –

1

Рассмотрит плавление данных от широкоугольного до тех пор, а затем запустить сводную таблицу в качестве источника столбчатого графа:

from io import StringIO 
import pandas as pd 
from matplotlib import rc, pyplot as plt 
import seaborn 

data = """month,cate_cnt1_members,cate_cnt2_members,cate_cnt3_members,cate_cnt4_members,cate_cnt5_members,cate_cnt6_members,cate_cnt7_members,cate_cnt8_members,cate_cnt9_members,cate_cnt10_members,cate_cnt11_members,cate_cnt12_members,cate_cnt13_members,cate_cnt14_members 
201501,93.525692,5.989799,0.455098,0.027863,0.001548,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.0 
201502,90.515995,8.396707,0.971026,0.107892,0.008380,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.0 
201503,82.525162,14.066414,2.836065,0.505229,0.061750,0.005380,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.0 
201504,75.546295,18.279664,4.884050,1.102780,0.172282,0.013621,0.001199,0.000109,0.000000,0.000000,0.000000,0.000000,0.000000,0.0 
201505,71.142107,20.954861,6.278794,1.401423,0.206386,0.015837,0.000593,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.0 
201506,63.783161,23.386509,9.241094,2.914457,0.601408,0.067921,0.005178,0.000273,0.000000,0.000000,0.000000,0.000000,0.000000,0.0""" 

df = pd.read_csv(StringIO(data)) 

dfm = pd.melt(df, id_vars="month") 

seaborn.set() 

dfm.pivot_table(values="value", columns="variable", index="month", aggfunc='sum').plot.bar(stacked=True) 
locs, labels = plt.xticks() 
plt.legend(loc='upper center', ncol=7, frameon=True, shadow=False, prop={'size':8}) 
plt.setp(labels, rotation=0, rotation_mode="anchor", ha="center") 
plt.show() 

Stacked Bar Graph