代码之家  ›  专栏  ›  技术社区  ›  augusto carillo ferrari

用可视化工具绘制多个子图

  •  0
  • augusto carillo ferrari  · 技术社区  · 6 年前

    kaggle ,我试图得到一个频率图,在给定的发布日期的每个任天堂平台x周,这里是代码

    import pandas as pd
    df = pd.read_csv("ign.csv")
    datetime_df = pd.DataFrame({'year': df["release_year"],
                       'month': df["release_month"],
                       'day': df["release_day"]})
    df["date"] = pd.to_datetime(datetime_df)
    
    df["week_day"] = df["date"].apply(lambda x : x.weekday_name)
    
    nintendo = ['Wii','Nintendo DS','Nintendo 3DS','Nintendo DS',
                'Game Boy', 'Game Boy Color','Nintendo 64DD','Game Boy Advance',
                'New Nintendo 3DS','GameCube','Nintendo DSi','Super NES']
    
    base_nintendo = df[df["platform"].isin(nintendo)]
    
    data = base_nintendo.groupby(["platform","week_day"]).size()
    
    data =data.unstack().fillna(0).stack()
    
    data
    

    输出:

    platform          week_day 
    Game Boy          Friday         5.0
                      Monday         5.0
                      Saturday       0.0
                      Sunday         0.0
                      Thursday       0.0
                      Tuesday        4.0
                      Wednesday      8.0
    Game Boy Advance  Friday       131.0
                      Monday       109.0
                      Saturday       0.0
                      Sunday         1.0
                      Thursday     153.0
                      Tuesday      123.0
                      Wednesday    106.0
    Game Boy Color    Friday        89.0
                      Monday        43.0
                      Saturday       1.0
                      Sunday         1.0
                      Thursday      55.0
                      Tuesday       78.0
                      Wednesday     89.0
    GameCube          Friday        99.0
                      Monday       100.0
                      Saturday       3.0
                      Sunday         0.0
                      Thursday      83.0
                      Tuesday      124.0
                      Wednesday    100.0
    

    我试过做:

    data.groupby("platform").plot("barh")
    

    但它只给了我最后一个平台(wii):

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   Sai Kumar    6 年前

    一种解决办法是 seaborn 和情节 barh

    data = data.unstack().fillna(0).stack()
    data = data.reset_index().rename(columns={0:'value'})
    
    import seaborn as sns
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(figsize=(10,7))
    sns.barplot(y='platform',x='value', hue='week_day', data=data, orient='h')
    plt.show()
    
        2
  •  1
  •   CT Zhu    6 年前

    请注意,在绘图上方,每个组都有一行,例如 Super NES .... matplotlib.AxesSubplot

    groupby.plot 实际上返回一个 matplotlib.AxesSubplot文件 对象。另一方面,ipython笔记本只显示了你最后的情节。

    解决办法是:改变你的习惯 data.groupby("platform").plot("barh") my_axes = data.groupby("platform").plot("barh")

    for ax in my_axes:
        ax.savefig(filename)
    

    gp = data.groupby("platform")
    f, axes = plt.subplots(5, 5)  # or any other large enough subplot grid
    for k, ax in zip(gp.groups, axes.ravel()):
        gp.get_group(k).plot('barh', ax=ax)