Python numpy.linspace实用方法

2019-01-25  本文已影响0人  GYBE

官方文档: numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)

Parameters

Returns

Examples

import numpy as np
np.linspace(-10, 10, 11)
# array([-10.,  -8.,  -6.,  -4.,  -2.,   0.,   2.,   4.,   6.,   8.,  10.])

import matplotlib.pyplot as plt
num = 11
y = np.zeros(num)
endpoint_T = np.linspace(-10, 10, num, endpoint = True)
endpoint_F = np.linspace(-10, 10, num, endpoint = False)

plt.plot(endpoint_T, y, 'o')
plt.plot(endpoint_F, y + 0.5, 'o')
plt.ylim([-0.5, 1])
xticks(np.linspace(-10, 10, num, endpoint = True))
plt.show()

# 设置dtype
lin_dtype = np.linspace(-10, 10, 11, dtype = np.int)
lin_dtype
# array([-10,  -8,  -6,  -4,  -2,   0,   2,   4,   6,   8,  10])
print(lin_dtype.dtype) # int64
上一篇 下一篇

猜你喜欢

热点阅读