FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/examples/api/date_index_formatter.py at master · codemaru/matplotlib · GitHub
codemaru
matplotlib
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
examples
/
api
/
date_index_formatter.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
40 lines (31 loc) · 1.08 KB
Breadcrumbs
matplotlib
/
examples
/
api
/
date_index_formatter.py
Copy path
File metadata and controls
40 lines (31 loc) · 1.08 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
"""
When plotting time series, e.g., financial time series, one often wants
to leave out days on which there is no data, eh weekends. The example
below shows how to use an 'index formatter' to achieve the desired plot
"""
from
__future__
import
print_function
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
import
matplotlib
.
mlab
as
mlab
import
matplotlib
.
cbook
as
cbook
import
matplotlib
.
ticker
as
ticker
datafile
=
cbook
.
get_sample_data
(
'aapl.csv'
,
asfileobj
=
False
)
print
(
'loading %s'
%
datafile
)
r
=
mlab
.
csv2rec
(
datafile
)
r
.
sort
()
r
=
r
[
-
30
:]
# get the last 30 days
# first we'll do it the default way, with gaps on weekends
fig
,
ax
=
plt
.
subplots
()
ax
.
plot
(
r
.
date
,
r
.
adj_close
,
'o-'
)
fig
.
autofmt_xdate
()
# next we'll write a custom formatter
N
=
len
(
r
)
ind
=
np
.
arange
(
N
)
# the evenly spaced plot indices
def
format_date
(
x
,
pos
=
None
):
thisind
=
np
.
clip
(
int
(
x
+
0.5
),
0
,
N
-
1
)
return
r
.
date
[
thisind
].
strftime
(
'%Y-%m-%d'
)
fig
,
ax
=
plt
.
subplots
()
ax
.
plot
(
ind
,
r
.
adj_close
,
'o-'
)
ax
.
xaxis
.
set_major_formatter
(
ticker
.
FuncFormatter
(
format_date
))
fig
.
autofmt_xdate
()
plt
.
show
()
Back
|
FazBrowse Home
|
New Git URL