1

I write a function to create barplot based on the column provide:

def bar_plot(dataset, col, figsize=(16,8)):
    fig, ax = plt.subplots(figsize=figsize)
    for loc in ['bottom', 'left']:
        ax.spines[loc].set_visible(True)
        ax.spines[loc].set_linewidth(2)
        ax.spines[loc].set_color('black')
        
    data = dataset[col].value_counts().reset_index()
    ax = sns.barplot(data=data,x=col,y='index',orient='h', linewidth=1, edgecolor='k',color='#005EB8')
            
    plt.title(f'Change counts by: {col.capitalize()}', size=16, fontweight='bold', color='#425563')
    ax.set_ylabel('')
    
    for p in ax.patches:
        width = p.get_width()
        plt.text(p.get_width(), 
                 p.get_y()+.55*p.get_height(),
                 round(width),
                 va='center',
                 color='#425563')

When I provide the month in number, the plot is showing OK like below:

Enter image description here

However, if I provide the full month name the last two values (Nov and Dec) are mingled in the plot:

Enter image description here

I have been researching on it for some time now (I adjusted the yticks, ylim, etc.), but it seems without any luck so far. I can do with the month in number, but how can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1cjtc jj
  • 77
  • 4

1 Answers1

0

The issue was when I assigned month name in categorical order. I missed a "," between November and December. The issue is clear now after I insert the "," in that line of code.

month_order = ['January', 'February', 'March', 'April', 'May','June','July','August','September', 'October','November''December'] 
raw_df['month_name'] = pd.Categorical(raw_df.month_name,categories=month_order,ordered=True)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1cjtc jj
  • 77
  • 4