Creating a bar chart to compare data

1

I'm doing the udacity data science course. It's my first contact with programming, so do not judge the silly mistakes haha.

I am comparing two data frames with 2008 and 2018 year car information. One of the questions asks to compare the improvement in fuel consumption by vehicle class.

I took the consumption averages of each class using grupby, then threw those averages into a variable and tried to create a graph to compare the evolution from one year to the next. My attempt was this:

ind = np.arange(3, 7)
width = 0.2
labels = ['SUV', 'LC', 'MC', 'Minivan', 'Pickup', 'SC', 'SW']

bar_08 = plt.bar(ind, [18.47, 18.50, 21.60, 19.11, 16.27, 21.09, 22.36], width, color='r', label=2008)
bar_18 = plt.bar(ind+width, [17.19, 22.67, 26.90, 20.80, 18.08, 24.37, 27.52], width, color='b', label=2018)

location = ind+width/2
labels=labels
plt.xticks(locatio, labels)
plt.legend()

The error that appears is as follows: shape mismatch: objects can not be broadcast to a single shape

Can anyone enlighten me on this issue?

    
asked by anonymous 31.03.2018 / 16:51

1 answer

0

Good afternoon Victor, how are you?

Following this code provided by the matplotlib website: Matplot Barplot Tutorial

We can see that it defines the index as a fixed value, in the case of 5 (variable N ) and then passes this value to ind=np.arange(N) and creates the range. It is and the amount of data to be plotted as we see for example in men_means = (20, 35, 30, 35, 27) , are 5 values.

In your case, you defined a range of 3 to 6, with np.arange(3,7) and you are trying to plot 7, [18.47, 18.50, 21.60, 19.11, 16.27, 21.09, 22.36] so you get the shape error, let's say that the matplot was not prepared to receive 7 values , since only 4 (np.arange (3,7) returns us [3, 4, 5, 6]).

If you change the value from 7 to 10 in arange , you would be able to plot. But the code is less readable.

You could just as in the tutorial above, set the number of data you want to plot in a variable, and move to arange , for example:

N = 7
ind = np.arange(N)
width = 0.2
labels = ['SUV', 'LC', 'MC', 'Minivan', 'Pickup', 'SC', 'SW']
But whenever you add a data or a label on the X axis, you would have to change this value, so to follow for example the number of labels, you can use the size of the label to define the amount of data plotted. >
ind = np.arange(len(labels))
width = 0.2
labels = ['SUV', 'LC', 'MC', 'Minivan', 'Pickup', 'SC', 'SW']

In both you will have the following return graphically.

Ifyouwanttoaddmorecolumnsforexample,usethesecondform,andyouonlyhavetoaddnewvaluesinthelabelslistandinthedatatobeplotted.

ind=np.arange(len(labels))width=0.2labels=['SUV','LC','MC','Minivan','Pickup','SC','SW','TESTE']bar_08=plt.bar(ind,[18.47,18.50,21.60,19.11,16.27,21.09,22.36,5],width,color='r',label=2008)bar_18=plt.bar(ind+width,[17.19,22.67,26.90,20.80,18.08,24.37,27.52,5],width,color='b',label=2018)location=ind+width/2labels=labelsplt.xticks(location,labels)plt.legend()

I hope to have helped, sorry if I was long-winded, if I can help you in other doubts feel free to ask.

Thank you.

    
11.04.2018 / 21:33