from numpy\core_multiarray_umath.py
@array_function_from_c_func_and_dispatcher(_multiarray_umath.lexsort)
def lexsort(keys, axis=None):
"""
lexsort(keys, axis=-1)
Perform an indirect stable sort using a sequence of keys.
使用键序列执行间接稳定排序。
Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it's rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc.
给定多个排序键(可以将其解释为电子表格中的列),lexsort返回一个整数索引数组,该数组描述按多个列排序的顺序。
序列中的最后一个键用于主排序顺序,倒数第二个键用于辅助排序顺序,依此类推。 keys参数必须是可以转换为相同形状的数组的对象序列。
如果为keys参数提供了2D数组,则将其行解释为排序键,并根据最后一行,倒数第二行等进行排序。
Parameters
----------
keys : (k, N) array or tuple containing k (N,)-shaped sequences
The `k` different "columns" to be sorted. The last column (or row if `keys` is a 2D array) is the primary sort key.
(k,N)个包含k(N,)形序列的数组或元组
要排序的k个不同的“列”。 最后一列(如果keys是2D数组,则为行)是主排序键。
axis : int, optional
Axis to be indirectly sorted. By default, sort over the last axis.
要间接排序的轴。 默认情况下,对最后一个轴进行排序。
Returns
-------
indices : (N,) ndarray of ints
Array of indices that sort the keys along the specified axis.
(N,)个整数的ndarray
沿指定轴对键进行排序的索引数组。
See Also
--------
argsort : Indirect sort.
ndarray.sort : In-place sort.
sort : Return a sorted copy of an array.
Examples
--------
Sort names: first by surname, then by name.
对名称进行排序:首先按姓氏,然后按名称。(就是如果姓氏相同,就按名称排)
>>> surnames = ('Hertz', 'Galilei', 'Hertz')
>>> first_names = ('Heinrich', 'Galileo', 'Gustav')
>>> ind = np.lexsort((first_names, surnames))
>>> ind
array([1, 2, 0])
>>> [surnames[i] + ", " + first_names[i] for i in ind]
['Galilei, Galileo', 'Hertz, Gustav', 'Hertz, Heinrich']
Sort two columns of numbers:
对两列数字进行排序:
>>> a = [1,5,1,4,3,4,4] # First column
>>> b = [9,4,0,4,0,2,1] # Second column
>>> ind = np.lexsort((b,a)) # Sort by a, then by b
>>> ind
array([2, 0, 4, 6, 5, 3, 1])
>>> [(a[i],b[i]) for i in ind]
[(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)]
Note that sorting is first according to the elements of ``a``.
Secondary sorting is according to the elements of ``b``.
请注意,排序首先是根据``a''的元素进行的。
二级排序是根据``b''的元素进行的。
A normal ``argsort`` would have yielded:
正常的``argsort''会产生:
>>> [(a[i],b[i]) for i in np.argsort(a)]
[(1, 9), (1, 0), (3, 0), (4, 4), (4, 2), (4, 1), (5, 4)]
Structured arrays are sorted lexically by ``argsort``:
>>> x = np.array([(1,9), (5,4), (1,0), (4,4), (3,0), (4,2), (4,1)],
... dtype=np.dtype([('x', int), ('y', int)]))
>>> np.argsort(x) # or np.argsort(x, order=('x', 'y'))
array([2, 0, 4, 6, 5, 3, 1])
"""
if isinstance(keys, tuple):
return keys
else:
return (keys,)
示例:
# -*- coding: utf-8 -*-
"""
@File : plot.py
@Time : 2020/2/24 8:55
@Author : Dontla
@Email : sxana@qq.com
@Software: PyCharm
"""
import matplotlib.pyplot as plt
import numpy as np
# 如发现格式不对可用记事本或notepad批量替换
keyword = {'11:30.0': (50000, 13.96), '12:16.0': (54500, 13.20), '13:15.0': (47500, 12.48),
'14:22.0': (55450, 12.44), '14:35.0': (55430, 13.72), '17:03.0': (13990, 11.00),
'17:38.0': (9058, 11.60), '17:57.0': (5044, 12.46), '18:20.0': (1300, 13.80),
'18:25.0': (900, 13.90), '18:28.0': (700, 13.96), '18:40.0': (200, 13.34),
'18:42.0': (150, 13.10), '18:44.0': (100, 11.80), '18:44.2': (90, 11.34),
'18.44.4': (80, 11.38), '18:44.8': (70, 9.50), '18:45.0': (60, 9.20),
'18:46.0': (50, 11.9), '18:46.3': (40, 10.8), '18:46.6': (30, 9.20),
'18:49.0': (20, 9.70), '18:49.6': (15, 6.90), '18:50.3': (13, 4.70),
'18:50.9': (12, 3.80), '18:51.5': (11, 2.60), '18:52.2': (10, 1.70),
'18:52.9': (9, 1.00), '18:53.6': (8, 0.2), '18:54.3': (7, 0.06),
'18:55.0': (6, 0.02)}
data = []
for key in keyword:
data.append(keyword[key])
data = np.array(data)
# print(data)
# [[5.000e+04 1.396e+01]
# [5.450e+04 1.320e+01]
# [4.750e+04 1.248e+01]
# [5.545e+04 1.244e+01]
# [5.543e+04 1.372e+01]
# [1.399e+04 1.100e+01]
# [9.058e+03 1.160e+01]
# [5.044e+03 1.246e+01]
# [1.300e+03 1.380e+01]
# [9.000e+02 1.390e+01]
# [7.000e+02 1.396e+01]
# [2.000e+02 1.334e+01]
# [1.500e+02 1.310e+01]
# [1.000e+02 1.180e+01]
# [9.000e+01 1.134e+01]
# [8.000e+01 1.138e+01]
# [7.000e+01 9.500e+00]
# [6.000e+01 9.200e+00]
# [5.000e+01 1.190e+01]
# [4.000e+01 1.080e+01]
# [3.000e+01 9.200e+00]
# [2.000e+01 9.700e+00]
# [1.500e+01 6.900e+00]
# [1.300e+01 4.700e+00]
# [1.200e+01 3.800e+00]
# [1.100e+01 2.600e+00]
# [1.000e+01 1.700e+00]
# [9.000e+00 1.000e+00]
# [8.000e+00 2.000e-01]
# [7.000e+00 6.000e-02]
# [6.000e+00 2.000e-02]]
x = data[:, 0]
# print(x)
# [5.000e+04 5.450e+04 4.750e+04 5.545e+04 5.543e+04 1.399e+04 9.058e+03
# 5.044e+03 1.300e+03 9.000e+02 7.000e+02 2.000e+02 1.500e+02 1.000e+02
# 9.000e+01 8.000e+01 7.000e+01 6.000e+01 5.000e+01 4.000e+01 3.000e+01
# 2.000e+01 1.500e+01 1.300e+01 1.200e+01 1.100e+01 1.000e+01 9.000e+00
# 8.000e+00 7.000e+00 6.000e+00]
y = data[:, 1]
# print(y)
# [13.96 13.2 12.48 12.44 13.72 11. 11.6 12.46 13.8 13.9 13.96 13.34
# 13.1 11.8 11.34 11.38 9.5 9.2 11.9 10.8 9.2 9.7 6.9 4.7
# 3.8 2.6 1.7 1. 0.2 0.06 0.02]
ind = np.lexsort((x,))
# print(ind)
# [30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
# 6 5 2 0 1 4 3]
data_sort = [(x[i], y[i]) for i in ind]
# print(data_sort)
# [(6.0, 0.02), (7.0, 0.06), (8.0, 0.2), (9.0, 1.0), (10.0, 1.7), (11.0, 2.6), (12.0, 3.8), (13.0, 4.7), (15.0, 6.9), (20.0, 9.7), (30.0, 9.2), (40.0, 10.8), (50.0, 11.9), (60.0, 9.2), (70.0, 9.5), (80.0, 11.38), (90.0, 11.34), (100.0, 11.8), (150.0, 13.1), (200.0, 13.34), (700.0, 13.96), (900.0, 13.9), (1300.0, 13.8), (5044.0, 12.46), (9058.0, 11.6), (13990.0, 11.0), (47500.0, 12.48), (50000.0, 13.96), (54500.0, 13.2), (55430.0, 13.72), (55450.0, 12.44)]
x_sort, y_sort = np.array(data_sort)[:, 0], np.array(data_sort)[:, 1]
# 用3次多项式拟合 可以改为5 次多项式。。。。 返回三次多项式系数
z1 = np.polyfit(x_sort, y_sort, 5)
p1 = np.poly1d(z1)
# 在屏幕上打印拟合多项式
print(p1)
# 3 2
# 2.534e-13 x - 2.506e-08 x + 0.000714 x + 7.821
# 设置绘制间隔
x_lin = np.arange(0, 60000, 5)
yvals = p1(x_lin) # 也可以使用yvals=np.polyval(z1,x)
plot1 = plt.plot(x_sort, y_sort, '*', label='original values')
plot2 = plt.plot(x_lin, yvals, 'r', label='polyfit values')
# 限制绘制上下限
plt.ylim(0, 16)
plt.xlabel('Illumination/lm')
plt.ylabel('Detect num/pcs')
plt.legend(loc=4) # 指定legend的位置,读者可以自己help它的用法
plt.title('polyfitting')
plt.show()
plt.savefig('p1.png')