【发布时间】:2015-05-31 15:30:51
【问题描述】:
我有一个二维数组absolute_heights,形状为(2, 6)。我想定义一个形状为(2, 6) 的新二维数组bottoms,它在每个位置0 处保存i,除非
1) absolute_heights[0, i] - absolute_heights[1, i] 的符号与absolute_heights[0, i] 的符号匹配,此时bottoms[0, i] 应设置为absolute_heights[1, i]。
2) #1 为假,此时bottoms[1, i] 应设置为absolute_heights[0, i]。
下面是实现此目的的for 循环:
def _get_bottoms(absolute_heights):
"""Define the bottom of each bar in a stacked bar graph.
Parameters
----------
absolute_heights : np.array
The absolute height of each bar. Stacking of the bars is along
the first axis of this array.
Returns
-------
bottoms : np.array
The absolute height of the bar in each stack that is closest to
zero.
"""
bottoms = np.zeros((2, 6))
for i, diff in enumerate(absolute_heights[0, :] - absolute_heights[1, :]):
if np.sign(diff) == np.sign(absolute_heights[0, i]):
bottoms[0, i] = absolute_heights[1, i]
else:
bottoms[1, i] = absolute_heights[0, i]
return bottoms
numpy 中是否有更有效的方法?
【问题讨论】:
标签: python arrays numpy matplotlib difference