Skip to content

Commit 36a038f

Browse files
merge master into enh-retfig
2 parents 6436539 + 3b73173 commit 36a038f

16 files changed

+523
-35
lines changed

examples/original_flavor/date_demo1.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
from pandas.plotting import register_matplotlib_converters
1919
register_matplotlib_converters()
2020
from matplotlib.dates import DateFormatter, MonthLocator, YearLocator
21+
import os.path
2122

2223
years = YearLocator() # every year
2324
months = MonthLocator() # every month
2425
yearsFmt = DateFormatter('%Y')
2526

26-
quotes = pd.read_csv('data/yahoofinance-INTC-19950101-20040412.csv',
27-
index_col=0,
28-
parse_dates=True,
29-
infer_datetime_format=True)
27+
# make file paths OS independent
28+
infile = os.path.join('data','yahoofinance-INTC-19950101-20040412.csv')
29+
quotes = pd.read_csv(infile,index_col=0,parse_dates=True,infer_datetime_format=True)
3030

3131
dates = quotes.index
3232
opens = quotes['Open']

examples/original_flavor/date_demo2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
register_matplotlib_converters()
1111
from matplotlib.dates import (MONDAY, DateFormatter, MonthLocator,
1212
WeekdayLocator)
13+
import os.path
1314

1415
date1 = "2002-1-5"
1516
date2 = "2003-12-1"
@@ -22,7 +23,8 @@
2223
monthsFmt = DateFormatter("%b '%y")
2324

2425

