YieldedTemparz
YieldedTemparz

Reputation: 63

Matplotlib conversion failure of dates array to axis units

I am following a plot tutorial on:

https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781849513265/1/ch01lvl1sec16/plotting-multiple-bar-charts

But am subject to the error:

f'units: {x!r}') from e matplotlib.units.ConversionError: Failed to convert value(s) to axis units: ['11-2019', '12-2019', 0] [Finished in 7.199s]

I believe it has something to do with converting my date value array to x ticks

#Arrays below represent the number of sales in each month from queries above in my code which i have not 
#included in this post. e.g. soya1 = [4,2,6].

#All arrays are the same length and are the same length as the date array

soya1, wholeMilk1, coconut1, semiSkimmed1, decaf1, skimmed1, dates = ([] for i in range(7))

dates = ['11-2019', '12-2019', '01-2020']

data = []
data.extend((soya1,wholeMilk1,coconut1,semiSkimmed1,decaf1,skimmed1))

gap = .8 / len(data)
for i, row in enumerate(data):
X = np.arange(len(row))
    plt.bar(X + i * gap, row,
    width = gap)

    plt.xticks(np.arange(len(dates)),dates)
    print('dates',dates)
    plt.ylabel('Volume')
    plt.xlabel('Dates')
    plt.title('Milk Options')
    plt.show()

Upvotes: 4

Views: 10334

Answers (1)

Reveille
Reveille

Reputation: 4629

Try converting dates from string to date object first:

from datetime import datetime

dates = ['11-2019', '12-2019', '01-2020']
dates = [datetime.strptime(x, '%m-%Y') for x in dates]

Upvotes: 2

Related Questions