I am a beginner and I am trying to create a bar chart on matplotlib. I am importing a CSV file with daily volume data on a financial instrument. I have successfully been able to summarize the data by month and now trying to chart the monthly volume figures.
I am trying to format the X-axis data as it currently shows minutes, hours and second in addition to days, month and years.
I suspect this is quite straightforward, but have been sitting on this problem for hours and been unable to resolve it. Any help would be greatly appreciated. I need the date formatting to be shown in YYYY-MM rather than that is shown here:
``
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates
df = pd.read_csv('C:/Users/gregor/Documents/Python Scripts/data.csv', infer_datetime_format='True')
df['date']=pd.to_datetime(df['date'])
#
df.set_index('date', inplace=True)
volume = df['volume'].resample('MS').sum()
oi = df['open interest'].resample('MS').mean()
xaxis = np.arange(len(volume))
plt.bar(xaxis,volume,align='edge')
plt.xticks(xaxis,volume.index, rotation = 45)
plt.autofmt_xdate()
plt.show()
``
Edit - it worked with the following code and the chart now looks like:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates
df = pd.read_csv('C:/Users/gregor/Documents/Python Scripts/data.csv', infer_datetime_format='True')
df['date']=pd.to_datetime(df['date'])
#
df.set_index('date', inplace=True)
myFmt = mdates.DateFormatter('%Y-%m')
volume = df['volume'].resample('MS').sum()
oi = df['open interest'].resample('MS').mean()
xaxis = np.arange(len(volume))
fig,ax = plt.subplots()
ax.bar(volume.index,volume,width=15)
ax.xaxis.set_major_formatter(myFmt)
plt.show()