25-
quotes = pd.read_csv('data/yahoofinance-INTC-19950101-20040412.csv',
26+
infile = os.path.join('data','yahoofinance-INTC-19950101-20040412.csv')
27+
quotes = pd.read_csv(infile,
2628
index_col=0,
2729
parse_dates=True,
2830
infer_datetime_format=True)

examples/original_flavor/finance_demo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pandas.plotting import register_matplotlib_converters
77
register_matplotlib_converters()
88
from matplotlib.dates import MONDAY, DateFormatter, DayLocator, WeekdayLocator
9+
import os.path
910

1011
from mplfinance.original_flavor import candlestick_ohlc
1112

@@ -18,7 +19,8 @@
1819
weekFormatter = DateFormatter('%b %d') # e.g., Jan 12
1920
dayFormatter = DateFormatter('%d') # e.g., 12
2021

21-
quotes = pd.read_csv('data/yahoofinance-INTC-19950101-20040412.csv',
22+
infile = os.path.join('data','yahoofinance-INTC-19950101-20040412.csv')
23+
quotes = pd.read_csv(infile,
2224
index_col=0,
2325
parse_dates=True,
2426
infer_datetime_format=True)

examples/original_flavor/finance_work2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import pandas as pd
77
from pandas.plotting import register_matplotlib_converters
88
register_matplotlib_converters()
9+
import os.path
910

1011
ticker = 'SPY'
11-
r = pd.read_csv('data/yahoofinance-SPY-20080101-20180101.csv',
12+
infile = os.path.join('data','yahoofinance-SPY-20080101-20180101.csv')
13+
r = pd.read_csv(infile,
1214
index_col=0,
1315
parse_dates=True,
1416
infer_datetime_format=True)

examples/original_flavor/longshort.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
from pandas.plotting import register_matplotlib_converters
1111
register_matplotlib_converters()
1212

13+
import os.path
1314
from six.moves import urllib
1415

1516
start_date = "2004-8-19"
1617
end_date = '2018-1-20'
1718

1819
# grab the price data off yahoo csv quotes
19-
r1 = pd.read_csv('data/yahoofinance-AAPL-20040819-20180120.csv',
20+
21+
infile1 = os.path.join('data','yahoofinance-AAPL-20040819-20180120.csv')
22+
r1 = pd.read_csv(infile1,
2023
index_col=0,
2124
parse_dates=True,
2225
infer_datetime_format=True)
23-
r2 = pd.read_csv('data/yahoofinance-GOOG-20040819-20180120.csv',
26+
27+
infile2 = os.path.join('data','yahoofinance-GOOG-20040819-20180120.csv')
28+
r2 = pd.read_csv(infile2,
2429
index_col=0,
2530
parse_dates=True,
2631
infer_datetime_format=True)

examples/original_flavor/plot_day_summary_oclh_demo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
register_matplotlib_converters()
88
from matplotlib.dates import (MONDAY, DateFormatter, MonthLocator,
99
WeekdayLocator, date2num)
10+
import os.path
1011

1112
from mplfinance.original_flavor import plot_day_summary_oclh
1213

@@ -18,7 +19,8 @@
1819
daysFmt = DateFormatter("%d %b %y")
1920

2021

21-
quotes = pd.read_csv('data/yahoofinance-INTC-19950101-20040412.csv',
22+
infile = os.path.join('data','yahoofinance-INTC-19950101-20040412.csv')
23+
quotes = pd.read_csv(infile,
2224
index_col=0,
2325
parse_dates=True,
2426
infer_datetime_format=True)

src/mplfinance/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version_info = (0, 12, 3, 'alpha', 3)
1+
version_info = (0, 12, 3, 'alpha', 5)
22

33
_specifier_ = {'alpha': 'a', 'beta': 'b', 'candidate': 'rc', 'final': ''}
44

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import pytest
2-
import os as os
3-
import pandas as pd
2+
import os
3+
import pandas as pd
4+
import os.path
45

56
os.environ['MPLBACKEND'] = 'agg'
67

78
print('\npd.__version__ =',pd.__version__ ) # for the record
89

9-
df = pd.read_csv('examples/data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
10+
infile = os.path.join('examples','data','SPY_20110701_20120630_Bollinger.csv')
11+
df = pd.read_csv(infile,index_col=0,parse_dates=True)
1012
print('df.shape=' , df.shape )
1113
print('df.head(3)=', df.head(3))
1214
print('df.tail(3)=', df.tail(3))
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Show how to make date plots in matplotlib using date tick locators and
3+
formatters. See major_minor_demo1.py for more information on
4+
controlling major and minor ticks
5+
6+
All matplotlib date plotting is done by converting date instances into
7+
days since the 0001-01-01 UTC. The conversion, tick locating and
8+
formatting is done behind the scenes so this is most transparent to
9+
you. The dates module provides several converter functions date2num
10+
and num2date
11+
12+
This example requires an active internet connection since it uses
13+
yahoo finance to get the data for plotting
14+
"""
15+
16+
import matplotlib.pyplot as plt
17+
import pandas as pd
18+
from pandas.plotting import register_matplotlib_converters
19+
register_matplotlib_converters()
20+
from matplotlib.dates import DateFormatter, MonthLocator, YearLocator
21+
import os.path
22+
import io
23+
24+
def test_date_demo1():
25+
26+
years = YearLocator() # every year
27+
months = MonthLocator() # every month
28+
yearsFmt = DateFormatter('%Y')
29+
30+
# make file paths OS independent
31+
infile = os.path.join('examples','data','yahoofinance-INTC-19950101-20040412.csv')
32+
quotes = pd.read_csv(infile,index_col=0,parse_dates=True,infer_datetime_format=True)
33+
34+
dates = quotes.index
35+
opens = quotes['Open']
36+
37+
fig, ax = plt.subplots()
38+
ax.plot_date(dates, opens, '-')
39+
40+
# format the ticks
41+
ax.xaxis.set_major_locator(years)
42+
ax.xaxis.set_major_formatter(yearsFmt)
43+
ax.xaxis.set_minor_locator(months)
44+
ax.autoscale_view()
45+
46+
47+
# format the coords message box
48+
def price(x):
49+
return '$%1.2f' % x
50+
51+
52+
ax.fmt_xdata = DateFormatter('%Y-%m-%d')
53+
ax.fmt_ydata = price
54+
ax.grid(True)
55+
56+
fig.autofmt_xdate()
57+
buf = io.BytesIO()
58+
plt.savefig(buf)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Show how to make date plots in matplotlib using date tick locators and
3+
formatters. See major_minor_demo1.py for more information on
4+
controlling major and minor ticks
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import pandas as pd
9+
from pandas.plotting import register_matplotlib_converters
10+
register_matplotlib_converters()
11+
from matplotlib.dates import (MONDAY, DateFormatter, MonthLocator,
12+
WeekdayLocator)
13+
import os.path
14+
import io
15+
16+
def test_date_demo2():
17+
18+
date1 = "2002-1-5"
19+
date2 = "2003-12-1"
20+
21+
# every monday
22+
mondays = WeekdayLocator(MONDAY)
23+
24+
# every 3rd month
25+
months = MonthLocator(range(1, 13), bymonthday=1, interval=3)
26+
monthsFmt = DateFormatter("%b '%y")
27+
28+
29+
infile = os.path.join('examples','data','yahoofinance-INTC-19950101-20040412.csv')
30+
quotes = pd.read_csv(infile,
31+
index_col=0,
32+
parse_dates=True,
33+
infer_datetime_format=True)
34+
35+
# select desired range of dates
36+
quotes = quotes[(quotes.index >= date1) & (quotes.index <= date2)]
37+
38+
dates = quotes.index
39+
opens = quotes['Open']
40+
41+
42+
fig, ax = plt.subplots()
43+
ax.plot_date(dates, opens, '-')
44+
ax.xaxis.set_major_locator(months)
45+
ax.xaxis.set_major_formatter(monthsFmt)
46+
ax.xaxis.set_minor_locator(mondays)
47+
ax.autoscale_view()
48+
# ax.xaxis.grid(False, 'major')
49+
# ax.xaxis.grid(True, 'minor')
50+
ax.grid(True)
51+
52+
fig.autofmt_xdate()
53+
54+
buf = io.BytesIO()
55+
plt.savefig(buf)

0 commit comments

Comments
 (0)