Format dates in pandas bar plots

When using dates as axis labels of a pandas barplot (, dates are rendered with time as well, e.g. 2021-01-01 00:00:00.

Below are two methods to display the date with a more suitable format.

# Import libraries
import pandas as pd
from numpy.random import randint

# Create sample data
df = pd.DataFrame({'date':pd.date_range(start='01/01/2021', periods=15), 
                   'value':randint(10, 20, 15)})

# Default plot with full datetime'date');


Keep only the date part with

# Keep only the date
df.assign(date=lambda x: x['date']'date');


More control with dt.strftime

# Format date with strftime
df.assign(date=lambda x: x['date'].dt.strftime('%b %d'))'date');