TypeError: unhashable type: 'numpy.ndarray' error when trying to create a bar chart using two seperate column names as x and y

Question:

I am trying to plot a column as the x axis and another column as the y axis but get the above error. I can see the data and can access it using head() or sum().

Here is the code itself:

dengue_by_region = ph_dengue_cases2016_2020.groupby('Region')
dengue_by_region.head()

x = dengue_by_region['Dengue_Cases'].sum()
y = dengue_by_region['Region']
plt.bar(x, y)

I am unsure why I am unable to using x on the x axis and y on the y without it bringing up the error. Any help would be very much appreciated. I am using Jupyter Notebook if that helps.

Asked By: Muhammad Ali

||

Answers:

plt.bar(x, y) function makes a bar plot with x as labels at the bottom, and y as values, both must be either a scalar or list-like. E.g., like in picture:
enter image description here

Now I’m not sure of what types exactly are x and y in your code, but I guess that at least one of them is not scalar or list-like.

I presume that what you are trying to achieve is a bar plot with Regions as x-labels, and Sum as y-labels. And for that it appears you don’t need to change much, as @MichaelDelgado pointed in comments:

# groupby will return series, 
# with "Regions" as index (x-labels for the plot) 
# and sum by regions as values (y-labels for the plot)
x = ph_dengue_cases2016_2020.groupby('Region')['Dengue_Cases'].sum()

plt.bar(x.index, x)
plt.show()

Or, you can make it all much simpler by using what pandas provides you for the plotting:

# here I start from clean plot, just in case
plt.clf()

cases_by_region = ph_dengue_cases2016_2020.groupby('Region')['Dengue_Cases'].sum()
cases_by_region.plot.bar()
plt.show()
Answered By: qaziqarta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.