Skip to content
Advertisement

Matplotlib stacked histogram numpy.ndarray error

I am trying to make a stacked histogram using matplotlib by looping through the categories in the dataframe and assigning the bar color based on a dictionary.

I get this error on the ax1.hist() call. How should I fix it? AttributeError: 'numpy.ndarray' object has no attribute 'hist'

Reproducible Example

JavaScript

Updated effort, still not working

This is close, but it is not stacking (should see stacks at y=5); I think maybe because of the loop?

JavaScript

enter image description here

Advertisement

Answer

To draw on a specific subplot, two indices are needed (row, column), so axes[0,0] for the first subplot. The error message comes from using ax1 = axes[0] instead of ax1 = axes[0,0].

Now, to create a stacked histogram via ax.hist(), all the y-data need to be provided at the same time. The code below shows how this can be done starting from the result of groupby. Also note, that when your values are discrete, it is important to explicitly set the bin boundaries making sure that the values fall precisely between these boundaries. Setting the boundaries at the halves is one way.

Things can be simplified a lot using seaborn’s histplot(). Here is a breakdown of the parameters used:

  • data=df the dataframe
  • y='y' gives the dataframe column for histogram. Use x= (instead of y=) for a vertical histogram.
  • hue='cat' gives the dataframe column to create mulitple groups
  • palette=mycolorsdict; the palette defines the coloring; there are many ways to assign a palette, one of which is a dictionary on the hue values
  • discrete=True: when working with discrete data, seaborn sets the appropriate bin boundaries
  • multiple='stack' creates a stacked histogram, depending on the hue categories
  • alpha=1: default seaborn sets an alpha of 0.75; optionally this can be changed
  • ax=axes[0, 1]: draw on the 2nd subplot of the 1st row
JavaScript

example plot

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement