title: numpy numerical differentiation
body: <p>I've plotted a curve along the width of an array, of the sum of the array columns:</p>
  
  <pre><code>import numpy as np
  from astropy.io import fits
  import matplotlib.pyplot as plt
  
  #def plot_slope(x, y):
  #    xs = x[1:] - x[:-1]
  #    ys = y[1:] - y[:-1]
  #    plt.plot(x[1:], ys / xs)
  
  hdulist = fits.open('w1_subtracted_2_deg.fits')
  nodisk_data, nodisk_header = hdulist[0].data, hdulist[0].header
  
  x = range(nodisk_data.shape[1])
  y = np.sum(nodisk_data, axis = 0)
  xticks = [0, 183, 365, 547, 729, 912, 1095, 1277, 1459]
  long_marks = [24, 18, 12, 6, 0, 354, 348, 342, 335]
  
  ax = plt.axes()
  ax.set_xticks(xticks)
  ax.set_xticklabels(long_marks)
  
  plt.plot(x, y, linewidth = 0.5, color = 'red')
  #plot_slope(x, y)
  plt.title('Longitudinal sum of flux density per steradian')
  plt.xlabel(r'Galactic longitude, $\ell$')
  plt.ylabel(r'Summed flux density per steradian, $MJ.sr^{-1}$')
  plt.grid(True)
  plt.show()
  
  plt.savefig('add_cols.png')
  
  hdulist.close()
  </code></pre>
  
  <p>The commented-out function is my attempt at numerically computing the derivative of the curve, but I get a</p>
  
  <pre><code>  File "C:/Users/Jeremy/Dropbox/Astro480/NEOWISE/add_cols.py", line 6, in plot_slope
      xs = x[1:] - x[:-1]
  
  TypeError: unsupported operand type(s) for -: 'range' and 'range'
  </code></pre>
  
  <p>The questions <a href="https://stackoverflow.com/questions/21069122/numerical-differentiation">here</a> and <a href="https://stackoverflow.com/questions/42547027/numerical-differentiation-in-python">here</a> aren't quite what I'm trying to solve. How can I fix my little function to plot the derivative? Or is there a built-in function to do it? All of the built-ins (such as <a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.misc.derivative.html" rel="nofollow noreferrer">scipy.misc.derivative</a> I've found depend on knowing the function you're differentiating.</p>