10000 Backport PR #14087: Cleanup date example. · matplotlib/matplotlib@f1ec582 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1ec582

Browse files
timhoffmMeeseeksDev[bot]
authored andcommitted
Backport PR #14087: Cleanup date example.
1 parent b9d3c83 commit f1ec582

File tree

1 file changed

+15
-17
lines changed
  • examples/text_labels_and_annotations

1 file changed

+15
-17
lines changed

examples/text_labels_and_annotations/date.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
Date tick labels
44
================
55
6-
Show how to make date plots in matplotlib using date tick locators and
7-
formatters. See major_minor_demo1.py for more information on
8-
controlling major and minor ticks
6+
Show how to make date plots in Matplotlib using date tick locators and
7+
formatters. See :doc:`/gallery/ticks_and_spines/major_minor_demo` for more
8+
information on controlling major and minor ticks.
99
1010
All matplotlib date plotting is done by converting date instances into days
1111
since 0001-01-01 00:00:00 UTC plus one day (for historical reasons). The
@@ -23,33 +23,31 @@
2323

2424
years = mdates.YearLocator() # every year
2525
months = mdates.MonthLocator() # every month
26-
yearsFmt = mdates.DateFormatter('%Y')
26+
years_fmt = mdates.DateFormatter('%Y')
2727

28-
# Load a numpy record array from yahoo csv data with fields date, open, close,
29-
# volume, adj_close from the mpl-data/example directory. The record array
30-
# stores the date as an np.datetime64 with a day unit ('D') in the date column.
28+
# Load a numpy structured array from yahoo csv data with fields date, open,
29+
# close, volume, adj_close from the mpl-data/example directory. This array
30+
# stores the date as an np.datetime64 with a day unit ('D') in the 'date'
31+
# column.
3132
with cbook.get_sample_data('goog.npz') as datafile:
32-
r = np.load(datafile)['price_data'].view(np.recarray)
33+
data = np.load(datafile)['price_data']
3334

3435
fig, ax = plt.subplots()
35-
ax.plot(r.date, r.adj_close)
36+
ax.plot('date', 'adj_close', data=data)
3637

3738
# format the ticks
3839
ax.xaxis.set_major_locator(years)
39-
ax.xaxis.set_major_formatter(yearsFmt)
40+
ax.xaxis.set_major_formatter(years_fmt)
4041
ax.xaxis.set_minor_locator(months)
4142

42-
# round to nearest years...
43-
datemin = np.datetime64(r.date[0], 'Y')
44-
datemax = np.datetime64(r.date[-1], 'Y') + np.timedelta64(1, 'Y')
43+
# round to nearest years.
44+
datemin = np.datetime64(data['date'][0], 'Y')
45+
datemax = np.datetime64(data['date'][-1], 'Y') + np.timedelta64(1, 'Y')
4546
ax.set_xlim(datemin, datemax)
4647

47-
4848
# format the coords message box
49-
def price(x):
50-
return '$%1.2f' % x
5149
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
52-
ax.format_ydata = price
50+
ax.format_ydata = lambda x: '$%1.2f' % x # format the price.
5351
ax.grid(True)
5452

5553
# rotates and right aligns the x labels, and moves the bottom of the

0 commit comments

Comments
 (0)
0