numpy数值微分

时间:2017-09-27 22:59:53

标签: python numpy numerical-methods numerical

我在阵列宽度上绘制了一条曲线,列出了数组列的总和:

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()

注释掉的函数是我尝试数值计算曲线的导数,但我得到了

  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'

问题herehere并不是我试图解决的问题。如何修复我的小功能来绘制衍生物?或者有内置功能吗?所有内置插件(例如我发现的scipy.misc.derivative都取决于您是否知道您要区分的功能。

1 个答案:

答案 0 :(得分:2)

在Python v3.x range()中创建range object而不是列表。显然,范围对象不支持算术。请尝试使用numpy ndarray

x = np.arange(nodisk_data.shape[1])