import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')
plt.show()
a = [1, 2, 3, 4]
a
[1, 2, 3, 4]
b = [5, 6, 7, 8]
b
[5, 6, 7, 8]
a + b
[1, 2, 3, 4, 5, 6, 7, 8]
a = np.array([1, 2, 3, 4])
a
array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
b
array([5, 6, 7, 8])
a + b
array([ 6, 8, 10, 12])
A = np.array([1, 2, 3, 4])
B = np.array([5, 6, 7, 8])
C = A + B
C
array([ 6, 8, 10, 12])
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
for i in range(4):
print a[i] + b[i]
6 8 10 12
c = []
for i in range(4):
c[i] = a[i] + b[i]
c
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-21-301189359ad0> in <module>() 1 c = [] 2 for i in range(4): ----> 3 c[i] = a[i] + b[i] 4 c IndexError: list assignment index out of range
c = [0] * 4
for i in range(4):
c[i] = a[i] + b[i]
c
[6, 8, 10, 12]
c = [0] * 4
c
[0, 0, 0, 0]
c = [0] * 4; c
[0, 0, 0, 0]
c = [0] * 4
for i in range(4):
c[i] = a[i] + b[i]
c
[6, 8, 10, 12]
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
people
('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos
array([0, 1, 2, 3, 4])
len(people)
5
np.arange(5)
array([0, 1, 2, 3, 4])
np.arange([5:10])
File "<ipython-input-33-174ad357d3e3>", line 1 np.arange([5:10]) ^ SyntaxError: invalid syntax
np.arange(5, 10)
array([5, 6, 7, 8, 9])
performance = 3 + 10 * np.random.rand(len(people))
performance
array([ 12.14960218, 7.34383151, 8.40929254, 7.76009947, 6.74908334])
np.random.rand(5)
array([ 0.06353612, 0.34027728, 0.38437605, 0.02881175, 0.76307418])
error = np.random.rand(len(people))
error
array([ 0.64232379, 0.16818741, 0.46068865, 0.24806729, 0.50396354])
plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')
plt.show()
plt.barh(y_pos, performance, xerr=error, align='edge', alpha=1.0)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')
plt.show()
plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, people)
plt.show()
([<matplotlib.axis.YTick at 0x10fa91f50>, <matplotlib.axis.YTick at 0x10b20e610>, <matplotlib.axis.YTick at 0x10b25a490>, <matplotlib.axis.YTick at 0x10f828050>, <matplotlib.axis.YTick at 0x10f828790>], <a list of 5 Text yticklabel objects>)