1

I am trying to compare my data with a poisson distribution, I need both my data and the poisson distribution to be bars on a histogram. However, the bars are the same size and the poisson distribution bars are on top of my data (see picture). How do I make it so the bars are side by side?

import math
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import poisson

n_counts = np.arange(10)
f_measured = np.array([45,54,55,24,14,7,1]) # How often count occured

f_mean = np.sum(f_measured*n_counts) / np.sum(f_measured)

f_poisson = poisson.pmf(n_counts,f_mean)  # Poisson distribution
f_poisson = f_poisson * f_measured.sum()  # Normalize to total number of trials

plt.bar(n_counts,f_measured,color = 'b')
plt.xlabel('Number of Counts')
plt.ylabel('Frequency')

ind = np.arange(n_counts+1)  # the x locations for the groups
width = 0.35       # the width of the bars

f_poisson = poisson.pmf(n_counts,f_mean)  # Poisson distribution
f_poisson = f_poisson * f_measured.sum()

plt.bar(n_counts,f_poisson,color='r')

enter image description here

I added in

ind = np.arange(n_counts+1) # the x locations for the groups

width = 0.35 # the width of the bars

and now it gives me this error:

enter image description here

matryoshka
  • 135
  • 8
  • 2
    possible duplicate of https://stackoverflow.com/questions/10369681/how-to-plot-bar-graphs-with-same-x-coordinates-side-by-side-dodged – trsvchn Apr 09 '19 at 19:27
  • Possible duplicate of [How to plot bar graphs with same X coordinates side by side ('dodged')](https://stackoverflow.com/questions/10369681/how-to-plot-bar-graphs-with-same-x-coordinates-side-by-side-dodged) – minterm Apr 09 '19 at 19:31
  • @tsavchyn I visited that question and I attempted to do "ind = np.arange(n_counts)" and it told me "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" – matryoshka Apr 09 '19 at 19:34
  • Possible duplicate of [Too much space between bars in matplotlib bar chart](https://stackoverflow.com/questions/43094727/too-much-space-between-bars-in-matplotlib-bar-chart) – cookiedough Apr 09 '19 at 19:37
  • Post your attempt with the full error message and we can go from there. – BenT Apr 09 '19 at 19:37
  • @BenT I edited the post with the attempt that I found from the other questions, and also included a screenshot of what error I get when I try to use it. It says (n_counts+1), but it didn't work with strictly (n_counts) either. – matryoshka Apr 09 '19 at 19:42
  • `n_counts` is an array not a value. Do `ind = n_counts+1` – BenT Apr 09 '19 at 19:43
  • @matryoshkawere you able to solve your issue? – BenT Apr 12 '19 at 21:17
  • @BenT Yes I did, thank you! – matryoshka Apr 14 '19 at 19:10

0 Answers0