1

I have an assignment that only allows matplotlib and basic python. I am unable to produce the bar chart required. Although anaconda has identified the problematic line, I am unable to understand it.

The data source is here: https://data.gov.sg/dataset/bookings-for-new-flats-annual?view_id=2cdedc08-ddf6-4e0b-b279-82fdc7e678ea&resource_id=666ed30a-8344-4213-9d2e-076eeafeddd3

Have copied a sample resource and replicated it.

import numpy as np
import matplotlib.pyplot as plt  
fname = "C:\data/bookings-for-new-flats.csv"
data = np.genfromtxt('C:\data/bookings-for-new-flats.csv', 
                        skip_header=1, 
                        dtype=[('financial_year','U50'),('no_of_units','i8')], delimiter=",",
                        missing_values=['na','-'],filling_values=[0])

labels = list(set(data['financial_year']))
labels.sort()
bookings = np.arange(0,len(labels))
bookings_values = data[['financial_year','no_of_units']]

values = bookings_values['no_of_units']

units_values = {}

for i in labels:
    valuesforFY = values[bookings_values['financial_year']==i] 
    print("No.of Units in FY: " + i + " is {}".format(valuesforFY))
    #the line below is critical
    units_values[i] = valuesforFY    

barchart = plt.bar(list(units_values.keys()), list(units_values.values()), color='b')

plt.show()

Expected a bar-chart but only received a empty one.

The system identified this line as problematic --->

barchart = plt.bar(list(units_values.keys()), list(units_values.values()), color='b')
Sheldore
  • 37,862
  • 7
  • 57
  • 71
user10823750
  • 17
  • 1
  • 4

1 Answers1

0

The problem was in reading the y-data (values of the dictionary) which were single values enclosed in an array and hence you were getting a list of arrays.

Following is the solution: Iterate over the values and store only the data which can be accessed using index 0 as [0]. Here I am rewriting your code by first extracting the x-values in xdata and then the y-values in ydata for the sake of readability.

xdata =  list(units_values.keys())
ydata = [i[0] for i in units_values.values()]
barchart = plt.bar(xdata, ydata, color='b')

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thanks! However another problem arise when I tried to label the chart using for i in range(len(barchart)): bar = barchart[i] x,y = bar.get_xy() h = bar.get_height() plt.text(x,h,"{:.0f}".format(list(units_values.values())[i]),fontsize=15) – user10823750 Dec 26 '18 at 13:43
  • Did you have a look [here](https://matplotlib.org/examples/api/barchart_demo.html) to put values on the bar chart? – Sheldore Dec 26 '18 at 14:00
  • You can also have a look [here](https://stackoverflow.com/questions/25447700/annotate-bars-with-values-on-pandas-bar-plots) and [here](https://stackoverflow.com/questions/28931224/adding-value-labels-on-a-matplotlib-bar-chart) for annotating a bar plot – Sheldore Dec 26 '18 at 14:01
  • I managed to get the labels by copying the code from the link. Thank you https://i.imgur.com/zuE1fMa.png I am still unclear about the mechanics behind it but I will read up on the links you have provided. – user10823750 Dec 26 '18 at 14:19