From a2b21a89190169919ec96c120e8a647298043c8a Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 12:25:20 +0100 Subject: [PATCH 01/11] Run misc examples as part of docs build --- doc/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/conf.py b/doc/conf.py index 7eef3bf5d742..1f12b1f9f746 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -138,6 +138,7 @@ ('axes_grid', 'axes_grid toolkit'), ('units', 'units'), ('widgets', 'widgets'), + ('misc', 'Miscellaneous examples'), ] From 1535bb0998d3cdc6390d5082f58a5d88b23ede90 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 12:26:15 +0100 Subject: [PATCH 02/11] Don't try to run image_thumbnail example as part of docs build --- examples/misc/image_thumbnail.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/misc/image_thumbnail.py b/examples/misc/image_thumbnail.py index 4e3a1dafc032..89a87ecd563e 100644 --- a/examples/misc/image_thumbnail.py +++ b/examples/misc/image_thumbnail.py @@ -1,3 +1,4 @@ +# -*- noplot -*- """ You can use matplotlib to generate thumbnails from existing images. matplotlib natively supports PNG files on the input side, and other From 2df20fc3cef184f73c04278d8d09968e76ecaa45 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 12:26:49 +0100 Subject: [PATCH 03/11] Multiprocess example depends on gtk so don't run it during docss build --- examples/misc/multiprocess.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/misc/multiprocess.py b/examples/misc/multiprocess.py index 510e9001fa3e..1ac93d646355 100644 --- a/examples/misc/multiprocess.py +++ b/examples/misc/multiprocess.py @@ -1,3 +1,4 @@ +# -*- noplot -*- # Demo of using multiprocessing for generating data in one process and plotting # in another. # Written by Robert Cimrman From 0c42d2f7b2c22a15124aa6b6fee3107beec9941a Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 12:27:18 +0100 Subject: [PATCH 04/11] rc_traits example depends on traits so don't run it during docss build --- examples/misc/rc_traits.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/misc/rc_traits.py b/examples/misc/rc_traits.py index 45bdbe079af2..e5aa417f4cd3 100644 --- a/examples/misc/rc_traits.py +++ b/examples/misc/rc_traits.py @@ -1,3 +1,4 @@ +# -*- noplot -*- # Here is some example code showing how to define some representative # rc properties and construct a matplotlib artist using traits. # matplotlib does not ship with enthought.traits, so you will need to From b172ec61d702d201aeb14170aac840f0e13abb84 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 12:27:50 +0100 Subject: [PATCH 05/11] Remove out of datelongshort example --- examples/misc/longshort.py | 46 -------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 examples/misc/longshort.py diff --git a/examples/misc/longshort.py b/examples/misc/longshort.py deleted file mode 100644 index 56b121db2cfc..000000000000 --- a/examples/misc/longshort.py +++ /dev/null @@ -1,46 +0,0 @@ -""" -Illustrate the rec array utility funcitons by loading prices from a -csv file, computing the daily returns, appending the results to the -record arrays, joining on date -""" -import urllib -import numpy as np -import matplotlib.pyplot as plt -import matplotlib.mlab as mlab - -# grab the price data off yahoo -u1 = urllib.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') -u2 = urllib.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=GOOG&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') - -# load the CSV files into record arrays -r1 = mlab.csv2rec(file(u1[0])) -r2 = mlab.csv2rec(file(u2[0])) - -# compute the daily returns and add these columns to the arrays -gains1 = np.zeros_like(r1.adj_close) -gains2 = np.zeros_like(r2.adj_close) -gains1[1:] = np.diff(r1.adj_close)/r1.adj_close[:-1] -gains2[1:] = np.diff(r2.adj_close)/r2.adj_close[:-1] -r1 = mlab.rec_append_fields(r1, 'gains', gains1) -r2 = mlab.rec_append_fields(r2, 'gains', gains2) - -# now join them by date; the default postfixes are 1 and 2. The -# default jointype is inner so it will do an intersection of dates and -# drop the dates in AAPL which occurred before GOOG started trading in -# 2004. r1 and r2 are reverse ordered by date since Yahoo returns -# most recent first in the CSV files, but rec_join will sort by key so -# r below will be properly sorted -r = mlab.rec_join('date', r1, r2) - - -# long appl, short goog -g = r.gains1 - r.gains2 -tr = (1 + g).cumprod() # the total return - -# plot the return -fig, ax = plt.subplots() -ax.plot(r.date, tr) -ax.set_title('total return: long APPL, short GOOG') -ax.grid() -fig.autofmt_xdate() -plt.show() From 222ed1422a80648ba3ee752da16745382599f74d Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 13:56:19 +0100 Subject: [PATCH 06/11] np.dtype does not support unicode strings on python 2.7 so encode before creating --- lib/matplotlib/mlab.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 07ddd98cd22c..22820c2cc662 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2596,8 +2596,10 @@ def mapped_r2field(name): if desc[0] not in key] r2desc = [(mapped_r2field(desc[0]), desc[1]) for desc in r2.dtype.descr if desc[0] not in key] - newdtype = np.dtype(keydesc + r1desc + r2desc) - + all_dtypes = keydesc + r1desc + r2desc + if six.PY2: + all_dtypes = [(a[0].encode('utf-8'), a[1]) for a in all_dtypes] + newdtype = np.dtype(all_dtypes) newrec = np.recarray((common_len + left_len + right_len,), dtype=newdtype) if defaults is not None: From cbec32b78056b73d44cc551f609278d0130abcf2 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 13:57:28 +0100 Subject: [PATCH 07/11] interkeys should be called on the dict and not the keys --- lib/matplotlib/mlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 22820c2cc662..5a1425d5d091 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2615,7 +2615,7 @@ def mapped_r2field(name): if jointype != 'inner' and defaults is not None: # fill in the defaults enmasse - newrec_fields = list(six.iterkeys(newrec.dtype.fields.keys)) + newrec_fields = list(six.iterkeys(newrec.dtype.fields)) for k, v in six.iteritems(defaults): if k in newrec_fields: newrec[k] = v From 0ba12c61aa3d0cf0382475dea237b2269573c5ab Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 14:00:16 +0100 Subject: [PATCH 08/11] Revert "Remove out of datelongshort example" This reverts commit 0d347a404ae81cc747990c1ce6ac546446630b2d. --- examples/misc/longshort.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/misc/longshort.py diff --git a/examples/misc/longshort.py b/examples/misc/longshort.py new file mode 100644 index 000000000000..56b121db2cfc --- /dev/null +++ b/examples/misc/longshort.py @@ -0,0 +1,46 @@ +""" +Illustrate the rec array utility funcitons by loading prices from a +csv file, computing the daily returns, appending the results to the +record arrays, joining on date +""" +import urllib +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.mlab as mlab + +# grab the price data off yahoo +u1 = urllib.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') +u2 = urllib.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=GOOG&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') + +# load the CSV files into record arrays +r1 = mlab.csv2rec(file(u1[0])) +r2 = mlab.csv2rec(file(u2[0])) + +# compute the daily returns and add these columns to the arrays +gains1 = np.zeros_like(r1.adj_close) +gains2 = np.zeros_like(r2.adj_close) +gains1[1:] = np.diff(r1.adj_close)/r1.adj_close[:-1] +gains2[1:] = np.diff(r2.adj_close)/r2.adj_close[:-1] +r1 = mlab.rec_append_fields(r1, 'gains', gains1) +r2 = mlab.rec_append_fields(r2, 'gains', gains2) + +# now join them by date; the default postfixes are 1 and 2. The +# default jointype is inner so it will do an intersection of dates and +# drop the dates in AAPL which occurred before GOOG started trading in +# 2004. r1 and r2 are reverse ordered by date since Yahoo returns +# most recent first in the CSV files, but rec_join will sort by key so +# r below will be properly sorted +r = mlab.rec_join('date', r1, r2) + + +# long appl, short goog +g = r.gains1 - r.gains2 +tr = (1 + g).cumprod() # the total return + +# plot the return +fig, ax = plt.subplots() +ax.plot(r.date, tr) +ax.set_title('total return: long APPL, short GOOG') +ax.grid() +fig.autofmt_xdate() +plt.show() From c9caa48a05cfce16fbd7114cd75eca536468c404 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 14:13:10 +0100 Subject: [PATCH 09/11] Make longshort python3 compatible --- examples/misc/longshort.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/misc/longshort.py b/examples/misc/longshort.py index 56b121db2cfc..8e2a4367c903 100644 --- a/examples/misc/longshort.py +++ b/examples/misc/longshort.py @@ -3,18 +3,18 @@ csv file, computing the daily returns, appending the results to the record arrays, joining on date """ -import urllib +from six.moves import urllib import numpy as np import matplotlib.pyplot as plt import matplotlib.mlab as mlab # grab the price data off yahoo -u1 = urllib.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') -u2 = urllib.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=GOOG&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') +u1 = urllib.request.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') +u2 = urllib.request.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=GOOG&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv') # load the CSV files into record arrays -r1 = mlab.csv2rec(file(u1[0])) -r2 = mlab.csv2rec(file(u2[0])) +r1 = mlab.csv2rec(open(u1[0])) +r2 = mlab.csv2rec(open(u2[0])) # compute the daily returns and add these columns to the arrays gains1 = np.zeros_like(r1.adj_close) From 90d6bac0818b6e372456925d33d9ceb86a397fde Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 31 Jul 2016 14:13:46 +0100 Subject: [PATCH 10/11] dtype names must be strings and not unicode in python2 --- lib/matplotlib/mlab.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 5a1425d5d091..9b77551bb035 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2387,8 +2387,10 @@ def rec_append_fields(rec, names, arrs, dtypes=None): dtypes = dtypes * len(arrs) else: raise ValueError("dtypes must be None, a single dtype or a list") - - newdtype = np.dtype(rec.dtype.descr + list(zip(names, dtypes))) + old_dtypes = rec.dtype.descr + if six.PY2: + old_dtypes = [(a[0].encode('utf-8'), a[1]) for a in old_dtypes] + newdtype = np.dtype(old_dtypes + list(zip(names, dtypes))) newrec = np.recarray(rec.shape, dtype=newdtype) for field in rec.dtype.fields: newrec[field] = rec[field] From b926d504b796360e7668411643331c4282d9fafc Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 7 Aug 2016 09:29:50 +0100 Subject: [PATCH 11/11] Feedback from review --- lib/matplotlib/mlab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 9b77551bb035..38b09ea2cc46 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -2389,7 +2389,7 @@ def rec_append_fields(rec, names, arrs, dtypes=None): raise ValueError("dtypes must be None, a single dtype or a list") old_dtypes = rec.dtype.descr if six.PY2: - old_dtypes = [(a[0].encode('utf-8'), a[1]) for a in old_dtypes] + old_dtypes = [(name.encode('utf-8'), dt) for name, dt in old_dtypes] newdtype = np.dtype(old_dtypes + list(zip(names, dtypes))) newrec = np.recarray(rec.shape, dtype=newdtype) for field in rec.dtype.fields: @@ -2600,7 +2600,7 @@ def mapped_r2field(name): if desc[0] not in key] all_dtypes = keydesc + r1desc + r2desc if six.PY2: - all_dtypes = [(a[0].encode('utf-8'), a[1]) for a in all_dtypes] + all_dtypes = [(name.encode('utf-8'), dt) for name, dt in all_dtypes] newdtype = np.dtype(all_dtypes) newrec = np.recarray((common_len + left_len + right_len,), dtype=newdtype)