Python Seaborn Facetgrid - PullRequest
       34

Python Seaborn Facetgrid

0 голосов
/ 05 июля 2018

У меня есть счетчик зеленого / оранжевого / красного цвета для каждого покупателя: Что-то вроде:

    sellor  customer    red green   orange
0   73  c1  5   96  15
1   77  c1  88  18  79
2   97  c1  58  59  71

Который я могу построить:

df = pd.DataFrame()
df["sellor"]   = np.random.randint(0,100, 20)
df["customer"]   = ["c1"]*5 + ["C2"]*5 + ["C3"]*5 + ["c4"]*5
df["red"] = np.random.randint(0,100, 20)
df["green"] = np.random.randint(0,100, 20)
df["orange"] = np.random.randint(0,100, 20)

df.sellor = df.sellor.astype("category")
df.customer = df.customer.astype("category")

Теперь я хочу представить данные в виде графиков: Сейчас я делаю:

for customer in df.customer.unique():
    df[df.customer==customer].plot.bar(title=customer)

, что дает мне 4 изображения, как следующие:

enter image description here

Я думаю, что мог бы сделать то же самое с фацетной сеткой морского волка, но на самом деле не нашел пути. Я попробовал:

sns.barplot(data=df, x="sellor", y="red", hue="customer")

но он дает мне только один участок (без деления на клиентов) + Он не дает мне 3 бара от продавцов :( enter image description here

Как бы я использовал фазовую сетку, чтобы получить тот же результат, что и мой цикл?

1 Ответ

0 голосов
/ 05 июля 2018

Без использования seaborn вы можете наносить свои столбцы на разные участки одной фигуры,

import matplotlib.pyplot as plt

u = df.customer.unique()
fig, axes = plt.subplots(ncols=len(u), figsize=(10,3))

for customer, ax in zip(u, axes):
    df[df.customer==customer].plot.bar(x="sellor", title=customer, ax =ax )

plt.tight_layout()    
plt.show()

enter image description here

Вы можете добиться чего-то подобного с FacetGrid от Seaborn через

import seaborn as sns
g = sns.FacetGrid(data=df, col="customer", col_order=["c1", "C2", "C3", "c4"])

def plot(*args,**kwargs):
    kwargs["data"].plot(kind="bar", x="sellor", ax=plt.gca())
g.map_dataframe(plot)
plt.show()

enter image description here

Наконец, чтобы изменить цвета, используемые здесь, чтобы они соответствовали цветам имен фреймов данных, вы можете установить цветовой цикл, например

plt.rcParams["axes.prop_cycle"] = plt.cycler("color", ["red", "green", "orange"])
...