Skip to content Skip to sidebar Skip to footer

Manipulating Dates In X-axis Pandas Matplotlib

I have a pretty simple set of data as displayed below. I am looking for a way to plot this stacked bar chart and format the x-axis (dates) so it starts at 1996-31-12 and ends at 20

Solution 1:

This is a similar question: Pandas timeseries plot setting x-axis major and minor ticks and labels

You can manage this using matplotlib itself instead of pandas.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# if your dates are strings you need this step
df.Date = pd.to_datetime(df.Date)

fig,ax = plt.subplots()
ax.plot_date(df.Date,df.A)
ax.plot_date(df.Date,df.B)
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b\n%Y'))
plt.show()

Post a Comment for "Manipulating Dates In X-axis Pandas Matplotlib"