代码之家  ›  专栏  ›  技术社区  ›  Sai Kumar

如何使用我的数据制作更具视觉吸引力的条形图

  •  0
  • Sai Kumar  · 技术社区  · 8 年前

    我试图用matplotlib中的barh绘制这个数据帧。

    SAMPLE DF:
    
        genre              tracks_sold   percentage
    0   Rock                  561        53.377735
    1   Alternative & Punk    130        12.369172
    2   Metal                 124        11.798287
    3   R&B/Soul               53         5.042816
    4   Blues                  36         3.425309
    
    genre_sold_usa.plot.barh()
    

    PLOT

    我试图使这个情节在视觉上更具信息性。

    enter image description here

    请告诉我还有什么可以使它具有视觉吸引力。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sheldore    8 年前

    鉴于 df 作为数据帧,我只是试图复制您提供的示例。在我看来,轮换 y

    fig = plt.figure(figsize=(10, 6))
    ax = df.plot.barh(y='tracks_sold', x='genre', color='mediumseagreen', ec='k', lw=1)
    plt.xlim(0, 630)
    plt.yticks(rotation=30)
    plt.ylabel('')
    plt.title('Top Selling Genres in the USA')
    ax.legend_.remove()
    
    # Way 1 of putting percentage values
    for i in range(len(df)):
        ax.text(tracks_sold[i] + 5, 0.98*i, str(int(percentage[i])) + '%', color='black', fontweight='bold')
    
    # Way 2 of putting percentage values
    # for i, p in enumerate(ax.patches):
    #    ax.annotate(str(int(percentage[i])) + '%', (p.get_width() * 1.02, i)) 
    

    enter image description here