# Matplotlib Introduction

In [1]:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

In [2]:
def listAttr(name, s = None):
if not s:
return [item for item in dir(name) if not item.startswith("_")]
return [item for item in dir(name) if (not item.startswith("_")) and s.lower() in item.lower()]
pass

In [5]:
plt.plot([1, 2, 3, 4])
plt.show()

In [6]:
plt.plot([1, 2, 3, 4], [1, 2, 3, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()

In [7]:
plt.plot([1, 2, 3, 4], [2, 5, 6, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()

In [8]:
plt.plot([1, 2, 3, 4], [2, 5, 6, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.axis([0, 5, 0, 10])
plt.show()

In [9]:
x = np.arange(1, 10)
y = x ** 2
y1 = 100 / x
y2 = x ** 2 - x

In [10]:
print(x)
print()
print(y)
print()
print(y1)
print()
print(y2)

[1 2 3 4 5 6 7 8 9]

[ 1  4  9 16 25 36 49 64 81]

[100.          50.          33.33333333  25.          20.
16.66666667  14.28571429  12.5         11.11111111]

[ 0  2  6 12 20 30 42 56 72]

In [11]:
plt.xlabel('Intergers')
plt.ylabel('Square')

plt.plot(x, y, y1) #(x, y) and (x,y1) graph

Out[11]:
[<matplotlib.lines.Line2D at 0x206901734c0>,
<matplotlib.lines.Line2D at 0x206901734f0>]
In [12]:
plt.xlabel('Intergers')
plt.ylabel('Square')

#plt.plot(x, y, y1) #(x, y) and (x,y1) graph
plt.plot(x, y)
plt.plot(x, y1)

Out[12]:
[<matplotlib.lines.Line2D at 0x20690093a00>]
In [13]:
plt.xlabel('Intergers')
plt.ylabel('Square')
plt.axis([0, 10, 0, 100])   #[xmin, xmax, ymin, ymax]
#plt.plot(x, y, x, y1, x, y2) #(x, y), (x,y1) and (x,y2) graph
plt.plot(x,y)
plt.plot(x,y1)
plt.plot(x,y2)

Out[13]:
[<matplotlib.lines.Line2D at 0x20690198e20>]
In [14]:
# r = red, o = circle circle, default is line
plt.plot(x, y, y1, 'or')  #(x,y) as red circle, (x,y1) as default

plt.axis([0, 10, 0, 100])   #[xmin, xmax, ymin, ymax]
plt.show()

In [15]:
plt.plot(x, y, 'r--', x, y1, 'bs', x, y2, 'g^')
plt.show()

In [16]:
plt.plot(x, y, 'r--')
plt.plot(x, y1, 'bs')
plt.plot(x, y2, 'g^')

plt.show()

In [17]:
#help(np.random.randint)

In [18]:
np.random.randint(0,5, 2)

Out[18]:
array([1, 1])
In [19]:
d1 = np.random.randn(50)
d1

Out[19]:
array([ 1.81898063, -0.74472489,  1.49295169,  0.53879865, -0.44599762,
0.49226756,  0.71687443,  1.82365746, -1.35501726,  1.12124227,
-0.06376229, -0.40779704,  0.86315364,  0.31096532,  0.98146923,
0.65701825,  1.61464174,  0.74602657,  0.68352237,  1.09271096,
-1.0232735 ,  0.94890993, -1.53755214,  1.67597315, -1.79698505,
1.40544541,  0.22324146, -0.04184051, -0.22382358,  0.8002226 ,
2.21754966,  1.29937459, -1.48176995,  1.86598122, -0.88121176,
-1.24588669,  0.41638352, -0.15394274,  0.64948889,  0.52619825,
0.83974743,  1.01075483, -0.60018436, -0.60352573,  0.86751679,
0.93899918,  0.87974191, -1.20421202,  0.27316221, -0.21233758])
In [20]:
np.abs(d1)

Out[20]:
array([1.81898063, 0.74472489, 1.49295169, 0.53879865, 0.44599762,
0.49226756, 0.71687443, 1.82365746, 1.35501726, 1.12124227,
0.06376229, 0.40779704, 0.86315364, 0.31096532, 0.98146923,
0.65701825, 1.61464174, 0.74602657, 0.68352237, 1.09271096,
1.0232735 , 0.94890993, 1.53755214, 1.67597315, 1.79698505,
1.40544541, 0.22324146, 0.04184051, 0.22382358, 0.8002226 ,
2.21754966, 1.29937459, 1.48176995, 1.86598122, 0.88121176,
1.24588669, 0.41638352, 0.15394274, 0.64948889, 0.52619825,
0.83974743, 1.01075483, 0.60018436, 0.60352573, 0.86751679,
0.93899918, 0.87974191, 1.20421202, 0.27316221, 0.21233758])
In [21]:
data = {
'a' : np.arange(50),
'b': np.random.randint(0,255, 50),
'd': np.random.randn(50)
}

In [22]:
data = {'a': np.arange(50),
'c': np.random.randint(0,255, 50),
'd': np.random.randn(50)
}

data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100

In [23]:
print(data["a"])

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49]

In [24]:
print(data["b"])

[  5.0381995   16.12331429  10.22035087  20.61103759  24.21563138
-11.39771095  11.65469987  -6.97734172  20.60668062   8.04907784
22.20728183   6.91255925  -0.45851185   7.76084867   9.00631117
11.85918313  20.18479204  22.05404306  12.50783726  32.89408392
22.83445947  27.437583    28.72682262  27.17385747  25.73618003
36.6388622   27.18844812  22.41543398  40.92286254  37.78329925
10.21271707  25.40890949  38.56943846  28.17200639  33.25096886
35.8176536   44.02327251  54.48458344  38.38526427  39.50849237
27.80548714  46.16328659  29.38046411  59.42318646  45.22534646
23.51923327  45.02855038  53.96343921  53.6172363   45.36008651]

In [25]:
print(data["c"])

[181  39  18  26 221 131  34 205 207 251 204 190   7  52  78 223  17  32
40  36  39  56 147 151 125  22  55 216 102 101 121 202 102  13   6  28
201 119  76 174 121  95 180  88  89  57  33 227 179 111]

In [26]:
print(data["d"])

[195.05384567  15.26817025  16.20358355  15.92535387 146.73131159
18.5762996   63.52986466  17.35953173 108.05984141  93.37375564
8.48042424  24.32407367 143.74142372  36.7029635   90.73257635
8.02493845  23.95047948  93.79025273 120.70237459 295.40136803
42.32184718  29.93927319 150.47913777   4.74326236  10.86736625
17.18270282 145.63554755 254.41997292 124.55049145  76.62527029
53.86034232  70.4611092   27.16619063 227.53430726  74.12215318
74.63374437  21.31267526  58.73894765  65.95718285  25.76850192
57.54143575 146.44957322 100.85579605 193.01546569 125.75528531
40.94300861  34.72397453  91.51917458 177.82541573 120.05448862]

In [27]:
#print(data["a"])
print(data.keys())
print(data.values())

dict_keys(['a', 'c', 'd', 'b'])
dict_values([array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]), array([181,  39,  18,  26, 221, 131,  34, 205, 207, 251, 204, 190,   7,
52,  78, 223,  17,  32,  40,  36,  39,  56, 147, 151, 125,  22,
55, 216, 102, 101, 121, 202, 102,  13,   6,  28, 201, 119,  76,
174, 121,  95, 180,  88,  89,  57,  33, 227, 179, 111]), array([195.05384567,  15.26817025,  16.20358355,  15.92535387,
146.73131159,  18.5762996 ,  63.52986466,  17.35953173,
108.05984141,  93.37375564,   8.48042424,  24.32407367,
143.74142372,  36.7029635 ,  90.73257635,   8.02493845,
23.95047948,  93.79025273, 120.70237459, 295.40136803,
42.32184718,  29.93927319, 150.47913777,   4.74326236,
10.86736625,  17.18270282, 145.63554755, 254.41997292,
124.55049145,  76.62527029,  53.86034232,  70.4611092 ,
27.16619063, 227.53430726,  74.12215318,  74.63374437,
21.31267526,  58.73894765,  65.95718285,  25.76850192,
57.54143575, 146.44957322, 100.85579605, 193.01546569,
125.75528531,  40.94300861,  34.72397453,  91.51917458,
177.82541573, 120.05448862]), array([  5.0381995 ,  16.12331429,  10.22035087,  20.61103759,
24.21563138, -11.39771095,  11.65469987,  -6.97734172,
20.60668062,   8.04907784,  22.20728183,   6.91255925,
-0.45851185,   7.76084867,   9.00631117,  11.85918313,
20.18479204,  22.05404306,  12.50783726,  32.89408392,
22.83445947,  27.437583  ,  28.72682262,  27.17385747,
25.73618003,  36.6388622 ,  27.18844812,  22.41543398,
40.92286254,  37.78329925,  10.21271707,  25.40890949,
38.56943846,  28.17200639,  33.25096886,  35.8176536 ,
44.02327251,  54.48458344,  38.38526427,  39.50849237,
27.80548714,  46.16328659,  29.38046411,  59.42318646,
45.22534646,  23.51923327,  45.02855038,  53.96343921,
53.6172363 ,  45.36008651])])

In [28]:
plt.xlabel('Intergers')
plt.ylabel('Square')
plt.plot(data['a'], data['b'], 'bo')

Out[28]:
[<matplotlib.lines.Line2D at 0x206903144f0>]
In [29]:
plt.xlabel('Intergers')
plt.ylabel('Square')
plt.plot(data['b'], data['c'],'r--', data['a'],data['c'], 'bs')

Out[29]:
[<matplotlib.lines.Line2D at 0x20690377c40>,
<matplotlib.lines.Line2D at 0x20690377d60>]
In [30]:
#help(plt.plot)

In [31]:
plt.plot('a','b','c' , data=data) # x= data['a'], y =  data['b']
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

In [32]:
print(data['b'])

[  5.0381995   16.12331429  10.22035087  20.61103759  24.21563138
-11.39771095  11.65469987  -6.97734172  20.60668062   8.04907784
22.20728183   6.91255925  -0.45851185   7.76084867   9.00631117
11.85918313  20.18479204  22.05404306  12.50783726  32.89408392
22.83445947  27.437583    28.72682262  27.17385747  25.73618003
36.6388622   27.18844812  22.41543398  40.92286254  37.78329925
10.21271707  25.40890949  38.56943846  28.17200639  33.25096886
35.8176536   44.02327251  54.48458344  38.38526427  39.50849237
27.80548714  46.16328659  29.38046411  59.42318646  45.22534646
23.51923327  45.02855038  53.96343921  53.6172363   45.36008651]

In [33]:
print(data['a'])

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49]

In [34]:
plt.plot('a','b','c', data=data) # x= data['a'], y =  data['b']
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

In [35]:
#listAttr(plt)

In [36]:
#help(plt.scatter)

In [37]:
plt.scatter?

In [38]:
plt.scatter('a','b', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

In [39]:
plt.scatter('a','b', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

In [40]:
print(data['c'])

[181  39  18  26 221 131  34 205 207 251 204 190   7  52  78 223  17  32
40  36  39  56 147 151 125  22  55 216 102 101 121 202 102  13   6  28
201 119  76 174 121  95 180  88  89  57  33 227 179 111]

In [41]:
plt.scatter('a', 'b', c='c', data=data)  # x= data['a'], y =  data['b']
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

In [42]:
print(data['d'])

[195.05384567  15.26817025  16.20358355  15.92535387 146.73131159
18.5762996   63.52986466  17.35953173 108.05984141  93.37375564
8.48042424  24.32407367 143.74142372  36.7029635   90.73257635
8.02493845  23.95047948  93.79025273 120.70237459 295.40136803
42.32184718  29.93927319 150.47913777   4.74326236  10.86736625
17.18270282 145.63554755 254.41997292 124.55049145  76.62527029
53.86034232  70.4611092   27.16619063 227.53430726  74.12215318
74.63374437  21.31267526  58.73894765  65.95718285  25.76850192
57.54143575 146.44957322 100.85579605 193.01546569 125.75528531
40.94300861  34.72397453  91.51917458 177.82541573 120.05448862]

In [43]:
plt.scatter('a', 'b', c='c', s='d', data=data)  # x= data['a'], y =  data['b']
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()