python - Define the bottom of each bar in a stacked bar graph -


i have two-dimensional array absolute_heights of shape (2, 6). i'd define new two-dimensional array bottoms of shape (2, 6) holds 0 @ each position i unless

1) sign of absolute_heights[0, i] - absolute_heights[1, i] matches of absolute_heights[0, i], in case bottoms[0, i] should set absolute_heights[1, i].

2) #1 false, in case bottoms[1, i] should set absolute_heights[0, i].

below for loop achieves this:

def _get_bottoms(absolute_heights):     """define bottom of each bar in stacked bar graph.      parameters     ----------     absolute_heights : np.array       absolute height of each bar.  stacking of bars along       first axis of array.      returns     -------     bottoms : np.array       absolute height of bar in each stack closest       zero.      """     bottoms = np.zeros((2, 6))     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 

is there more efficient way of doing in numpy?

you use boolean indexing avoid for loop:

def _get_bottoms(absolute_heights):     bottoms = np.zeros((2,6))     diff = absolute_heights[0, :] - absolute_heights[1, :]     = np.sign(diff) == np.sign(absolute_heights[0, :])     bottoms[0, i] = absolute_heights[1, i]     bottoms[1, ~i] = absolute_heights[0, ~i]     return bottoms 

in function i boolean array indicating whether signs match (essentially if statement). inverting boolean values ~i gives array else statement.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -