2

I am trying to plot a histogram with mu and sigma.

Im trying to use the ec_scores values on the y axis, its supposed to show me 0.1 to 1.0 It gives me 1, 2, 3, 4, 5, 6 on the y axis instead. Im not getting any errors but this is throwing off the graph completely. Please assist me and tell me what I am doing wrong and how can i get the graph to be generated properly. Thanks.

This is my code :

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.mlab as mlab

x = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])

ec_scores = np.array([1., 1., 1., 0.95923677, 0.94796184, 1., 0.76669558, 1., 0.99913194, 1.])

mu, sigma = (np.mean(ec_scores), np.std(ec_scores))


fig = plt.figure()

ax = fig.add_subplot(111)

n, bins, patches = ax.hist(x, 50, normed=1, facecolor='blue', alpha=0.75)


bincenters = 0.5*(bins[1:]+bins[:-1])

y = mlab.normpdf( bincenters, mu, sigma)

l = ax.plot(bincenters, y, 'r--', linewidth=1)

ax.set_xlabel('Parameters')

ax.set_ylabel('EC scores ')

plt.plot(x, ec_scores)

ax.grid(True)

plt.show()

Currently the graph looks like this: enter image description here

Fred Barclay
  • 834
  • 1
  • 13
  • 24
Deepak M
  • 1,124
  • 2
  • 18
  • 28
  • 2
    histograms, by definition, plot the "counts" on the y axis. it seems you want probabilities, which means you want to use a barplot function rather than a histogram function. – benten Sep 17 '16 at 01:12
  • @benten This is not true at all. plotting a histogram a probabliity density is perfectly valid. I think the OP is looking for the `normed` kwarg (see http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.hist or https://stackoverflow.com/questions/5498008/pylab-histdata-normed-1-normalization-seems-to-work-incorrect) – tacaswell Sep 17 '16 at 22:49
  • I stand corrected. [From wikipedia](https://en.wikipedia.org/wiki/Histogram) "A histogram may also be normalized displaying relative frequencies." Also, bar charts are preferred for categorical data while histograms are preferred for continuous data. From a matplotlib standpoint you can generate the same picture with either, but is interesting all the same. Thanks tacaswell. – benten Sep 18 '16 at 00:26

1 Answers1

1

As mentioned by @benton in the comments, you can plot the ec_scores as a barchart:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
ec_scores = np.array([1., 1., 1., 0.95923677, 0.94796184, 1., 0.76669558, 1., 0.99913194, 1.])
mu, sigma = (np.mean(ec_scores), np.std(ec_scores))

fig = plt.figure()
ax = fig.add_subplot(111)
rects = ax.bar(x, ec_scores, width=0.1, align='center', facecolor='blue', alpha=0.75)

ax.set_xlabel('Parameters')
ax.set_ylabel('EC scores ')
ax.grid(True)

plt.show()

Which will look like this: Plotted as a barplot.

You can also add error bars by passing an array to the yerr parameter. This is illustrated in the barchart example I linked to above. I am not sure what exactly you are trying to do with normpdf, but the barchart returns a list of rectangles instead of bins, so you might need to adapt this to your code.

I hope this helps.

m00am
  • 5,910
  • 11
  • 53
  • 69