FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib.github.com/1.3.1/mpl_examples/api/date_demo.py at main · matplotlib/matplotlib.github.com · GitHub
matplotlib
/
matplotlib.github.com
Public
Notifications
You must be signed in to change notification settings
Fork
62
Star
29
Code
Issues
1
Pull requests
2
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib.github.com
/
1.3.1
/
mpl_examples
/
api
/
date_demo.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (43 loc) · 1.63 KB
Breadcrumbs
matplotlib.github.com
/
1.3.1
/
mpl_examples
/
api
/
date_demo.py
Copy path
File metadata and controls
54 lines (43 loc) · 1.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
"""
Show how to make date plots in matplotlib using date tick locators and
formatters. See major_minor_demo1.py for more information on
controlling major and minor ticks
All matplotlib date plotting is done by converting date instances into
days since the 0001-01-01 UTC. The conversion, tick locating and
formatting is done behind the scenes so this is most transparent to
you. The dates module provides several converter functions date2num
and num2date
"""
import
datetime
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
import
matplotlib
.
dates
as
mdates
import
matplotlib
.
cbook
as
cbook
years
=
mdates
.
YearLocator
()
# every year
months
=
mdates
.
MonthLocator
()
# every month
yearsFmt
=
mdates
.
DateFormatter
(
'%Y'
)
# load a numpy record array from yahoo csv data with fields date,
# open, close, volume, adj_close from the mpl-data/example directory.
# The record array stores python datetime.date as an object array in
# the date column
datafile
=
cbook
.
get_sample_data
(
'goog.npy'
)
r
=
np
.
load
(
datafile
).
view
(
np
.
recarray
)
fig
,
ax
=
plt
.
subplots
()
ax
.
plot
(
r
.
date
,
r
.
adj_close
)
# format the ticks
ax
.
xaxis
.
set_major_locator
(
years
)
ax
.
xaxis
.
set_major_formatter
(
yearsFmt
)
ax
.
xaxis
.
set_minor_locator
(
months
)
datemin
=
datetime
.
date
(
r
.
date
.
min
().
year
,
1
,
1
)
datemax
=
datetime
.
date
(
r
.
date
.
max
().
year
+
1
,
1
,
1
)
ax
.
set_xlim
(
datemin
,
datemax
)
# format the coords message box
def
price
(
x
):
return
'$%1.2f'
%
x
ax
.
format_xdata
=
mdates
.
DateFormatter
(
'%Y-%m-%d'
)
ax
.
format_ydata
=
price
ax
.
grid
(
True
)
# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig
.
autofmt_xdate
()
plt
.
show
()
Back
|
FazBrowse Home
|
New Git URL