|
1 | 1 | import matplotlib.pyplot as plt
|
2 |
| -import numpy as np |
3 |
| - |
4 |
| -import matplotlib.cbook as cbook |
5 |
| -from matplotlib.ticker import MultipleLocator |
6 |
| - |
7 | 2 | """
|
| 3 | +================ |
8 | 4 | Stock Demo Plots
|
| 5 | +================ |
9 | 6 |
|
10 | 7 | The following example displays Matplotlibs capabilities of creating
|
11 | 8 | graphs that can be used for stocks. The example specfically uses
|
12 | 9 | Apple and Intel stock data and graphs the normalized prices on the
|
13 |
| -same plot. |
14 |
| -
|
| 10 | +same plot. |
15 | 11 | """
|
16 | 12 |
|
| 13 | + |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +import matplotlib.cbook as cbook |
| 17 | +from matplotlib.ticker import MultipleLocator |
| 18 | + |
| 19 | + |
17 | 20 | def get_two_stock_data():
|
18 | 21 | """
|
19 | 22 | load stock time and price data for two stocks The return values
|
20 | 23 | (d1,p1,d2,p2) are the trade time (in days) and prices for stocks 1
|
21 | 24 | and 2 (intc and aapl)
|
22 | 25 | """
|
23 |
| - ticker1, ticker2 = 'INTC', 'AAPL' |
24 |
| - |
25 | 26 | file1 = cbook.get_sample_data('INTC.dat.gz')
|
26 | 27 | file2 = cbook.get_sample_data('AAPL.dat.gz')
|
27 |
| - M1 = fromstring(file1.read(), '<d') |
| 28 | + M1 = np.fromstring(file1.read(), '<d') |
28 | 29 |
|
29 |
| - M1 = resize(M1, (M1.shape[0]//2, 2)) |
| 30 | + M1 = np.resize(M1, (M1.shape[0]//2, 2)) |
30 | 31 |
|
31 |
| - M2 = fromstring(file2.read(), '<d') |
32 |
| - M2 = resize(M2, (M2.shape[0]//2, 2)) |
| 32 | + M2 = np.fromstring(file2.read(), '<d') |
| 33 | + M2 = np.resize(M2, (M2.shape[0]//2, 2)) |
33 | 34 |
|
34 | 35 | d1, p1 = M1[:, 0], M1[:, 1]
|
35 | 36 | d2, p2 = M2[:, 0], M2[:, 1]
|
36 | 37 | return (d1, p1, d2, p2)
|
37 | 38 |
|
| 39 | + |
38 | 40 | d1, p1, d2, p2 = get_two_stock_data()
|
39 | 41 |
|
40 | 42 | fig, ax = plt.subplots()
|
41 |
| -lines1 = plt.plot(d1, p1, 'b', label="INTC") |
42 |
| -lines2 = plt.plot(d2, p2, 'r', label="AAPL") |
43 |
| -plt.xlabel('Days') |
44 |
| -plt.ylabel('Normalized price') |
45 |
| -plt.xlim(0, 3) |
| 43 | +lines1 = ax.plot(d1, p1, 'b', label="INTC") |
| 44 | +lines2 = ax.plot(d2, p2, 'r', label="AAPL") |
| 45 | +ax.set_xlabel('Days') |
| 46 | +ax.set_ylabel('Normalized price') |
| 47 | +ax.set_xlim(0, 3) |
46 | 48 | ax.xaxis.set_major_locator(MultipleLocator(1))
|
47 | 49 |
|
48 |
| -plt.title('INTC vs AAPL') |
49 |
| -plt.legend() |
| 50 | +ax.set_title('INTC vs AAPL') |
| 51 | +ax.legend() |
50 | 52 |
|
51 | 53 | plt.show()
|
0 commit comments