Skip to content

Commit 69f574a

Browse files
Add files via upload
1 parent 7b4fd5a commit 69f574a

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

Python_4_Plot_Charts.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Nov 27 08:09:11 2020
4+
5+
@author: Tin
6+
"""
7+
# Plot Line Chart
8+
import pandas as pd # Dataframe Library
9+
import matplotlib.pyplot as plt # Plot Chart Library
10+
11+
pd.set_option('max_columns', None) # To show all columns
12+
13+
import yfinance as yf
14+
yf.pdr_override()
15+
16+
17+
# input
18+
symbol = 'AAPL'
19+
start = '2014-01-01'
20+
end = '2018-01-01'
21+
22+
23+
# dataframe
24+
data = yf.download(symbol,start,end)
25+
26+
# View the first 5 rows
27+
print('First 5 Rows')
28+
print(data.head())
29+
print('-'*80)
30+
31+
print('Line Chart')
32+
plt.figure(figsize=(12,8))
33+
plt.plot(data['Adj Close'])
34+
plt.title("Stock Line Chart")
35+
plt.legend(loc='best')
36+
plt.xlabel("Date")
37+
plt.ylabel("Price")
38+
plt.show()
39+
print('-'*80)
40+
41+
42+
print('Line Chart with Grid')
43+
plt.figure(figsize=(12,8))
44+
plt.plot(data['Adj Close'])
45+
plt.title("Stock Line Chart")
46+
plt.grid()
47+
plt.legend(loc='best')
48+
plt.xlabel("Date")
49+
plt.ylabel("Price")
50+
plt.show()
51+
print('-'*80)
52+
53+
54+
print('Render the grid')
55+
fig, ax = plt.subplots()
56+
data.plot(kind='line', y= 'Adj Close', ax=ax)
57+
# Turn on the grid
58+
ax.grid()
59+
plt.title("Stock Line Chart")
60+
plt.legend(loc='best')
61+
plt.xlabel("Date")
62+
plt.ylabel("Price")
63+
plt.show()
64+
print('-'*80)
65+
66+
67+
print('Customize the grid')
68+
fig, ax = plt.subplots()
69+
data.plot(kind='line', y= 'Adj Close', ax=ax)
70+
# Don't allow the axis to be on top of your data
71+
ax.set_axisbelow(True)
72+
# Customize the grid
73+
ax.grid(linestyle='-', linewidth='0.5', color='red')
74+
plt.title("Stock Line Chart")
75+
plt.xlabel("Date")
76+
plt.ylabel("Price")
77+
plt.show()
78+
print('-'*80)
79+
80+
81+
print('Major grid & Minor Grid')
82+
plt.figure(figsize=(12,8))
83+
plt.plot(data['Adj Close'])
84+
plt.minorticks_on()
85+
plt.grid(b=True, which='major', color='b', linestyle='-')
86+
plt.grid(b=True, which='minor', color='r', linestyle='--')
87+
plt.title("Stock Line Chart")
88+
plt.xlabel("Date")
89+
plt.ylabel("Price")
90+
plt.show()
91+
print('-'*80)
92+
93+
94+
import seaborn as sns # Plot Library 0.9.0 Version
95+
# conda install -c anaconda seaborn=0.9.0
96+
plt.figure(figsize=(10,5))
97+
sns.lineplot(data=data, x=data.index, y='Adj Close')
98+
print('-'*80)
99+
100+
plt.figure(figsize=(10,5))
101+
top = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4)
102+
bottom = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)
103+
top.plot(data.index, data['Adj Close'])
104+
bottom.bar(data.index, data['Volume'])
105+
106+
# set the labels
107+
top.axes.get_xaxis().set_visible(False)
108+
top.set_title('Stock Price and Volume')
109+
top.set_ylabel('Adj Closing Price')
110+
bottom.set_ylabel('Volume')
111+
print('-'*80)
112+
113+
114+
# Candlestick
115+
from mpl_finance import candlestick_ohlc
116+
from matplotlib import dates as mdates
117+
118+
# Converting date to pandas datetime format
119+
dfc = data.copy()
120+
dfc = dfc.reset_index()
121+
dfc['Date'] = pd.to_datetime(dfc['Date'])
122+
dfc['Date'] = dfc['Date'].apply(mdates.date2num)
123+
# dfc.head()
124+
125+
fig = plt.figure(figsize=(14,10))
126+
ax1 = plt.subplot(2, 1, 1)
127+
candlestick_ohlc(ax1,dfc.values, width=0.5, colorup='g', colordown='r', alpha=1.0)
128+
ax1.xaxis_date()
129+
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
130+
ax1.set_title('Stock '+ symbol +' Closing Price')
131+
ax1.set_ylabel('Price')
132+
133+
134+
135+
136+
import plotly.graph_objs as go
137+
# from plotly.offline import init_notebook_mode, iplot
138+
139+
df = data.copy()
140+
# Plot OHLC Bar Chart
141+
trace = go.Ohlc(x=df['12-2016'].index,
142+
open=df['12-2016'].Open,
143+
high=df['12-2016'].High,
144+
low=df['12-2016'].Low,
145+
close=df['12-2016'].Close)
146+
data = [trace]
147+
iplot(data, filename='simple_ohlc')
148+
149+

0 commit comments

Comments
 (0)