Python: Connecting list values with array values
<p>I have created a tornado plot taking inspiration from <a href="http://stackoverflow.com/questions/32132773/a-tornado-chart-and-p10-p90-in-python-matplotlib">here</a>. It has input variables labelled on the y-axis (a1,b1,c1...) and their respective correlation coefficients plotted next to them. See pic below:</p>

<p><a href="https://i.stack.imgur.com/4NE3f.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/4NE3f.jpg" alt="enter image description here"></a></p>

<p>I then sorted the correlation coefficients in a way that the highest absolute value without loosing its sign gets plotted first, then the next highest and so on. using <code>sorted(values,key=abs, reverse=True)</code>. See the result below</p>

<p><a href="https://i.stack.imgur.com/xamYL.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/xamYL.jpg" alt="enter image description here"></a></p>

<p>If you notice, in the second pic even though the bars were sorted in the absolute descending order, the y-axis label still stay the same.</p>

<p>Question: How do I make the y-axis label(variable) connect to the correlation coefficient such that it always corresponds to its correlation coefficient.</p>

<p>Below is my code:</p>

<pre><code>import numpy as np
from matplotlib import pyplot as plt

#####Importing Data from csv file#####
dataset1 = np.genfromtxt('dataSet1.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])
dataset2 = np.genfromtxt('dataSet2.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])
dataset3 = np.genfromtxt('dataSet3.csv', dtype = float, delimiter = ',', skip_header = 1, names = ['a', 'b', 'c', 'x0'])

corr1 = np.corrcoef(dataset1['a'],dataset1['x0'])
corr2 = np.corrcoef(dataset1['b'],dataset1['x0'])
corr3 = np.corrcoef(dataset1['c'],dataset1['x0'])
corr4 = np.corrcoef(dataset2['a'],dataset2['x0'])
corr5 = np.corrcoef(dataset2['b'],dataset2['x0'])
corr6 = np.corrcoef(dataset2['c'],dataset2['x0'])
corr7 = np.corrcoef(dataset3['a'],dataset3['x0'])
corr8 = np.corrcoef(dataset3['b'],dataset3['x0'])
corr9 = np.corrcoef(dataset3['c'],dataset3['x0'])

np.set_printoptions(precision=4)
variables = ['a1','b1','c1','a2','b2','c2','a3','b3','c3']
base = 0  
values = np.array([corr1[0,1],corr2[0,1],corr3[0,1],
                   corr4[0,1],corr5[0,1],corr6[0,1],
                   corr7[0,1],corr8[0,1],corr9[0,1]])
values = sorted(values,key=abs, reverse=True)
# The y position for each variable
ys = range(len(values))[::-1]  # top to bottom   
# Plot the bars, one by one
for y, value in zip(ys, values):
    high_width = base + value
    #print high_width

    # Each bar is a "broken" horizontal bar chart
    plt.broken_barh(
        [(base, high_width)],
        (y - 0.4, 0.8),
        facecolors=['red', 'red'],  # Try different colors if you like
        edgecolors=['black', 'black'],
        linewidth=1)
# Draw a vertical line down the middle
plt.axvline(base, color='black')
# Position the x-axis on the top/bottom, hide all the other spines (=axis lines)
axes = plt.gca()  # (gca = get current axes)
axes.spines['left'].set_visible(False)
axes.spines['right'].set_visible(False)
axes.spines['top'].set_visible(False)
axes.xaxis.set_ticks_position('bottom')
# Make the y-axis display the variables
plt.yticks(ys, variables)

plt.ylim(-2, len(variables))
plt.show()
</code></pre>

<p>Many thanks in advance</p>

<p>use build-in zip function - returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. But aware the returned list is truncated in length to the length of the shortest argument sequence.</p>

python python-2.7 matplotlib