1

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: enter image description 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)


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()
Armali
  • 18,255
  • 14
  • 57
  • 171
Gregor
  • 11
  • 2
  • You imported `mdates` but don't seem to be using it anywhere. Have a look [here](https://stackoverflow.com/questions/14946371/editing-the-date-formatting-of-x-axis-tick-labels-in-matplotlib) to see how to specify a format, and then use that to format your labels. – ALollz Sep 26 '18 at 21:43
  • 1
    thanks Paul, will update the solution - it worked with a little tweak! – Gregor Sep 26 '18 at 22:01

0 Answers0