2

I have created a cumulative (CDF) histogram from a list which is what I wanted. Then I subtracted a fixed value (by using x = [ fixed_value - i for i in myArray]) from each element in the list to essential just shift the bins over a fixed amount. This however makes my CDF histogram inverted in the y-axis. I thought it should look identical to the original except the x-axis (bins) are shifted by a fixed amount.

So can someone explain what I am doing wrong or give a solution to just shifting the bins over instead of recreating another histogram with a new array?

EDIT:

Sometimes I see this error:

>>> plt.hist(l,bins, normed = 1, cumulative = True)
C:\Python27\lib\site-packages\matplotlib\axes.py:8332: RuntimeWarning: invalid value encountered in true_divide
  m = (m.astype(float) / db) / m.sum()

But it is not exclusive to the second subtracting case. And plt.hist returns an NaN array. Not sure if this helps but I am getting closer to figuring it out I think.

EDIT: Here are my two graphs. The first is the "good" one. The second is the shifted "bad" one:

All I want to do is shift the first one bins over by a fixed amount. However, when I subtract the same value from each list that is in the histogram it seems to alter the histogram in the y direction and in the x-direction. Also, note how the first histogram are all negative values, and the second is positive. I seemed to fix it by keeping it negative (I use original_array[i] - fixed_value <0, instead fixed_value - original_array[i] > 0)

laserpython
  • 300
  • 1
  • 6
  • 21
  • Inverted in the y-axis.....? Can you show some resulting figures, and/or a minimal working code to reproduce the problem? As for the `invalid values`, `db` is calculated as `db = np.diff(bins)` in Matplotlib, so the only thing I can think of is that you provide one or more bins of size zero? – Bart Jun 13 '16 at 19:33
  • @Bart Thanks for the reply. I made some edits. Does this help clarify? I use the same code for both graphs, except that on the second one I iterate through my array and subtract a fixed value first (plus change the bins), and the first graph is what I want. The second graph should be same just different x-axis. – laserpython Jun 13 '16 at 19:54
  • 2
    I think you watn to do `[i - fixed_value ....]` – MaxNoe Jun 13 '16 at 20:02
  • @MaxNoe I think you are right and that seems to be the consensus. Thanks! – laserpython Jun 13 '16 at 20:49

2 Answers2

1

I think that the problem might be in how you calculate the shifted values. This example works fine for me:

import numpy as np
import matplotlib.pylab as pl

original_array = np.random.normal(size=100)
bins = np.linspace(-5,5,11)

pl.figure()
pl.subplot(121)
pl.hist(original_array, bins, normed=1, cumulative=True, histtype='step')

offset = -2
modified_array = [original_value + offset for original_value in original_array]

pl.subplot(122)
pl.hist(modified_array, bins, normed=1, cumulative=True, histtype='step')

enter image description here

Note that numpy might make your life easier (and for large sizes of original_array, a lot faster); for example if your data is a numpy.array, you can also write it as:

modified_array = original_array + offset
Bart
  • 9,825
  • 5
  • 47
  • 73
1

Your problem has nothing to do with histograms, but purely with the fact that you write x = [ fixed_value - i for i in myArray], rather than x = [i - fixed_value for i in myArray]. Check the following example:

import numpy as np
import pylab as pl

x = np.linspace(0, 10., 1000)
y = np.exp(x)

x2 = [ 5. - i  for i in x]
x3 = [ i  - 5. for i in x]

pl.plot(x , y)
pl.plot(x2, y)
pl.plot(x3, y)

If you plot those, you see that the second plot is a mirrored, shifted version of the first plot and that the third plot has the shift that you are looking for.

Chiel
  • 6,006
  • 2
  • 32
  • 57
  • Thank you for your answer, I really appreciate it. That makes sense...but I have only "y-values", so I shifted the "y-values". Then I put each of them into bins by calling pyplot.hist(). I am seeing an inversion about the x-axis, not about the y-axis as your example shows. – laserpython Jun 13 '16 at 20:45