我想加入两个可以在X轴上不同的XY图。 这些图是在numpy ndarrys中保存的,我希望连接操作是最佳的,性能明智的(我知道如何使用关联数组来解决这个问题)。
联合作战定义为PlotAB = join_op(PlotA,PlotB)
:
PlotA =
array([[2, 5],
[3, 5],
[5, 5])
其中plotA[:,0]
是X轴& plotA[:,1]
是Y轴
PlotB =
array([[1, 7],
[2, 7],
[3, 7],
[4, 7]])
其中plotB[:,0]
是X轴& plotB[:,1]
是Y轴
联接的数组是:
PlotsAB =
array([[1, n, 7],
[2, 5, 7],
[3, 5, 7],
[4, n, 7],
[5, 5, n]])
,其中
PlotAB[:,0]
是联合X轴(排序uniuq)。
plotAB[:,1]
是PlotA的Y轴。
plotAB[:,2]
是PlotB的Y轴。
'n'
表示缺少值的地方 - 此图中不存在。
顺便说一句,我需要这个来为dygraphs绘图仪(http://dygraphs.com/gallery/#g/independent-series)
组成数据请指教, 感谢。
答案 0 :(得分:0)
这是一个解决方案,使用numpy.setdiff1d
查找每个输入数组中的唯一x元素,并numpy.argsort
在插入[x,NaN]元素后对输入数组进行排序进入他们。
import numpy as np
def join_op(a,b):
ax = a[:,0]
bx = b[:,0]
# elements unique to b
ba_x = np.setdiff1d(bx,ax)
# elements unique to a
ab_x = np.setdiff1d(ax,bx)
ba_y = np.NaN*np.empty(ba_x.shape)
ab_y = np.NaN*np.empty(ab_x.shape)
ba = np.array((ba_x,ba_y)).T
ab = np.array((ab_x,ab_y)).T
a_new = np.concatenate((a,ba))
b_new = np.concatenate((b,ab))
a_sort_idx = np.argsort(a_new[:,0])
b_sort_idx = np.argsort(b_new[:,0])
a_new_sorted = a_new[a_sort_idx]
b_new_sorted = b_new[b_sort_idx]
b_new_sorted_y = b_new_sorted[:,1].reshape(-1,1)
return np.concatenate((a_new_sorted,b_new_sorted_y),axis=1)
a = np.array([[2,5],[3,5],[5,5]])
b = np.array([[1,7],[2,7],[3,7],[4,7]])
c = combine(a,b)
输出:
[[ 1. nan 7.]
[ 2. 5. 7.]
[ 3. 5. 7.]
[ 4. nan 7.]
[ 5. 5. nan]]