0

I have an array: [0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1] (16 long) How can I create density histogram with, for instance bins=4 to see where there appears to be most 1:s? This histogram would for instance be very tall in the middle part, and raise at the end a little (most 1:s in the beginning and the end). I have this:

plt.hist([0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1], bins=4)

This is what i get. This histogram just presents that it's as many 1:s as 0:s.

How can I later create a graph (line) to show me the average raise and fall och the histogram?

Community
  • 1
  • 1
Filip Mellqvist
  • 113
  • 1
  • 6

2 Answers2

0

The histogram will not evaluate the positions of your values; it's a representation of the distribution of the data, the positions of the values in hist are irrelevant; with the min and max representing the min and max of the data. I'd try setting an index and using that as your range instead.

I think this is the closest answer to the graph you've described: Pandas bar plot with binned range The importanceOfBeingEnrnest's answer here may also be helpful.

Plot a histogram using the index as x-axis labels

Elizabeth
  • 31
  • 7
0

I wouldn't call this a histogram. It's more a plot of spacial density. So you can enumerate your list, such that the first element has the number 0, the last the number 15. Then divide this list into 4 bins. Within each bin you can count how often you see a 1. To automate this, an option is scipy.stats.binned_statistic.

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import binned_statistic

data = [0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1]
x = np.arange(len(data))

s, edges, _ = binned_statistic(x, data, bins=4, statistic = lambda c: len(c[c==1]))
print(s, edges)
plt.bar(edges[:-1], s, width=np.diff(edges), align="edge", ec="k")
plt.show()

So here the edges are [0., 3.75, 7.5, 11.25, 15.] and the number of 1 within each bin is [0. 3. 2. 3.].

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712