CG7
CG7

Reputation: 111

Remove empty space from seaborn boxplot

I made a seaborn boxplot but it contains an empty space on the left side of the plot

print(rain_df)
            date  rain_gauge_value_24hr
96    2017-01-11                    9.0
120   2017-01-12                    5.3
144   2017-01-13                    5.7
240   2017-01-17                    1.0
288   2017-01-19                    7.0
...          ...                    ...
37920 2021-05-06                    4.0
38064 2021-05-12                    1.8
38088 2021-05-13                    0.2
38184 2021-05-17                    2.0
38352 2021-05-24                    0.2
rain_month = rain_df[['date','rain_gauge_value_24hr']]

rain_month['month'] = pd.Categorical.from_codes(rain_month['date'].dt.month,categories=list(calendar.month_name),ordered=True)

rain_month = rain_month.melt(id_vars ='month', value_vars=['rain_gauge_value_24hr'], value_name='rain_gauge_value_24hr')

print(rain_month)
       month               variable  rain_gauge_value_24hr
0    January  rain_gauge_value_24hr                    9.0
1    January  rain_gauge_value_24hr                    5.3
2    January  rain_gauge_value_24hr                    5.7
3    January  rain_gauge_value_24hr                    1.0
4    January  rain_gauge_value_24hr                    7.0
..       ...                    ...                    ...
836      May  rain_gauge_value_24hr                    4.0
837      May  rain_gauge_value_24hr                    1.8
838      May  rain_gauge_value_24hr                    0.2
839      May  rain_gauge_value_24hr                    2.0
840      May  rain_gauge_value_24hr                    0.2
ax=sn.boxplot(x='month', y='rain_gauge_value_24hr', hue='variable', data=rain_month, palette="Set3", linewidth=1)

ax.set_title('Joliette')
plt.xticks(rotation=45)
ax.set_yscale('log')

ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

enter image description here

What's causing that empty spot on the plot? How can I fix this? Is there a better approach to making this plot?

EDIT:

rain_month['month'].value_counts(dropna=False)
January      102
February      99
March         80
April         78
December      71
November      67
May           66
July          63
August        62
June          56
October       55
September     42
               0
Name: month, dtype: int64

Hmm, thats strange.

Upvotes: 0

Views: 947

Answers (2)

saaj
saaj

Reputation: 25204

Python documentation for calendar.month_name says this:

An array that represents the months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and month_name[0] is the empty string.

>>> import calendar
... calendar.month_name[:4]
['', 'January', 'February', 'March']

You see the first empty string as the first month tick on X axis. You can get rid of it like this:

rain_month['month'] = pd.Categorical.from_codes(
    rain_month['date'].dt.month - 1, 
    calendar.month_name[1:],
    ordered=True,
)

Upvotes: 2

mozway
mozway

Reputation: 260630

Try to change the x-axis limits with set_xlim

ax.set_xlim(xmin=0.5)

Upvotes: 1

Related Questions