pandas / matplotlib: faceting bar plots

前端 未结 3 2163
故里飘歌
故里飘歌 2021-02-09 20:25

I am making a series of bar plots of data with two categorical variables and one numeric. What i have is the below, but what I would love to do is to facet by one of the categor

3条回答
  •  无人及你
    2021-02-09 21:08

    @tcasell suggested the bar call in the loop. Here is a working, if not elegant, example.

    ## second try--facet by county
    
    N = 100
    industry = ['a','b','c']
    city = ['x','y','z']
    ind = np.random.choice(industry, N)
    cty = np.random.choice(city, N)
    jobs = np.random.randint(low=1,high=250,size=N)
    df_city =pd.DataFrame({'industry':ind,'city':cty,'jobs':jobs})
    
    ## how many panels do we need?
    cols =df_city.city.value_counts().shape[0]
    fig, axes = plt.subplots(1, cols, figsize=(8, 8))
    
    for x, city in enumerate(df_city.city.value_counts().index.values):
        data = df_city[(df_city['city'] == city)]
        data = data.groupby(['industry']).jobs.sum()
        print (data)
        print type(data.index)
        left=  [k[0] for k in enumerate(data)]
        right=  [k[1] for k in enumerate(data)]
    
        axes[x].bar(left,right,label="%s" % (city))
        axes[x].set_xticks(left, minor=False)
        axes[x].set_xticklabels(data.index.values)
    
        axes[x].legend(loc='best')
        axes[x].grid(True)
        fig.suptitle('Employment By Industry By City', fontsize=20)
    

    enter image description here

提交回复
热议问题