From ac731191e6a2955014b2ac9b7f7e3ddac8d92ee9 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 29 Dec 2018 13:06:44 +0100 Subject: [PATCH] Make examples that load msft.csv robust against locale changes. ... which may or may not affect datetime parsing by bytespdate2num. --- examples/misc/load_converter.py | 16 +++++++------- examples/misc/plotfile_demo.py | 1 + .../ticks_and_spines/date_index_formatter2.py | 21 ++++++++----------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/misc/load_converter.py b/examples/misc/load_converter.py index 9488972cae87..2c3e03f73b89 100644 --- a/examples/misc/load_converter.py +++ b/examples/misc/load_converter.py @@ -4,19 +4,21 @@ ============== """ -import numpy as np + +import dateutil.parser +from matplotlib import cbook, dates import matplotlib.pyplot as plt -import matplotlib.cbook as cbook -from matplotlib.dates import bytespdate2num +import numpy as np + datafile = cbook.get_sample_data('msft.csv', asfileobj=False) print('loading', datafile) -dates, closes = np.loadtxt(datafile, delimiter=',', - converters={0: bytespdate2num('%d-%b-%y')}, - skiprows=1, usecols=(0, 2), unpack=True) +data = np.genfromtxt( + datafile, delimiter=',', names=True, + converters={0: lambda s: dates.date2num(dateutil.parser.parse(s))}) fig, ax = plt.subplots() -ax.plot_date(dates, closes, '-') +ax.plot_date(data['Date'], data['High'], '-') fig.autofmt_xdate() plt.show() diff --git a/examples/misc/plotfile_demo.py b/examples/misc/plotfile_demo.py index 94bd1bd9b2bb..d55323b1f7c7 100644 --- a/examples/misc/plotfile_demo.py +++ b/examples/misc/plotfile_demo.py @@ -5,6 +5,7 @@ Example use of ``plotfile`` to plot data directly from a file. """ + import matplotlib.pyplot as plt import matplotlib.cbook as cbook diff --git a/examples/ticks_and_spines/date_index_formatter2.py b/examples/ticks_and_spines/date_index_formatter2.py index 4d9e5750bb5e..c59342e134a7 100644 --- a/examples/ticks_and_spines/date_index_formatter2.py +++ b/examples/ticks_and_spines/date_index_formatter2.py @@ -11,19 +11,18 @@ Formatter to get the appropriate date string for a given index. """ - -import numpy as np - +import dateutil.parser +from matplotlib import cbook, dates import matplotlib.pyplot as plt -import matplotlib.cbook as cbook -from matplotlib.dates import bytespdate2num, num2date from matplotlib.ticker import Formatter +import numpy as np datafile = cbook.get_sample_data('msft.csv', asfileobj=False) print('loading %s' % datafile) -msft_data = np.genfromtxt(datafile, delimiter=',', names=True, - converters={0: bytespdate2num('%d-%b-%y')})[-40:] +msft_data = np.genfromtxt( + datafile, delimiter=',', names=True, + converters={0: lambda s: dates.date2num(dateutil.parser.parse(s))}) class MyFormatter(Formatter): @@ -36,13 +35,11 @@ def __call__(self, x, pos=0): ind = int(np.round(x)) if ind >= len(self.dates) or ind < 0: return '' + return dates.num2date(self.dates[ind]).strftime(self.fmt) - return num2date(self.dates[ind]).strftime(self.fmt) - -formatter = MyFormatter(msft_data['Date']) fig, ax = plt.subplots() -ax.xaxis.set_major_formatter(formatter) -ax.plot(np.arange(len(msft_data)), msft_data['Close'], 'o-') +ax.xaxis.set_major_formatter(MyFormatter(msft_data['Date'])) +ax.plot(msft_data['Close'], 'o-') fig.autofmt_xdate() plt.show()