@@ -4,19 +4,18 @@ module PyPlot
4
4
5
5
using PyCall
6
6
import PyCall: PyObject, pygui, pycall, pyexists
7
+ import PyCall: hasproperty # Base.hasproperty in Julia 1.2
7
8
import Base: convert, == , isequal, hash, getindex, setindex!, haskey, keys, show
9
+ using Base: @deprecate
8
10
export Figure, plt, matplotlib, pygui, withfig
9
11
10
- using Compat
11
- import Base. show
12
-
13
12
# ##########################################################################
14
13
# Julia 0.4 help system: define a documentation object
15
14
# that lazily looks up help from a PyObject via zero or more keys.
16
15
# This saves us time when loading PyPlot, since we don't have
17
16
# to load up all of the documentation strings right away.
18
17
struct LazyHelp
19
- o # a PyObject or similar object supporting getindex with a __doc__ key
18
+ o # a PyObject or similar object supporting getindex with a __doc__ property
20
19
keys:: Tuple{Vararg{String}}
21
20
LazyHelp (o) = new (o, ())
22
21
LazyHelp (o, k:: AbstractString ) = new (o, (k,))
@@ -28,8 +27,8 @@ function show(io::IO, ::MIME"text/plain", h::LazyHelp)
28
27
for k in h. keys
29
28
o = o[k]
30
29
end
31
- if haskey (o, " __doc__" )
32
- print (io, convert (AbstractString, o[ " __doc__" ] ))
30
+ if hasproperty (o, " __doc__" )
31
+ print (io, convert (AbstractString, o. " __doc__" ))
33
32
else
34
33
print (io, " no Python docstring found for " , h. k)
35
34
end
@@ -53,46 +52,46 @@ include("init.jl")
53
52
mutable struct Figure
54
53
o:: PyObject
55
54
end
56
- PyObject (f:: Figure ) = f . o
55
+ PyObject (f:: Figure ) = getfield (f, :o )
57
56
convert (:: Type{Figure} , o:: PyObject ) = Figure (o)
58
- == (f:: Figure , g:: Figure ) = f. o == g. o
59
- == (f:: Figure , g:: PyObject ) = f. o == g
60
- == (f:: PyObject , g:: Figure ) = f == g. o
61
- hash (f:: Figure ) = hash (f. o)
62
- pycall (f:: Figure , args... ; kws... ) = pycall (f. o, args... ; kws... )
63
- (f:: Figure )(args... ; kws... ) = pycall (f. o, PyAny, args... ; kws... )
64
- Base. Docs. doc (f:: Figure ) = Base. Docs. doc (f. o)
65
-
66
- getindex (f:: Figure , x) = getindex (f. o, x)
67
- setindex! (f:: Figure , v, x) = setindex! (f. o, v, x)
68
- haskey (f:: Figure , x) = haskey (f. o, x)
69
- keys (f:: Figure ) = keys (f. o)
57
+ == (f:: Figure , g:: Figure ) = PyObject (f) == PyObject (g)
58
+ == (f:: Figure , g:: PyObject ) = PyObject (f) == g
59
+ == (f:: PyObject , g:: Figure ) = f == PyObject (g)
60
+ hash (f:: Figure ) = hash (PyObject (f))
61
+ pycall (f:: Figure , args... ; kws... ) = pycall (PyObject (f), args... ; kws... )
62
+ (f:: Figure )(args... ; kws... ) = pycall (PyObject (f), PyAny, args... ; kws... )
63
+ Base. Docs. doc (f:: Figure ) = Base. Docs. doc (PyObject (f))
64
+
65
+ # Note: using `Union{Symbol,String}` produces ambiguity.
66
+ Base. getproperty (f:: Figure , s:: Symbol ) = getproperty (PyObject (f), s)
67
+ Base. getproperty (f:: Figure , s:: AbstractString ) = getproperty (PyObject (f), s)
68
+ Base. setproperty! (f:: Figure , s:: Symbol , x) = setproperty! (PyObject (f), s, x)
69
+ Base. setproperty! (f:: Figure , s:: AbstractString , x) = setproperty! (PyObject (f), s, x)
70
+ hasproperty (f:: Figure , s:: Symbol ) = hasproperty (PyObject (f), s)
71
+ Base. propertynames (f:: Figure ) = propertynames (PyObject (f))
72
+ haskey (f:: Figure , x) = haskey (PyObject (f), x)
73
+
74
+ @deprecate getindex (f:: Figure , x) getproperty (f, x)
75
+ @deprecate setindex! (f:: Figure , v, x) setproperty! (f, v, x)
76
+ @deprecate keys (f:: Figure ) propertynames (f)
70
77
71
78
for (mime,fmt) in aggformats
72
79
@eval function show (io:: IO , m:: MIME{Symbol($mime)} , f:: Figure )
73
- if ! haskey (pycall (f. o[ " canvas" ][ " get_supported_filetypes" ] , PyDict),
80
+ if ! haskey (pycall (f." canvas" . " get_supported_filetypes" , PyDict),
74
81
$ fmt)
75
82
throw (MethodError (show, (io, m, f)))
76
83
end
77
- f. o[ " canvas" ][ " print_figure" ] (io, format= $ fmt, bbox_inches= " tight" )
84
+ f." canvas" . " print_figure" (io, format= $ fmt, bbox_inches= " tight" )
78
85
end
79
86
if fmt != " svg"
80
- if isdefined (Base, :showable )
81
- @eval Base. showable (:: MIME{Symbol($mime)} , f:: Figure ) = ! isempty (f) && haskey (pycall (f. o[" canvas" ][" get_supported_filetypes" ], PyDict), $ fmt)
82
- else
83
- @eval Base. mimewritable (:: MIME{Symbol($mime)} , f:: Figure ) = ! isempty (f) && haskey (pycall (f. o[" canvas" ][" get_supported_filetypes" ], PyDict), $ fmt)
84
- end
87
+ @eval Base. showable (:: MIME{Symbol($mime)} , f:: Figure ) = ! isempty (f) && haskey (pycall (f." canvas" ." get_supported_filetypes" , PyDict), $ fmt)
85
88
end
86
89
end
87
90
88
91
# disable SVG output by default, since displaying large SVGs (large datasets)
89
92
# in IJulia is slow, and browser SVG display is buggy. (Similar to IPython.)
90
93
const SVG = [false ]
91
- if isdefined (Base, :showable )
92
- Base. showable (:: MIME"image/svg+xml" , f:: Figure ) = SVG[1 ] && ! isempty (f) && haskey (pycall (f. o[" canvas" ][" get_supported_filetypes" ], PyDict), " svg" )
93
- else
94
- Base. mimewritable (:: MIME"image/svg+xml" , f:: Figure ) = SVG[1 ] && ! isempty (f) && haskey (pycall (f. o[" canvas" ][" get_supported_filetypes" ], PyDict), " svg" )
95
- end
94
+ Base. showable (:: MIME"image/svg+xml" , f:: Figure ) = SVG[1 ] && ! isempty (f) && haskey (pycall (f." canvas" ." get_supported_filetypes" , PyDict), " svg" )
96
95
svg () = SVG[1 ]
97
96
svg (b:: Bool ) = (SVG[1 ] = b)
98
97
@@ -103,7 +102,7 @@ svg(b::Bool) = (SVG[1] = b)
103
102
# since the user is keeping track of these in some other way,
104
103
# e.g. for interactive widgets.
105
104
106
- Base. isempty (f:: Figure ) = isempty (pycall (f[ " get_axes" ] , PyVector))
105
+ Base. isempty (f:: Figure ) = isempty (pycall (f. " get_axes" , PyVector))
107
106
108
107
# We keep a set of figure numbers for the figures used in withfig, because
109
108
# for these figures we don't want to auto-display or auto-close them
@@ -114,23 +113,23 @@ const withfig_fignums = Set{Int}()
114
113
115
114
function display_figs () # called after IJulia cell executes
116
115
if isjulia_display[1 ]
117
- for manager in Gcf[ " get_all_fig_managers" ] ()
118
- f = manager[ " canvas" ][ " figure" ]
119
- if f[ : number] ∉ withfig_fignums
116
+ for manager in Gcf. " get_all_fig_managers" ()
117
+ f = manager. " canvas" . " figure"
118
+ if f. number ∉ withfig_fignums
120
119
fig = Figure (f)
121
120
isempty (fig) || display (fig)
122
- pycall (plt[ " close" ] , PyAny, f)
121
+ pycall (plt. " close" , PyAny, f)
123
122
end
124
123
end
125
124
end
126
125
end
127
126
128
127
function close_figs () # called after error in IJulia cell
129
128
if isjulia_display[1 ]
130
- for manager in Gcf[ " get_all_fig_managers" ] ()
131
- f = manager[ " canvas" ][ " figure" ]
132
- if f[ : number] ∉ withfig_fignums
133
- pycall (plt[ " close" ] , PyAny, f)
129
+ for manager in Gcf. " get_all_fig_managers" ()
130
+ f = manager. " canvas" . " figure"
131
+ if f. number ∉ withfig_fignums
132
+ pycall (plt. " close" , PyAny, f)
134
133
end
135
134
end
136
135
end
@@ -166,39 +165,39 @@ export acorr,annotate,arrow,autoscale,autumn,axhline,axhspan,axis,axvline,axvspa
166
165
# overlap with standard Julia functions:
167
166
# close, connect, fill, hist, xcorr
168
167
import Base: close, fill, step
169
- import Compat . Sockets: connect
168
+ import Sockets: connect
170
169
171
170
const plt_funcs = (:acorr,:annotate,:arrow,:autoscale,:autumn,:axes,:axhline,:axhspan,:axis,:axvline,:axvspan,:bar,:barbs,:barh,:bone,:box,:boxplot,:broken_barh,:cla,:clabel,:clf,:clim,:cohere,:colorbar,:colors,:contour,:contourf,:cool,:copper,:csd,:delaxes,:disconnect,:draw,:errorbar,:eventplot,:figaspect,:figimage,:figlegend,:figtext,:fill_between,:fill_betweenx,:findobj,:flag,:gca,:gci,:get_current_fig_manager,:get_figlabels,:get_fignums,:get_plot_commands,:ginput,:gray,:grid,:hexbin,:hlines,:hold,:hot,:hsv,:imread,:imsave,:imshow,:ioff,:ion,:ishold,:jet,:legend,:locator_params,:loglog,:margins,:matshow,:minorticks_off,:minorticks_on,:over,:pause,:pcolor,:pcolormesh,:pie,:pink,:plot,:plot_date,:plotfile,:polar,:prism,:psd,:quiver,:quiverkey,:rc,:rc_context,:rcdefaults,:rgrids,:savefig,:sca,:scatter,:sci,:semilogx,:semilogy,:set_cmap,:setp,:specgram,:spectral,:spring,:spy,:stackplot,:stem,:streamplot,:subplot,:subplot2grid,:subplot_tool,:subplots,:subplots_adjust,:summer,:suptitle,:table,:text,:thetagrids,:tick_params,:ticklabel_format,:tight_layout,:title,:tricontour,:tricontourf,:tripcolor,:triplot,:twinx,:twiny,:vlines,:waitforbuttonpress,:winter,:xkcd,:xlabel,:xlim,:xscale,:xticks,:ylabel,:ylim,:yscale,:yticks,:hist,:xcorr,:isinteractive)
172
171
173
172
for f in plt_funcs
174
173
sf = string (f)
175
174
@eval @doc LazyHelp (plt,$ sf) function $f (args... ; kws... )
176
- if ! haskey (plt, $ sf)
175
+ if ! hasproperty (plt, $ sf)
177
176
error (" matplotlib " , version, " does not have pyplot." , $ sf)
178
177
end
179
- return pycall (plt[ $ sf] , PyAny, args... ; kws... )
178
+ return pycall (plt. $ sf, PyAny, args... ; kws... )
180
179
end
181
180
end
182
181
183
- @doc LazyHelp (plt," step" ) step (x, y; kws... ) = pycall (plt[ " step" ] , PyAny, x, y; kws... )
182
+ @doc LazyHelp (plt," step" ) step (x, y; kws... ) = pycall (plt. " step" , PyAny, x, y; kws... )
184
183
185
- Base. show (; kws... ) = begin pycall (plt[ " show" ] , PyObject; kws... ); nothing ; end
184
+ Base. show (; kws... ) = begin pycall (plt. " show" , PyObject; kws... ); nothing ; end
186
185
187
- close (f:: Figure ) = close (f[ : number] )
186
+ close (f:: Figure ) = close (f. number)
188
187
function close (f:: Integer )
189
188
pop! (withfig_fignums, f, f)
190
- pycall (plt[ " close" ] , PyAny, f)
189
+ pycall (plt. " close" , PyAny, f)
191
190
end
192
- close (f:: Union{AbstractString,Symbol} ) = pycall (plt[ " close" ] , PyAny, f)
193
- @doc LazyHelp (plt," close" ) close () = pycall (plt[ " close" ] , PyAny)
191
+ close (f:: Union{AbstractString,Symbol} ) = pycall (plt. " close" , PyAny, f)
192
+ @doc LazyHelp (plt," close" ) close () = pycall (plt. " close" , PyAny)
194
193
195
- @doc LazyHelp (plt," connect" ) connect (s:: Union{AbstractString,Symbol} , f:: Function ) = pycall (plt[ " connect" ] , PyAny, s, f)
194
+ @doc LazyHelp (plt," connect" ) connect (s:: Union{AbstractString,Symbol} , f:: Function ) = pycall (plt. " connect" , PyAny, s, f)
196
195
197
196
@doc LazyHelp (plt," fill" ) fill (x:: AbstractArray ,y:: AbstractArray , args... ; kws... ) =
198
- pycall (plt[ " fill" ] , PyAny, x, y, args... ; kws... )
197
+ pycall (plt. " fill" , PyAny, x, y, args... ; kws... )
199
198
200
199
# consistent capitalization with mplot3d, avoid conflict with Base.hist2d
201
- @doc LazyHelp (plt," hist2d" ) hist2D (args... ; kws... ) = pycall (plt[ " hist2d" ] , PyAny, args... ; kws... )
200
+ @doc LazyHelp (plt," hist2d" ) hist2D (args... ; kws... ) = pycall (plt. " hist2d" , PyAny, args... ; kws... )
202
201
203
202
include (" colormaps.jl" )
204
203
@@ -212,9 +211,9 @@ function bar(x::AbstractVector{T}, y; kws...) where T<:AbstractString
212
211
end
213
212
p = bar (xi, y; kws... )
214
213
ax = any (kw -> kw[1 ] == :orientation && lowercase (kw[2 ]) == " horizontal" ,
215
- kws) ? gca ()[ " yaxis" ] : gca ()[ " xaxis" ]
216
- ax[ " set_ticks" ] (xi)
217
- ax[ " set_ticklabels" ] (x)
214
+ kws) ? gca (). " yaxis" : gca (). " xaxis"
215
+ ax. " set_ticks" (xi)
216
+ ax. " set_ticklabels" (x)
218
217
return p
219
218
end
220
219
274
273
275
274
function withfig (actions:: Function , f:: Figure ; clear= true )
276
275
ax_save = gca ()
277
- push! (withfig_fignums, f[ : number] )
278
- figure (f[ : number] )
279
- @compat finalizer (close, f)
276
+ push! (withfig_fignums, f. number)
277
+ figure (f. number)
278
+ finalizer (close, f)
280
279
try
281
280
if clear && ! isempty (f)
282
281
clf ()
0 commit comments