Home > Enterprise >  TypeError: unhashable type: 'numpy.ndarray' error when trying to create a bar chart using
TypeError: unhashable type: 'numpy.ndarray' error when trying to create a bar chart using

Time:09-10

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.

CodePudding user response:

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. To do that, you need to change your code into something like this:

# List of region names, in order
x = [region for region,_ in dengue_by_region['Region']]

# List of values, for each region
y = list(dengue_by_region['Dengue_Cases'].sum())

plt.bar(x, y)
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()
  • Related