The following code provides a solution.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Data example:
df = pd.DataFrame([{'date': '3/22/20', 'quarter': 1, 'val':2},{'date': '3/22/20', 'quarter': 1, 'val':5}, {'date': '6/22/20', 'quarter': 3, 'val':7},{'date': '6/22/20', 'quarter': 3, 'val':8}, {'date': '9/22/20', 'quarter': 2, 'val':10},{'date': '11/22/20', 'quarter': 2, 'val':11}])
Convert to quarters.
df['date'] = pd.to_datetime(df.date)
df['quarter'] = pd.PeriodIndex(df['date'], freq='Q')
df['quarterc'] = df['quarter'].astype('string')
The quarter data type period[Q-DEC]
is not accepted in the x-axis of Matplotlib, but string
works.
plt.plot(df['quarterc'], df['val'])
plt.show()
plt.plot(df['quarterc'], df['val'])
plt.show()
