【问题标题】:Seaborn Countplot: Displaying the counts on barSeaborn Countplot:在条形图上显示计数
【发布时间】:2020-06-22 04:18:14
【问题描述】:

我写了以下代码:

plt.figure(figsize=(12,6)) 
ax = sns.countplot(x='Region', data=data, order=data['Region'].value_counts().iloc[:15].index)
plt.xticks(rotation='vertical')
plt.xlabel('Regions', fontsize=14)
plt.ylabel('Total Restaurants', fontsize=14)
plt.title('Total Restaurnats present in Each Regions', fontsize=20, y=1.03, weight='bold')

我的输出:

计数值

values = list(data['Region'].value_counts()[:15].values)
values

我的输出:

[203, 192, 177, 124, 113, 112, 99, 99, 94, 86, 82, 68, 67, 55, 53]

我想在每个条上显示各自的值,例如第一个条上的 203; 192 次以此类推。
有没有办法做到这一点?我会非常乐意为您提供帮助。

提前致谢!

【问题讨论】:

    标签: python-3.x matplotlib seaborn


    【解决方案1】:

    这是垂直条的版本

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    N=11
    t = np.linspace(0,0.5*np.pi,N)                                     #---- generate data ----
    df = pd.DataFrame({'A':10*np.sin(t),
                       'B':10*np.cos(t)} )
    
    fig = plt.figure(figsize=(15,8))                                   #---- set up graphics ----
    ax1 = fig.add_subplot(111)
    
    width=0.9                                                          #---- tuning parameters for the bar plot ----
    dy = 0.2
    
    df['A'].plot(kind='bar',width=width, color='b', alpha=0.3)         #---- define the bar plot ----
    for x,y in enumerate(df['A']):                                     #---- annotate the bars ----
        plt.annotate(str(np.around(y,decimals=3)), xy=(x+width/2, y+dy), va='center',ha='right', color='b', fontweight='bold', fontsize=16)
        
    df['B'].plot(kind='bar',width=width, color='r', alpha=0.2)         #---- define the bar plot ----
    for x,y in enumerate(df['B']):                                     #---- annotate the bars ----
        plt.annotate(str(np.around(y,decimals=3)), xy=(x+width/2, y+dy), va='center',ha='right', color='r', fontweight='bold', fontsize=16)
    
    plt.show()
    pic_name='psc_annotaed_vertical_bars.png'
    fig.savefig(pic_name, transparency=True)
    

    这是单杠的版本

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    N=11
    t = np.linspace(0,0.5*np.pi,N)
    df = pd.DataFrame({'A':10*np.sin(t),'B':10*np.cos(t)} )
    
    fig = plt.figure(figsize=(15,8))
    ax1 = fig.add_subplot(111)
    
    df['A'].plot(kind='barh',width=0.9, color='b', alpha=0.3) 
    for y, x in enumerate(df['A']): 
        plt.annotate(str(np.around(x,decimals=3)), xy=(x-0.01, y), va='center',ha='right', color='b', fontweight='bold', fontsize=16)
    
    df['B'].plot(kind='barh',width=0.9, color='r', alpha=0.2) 
    for y, x in enumerate(df['B']): 
        plt.annotate(str(np.around(x,decimals=3)), xy=(x-0.01, y), va='center',ha='right', color='r', fontweight='bold', fontsize=16)
    
    plt.show()
    pic_name='psc_annotaed_horizontal_bars.png'
    fig.savefig(pic_name, transparency=True)
    

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 2023-03-19
      • 1970-01-01
      • 2021-12-31
      • 2016-01-04
      • 2018-09-20
      • 2020-02-28
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多