@@ -4,19 +4,18 @@ module PyPlot
44
55using PyCall
66import PyCall: PyObject, pygui, pycall, pyexists
7+ import PyCall: hasproperty # Base.hasproperty in Julia 1.2
78import Base: convert, == , isequal, hash, getindex, setindex!, haskey, keys, show
9+ using Base: @deprecate
810export Figure, plt, matplotlib, pygui, withfig
911
10- using Compat
11- import Base. show
12-
1312# ##########################################################################
1413# Julia 0.4 help system: define a documentation object
1514# that lazily looks up help from a PyObject via zero or more keys.
1615# This saves us time when loading PyPlot, since we don't have
1716# to load up all of the documentation strings right away.
1817struct 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
2019 keys:: Tuple{Vararg{String}}
2120 LazyHelp (o) = new (o, ())
2221 LazyHelp (o, k:: AbstractString ) = new (o, (k,))
@@ -28,8 +27,8 @@ function show(io::IO, ::MIME"text/plain", h::LazyHelp)
2827 for k in h. keys
2928 o = o[k]
3029 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__" ))
3332 else
3433 print (io, " no Python docstring found for " , h. k)
3534 end
@@ -53,46 +52,46 @@ include("init.jl")
5352mutable struct Figure
5453 o:: PyObject
5554end
56- PyObject (f:: Figure ) = f . o
55+ PyObject (f:: Figure ) = getfield (f, :o )
5756convert (:: 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)
7077
7178for (mime,fmt) in aggformats
7279 @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),
7481 $ fmt)
7582 throw (MethodError (show, (io, m, f)))
7683 end
77- f. o[ " canvas" ][ " print_figure" ] (io, format= $ fmt, bbox_inches= " tight" )
84+ f." canvas" . " print_figure" (io, format= $ fmt, bbox_inches= " tight" )
7885 end
7986 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)
8588 end
8689end
8790
8891# disable SVG output by default, since displaying large SVGs (large datasets)
8992# in IJulia is slow, and browser SVG display is buggy. (Similar to IPython.)
9093const 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" )
9695svg () = SVG[1 ]
9796svg (b:: Bool ) = (SVG[1 ] = b)
9897
@@ -103,7 +102,7 @@ svg(b::Bool) = (SVG[1] = b)
103102# since the user is keeping track of these in some other way,
104103# e.g. for interactive widgets.
105104
106- Base. isempty (f:: Figure ) = isempty (pycall (f[ " get_axes" ] , PyVector))
105+ Base. isempty (f:: Figure ) = isempty (pycall (f. " get_axes" , PyVector))
107106
108107# We keep a set of figure numbers for the figures used in withfig, because
109108# for these figures we don't want to auto-display or auto-close them
@@ -114,23 +113,23 @@ const withfig_fignums = Set{Int}()
114113
115114function display_figs () # called after IJulia cell executes
116115 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
120119 fig = Figure (f)
121120 isempty (fig) || display (fig)
122- pycall (plt[ " close" ] , PyAny, f)
121+ pycall (plt. " close" , PyAny, f)
123122 end
124123 end
125124 end
126125end
127126
128127function close_figs () # called after error in IJulia cell
129128 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)
134133 end
135134 end
136135 end
@@ -166,39 +165,39 @@ export acorr,annotate,arrow,autoscale,autumn,axhline,axhspan,axis,axvline,axvspa
166165# overlap with standard Julia functions:
167166# close, connect, fill, hist, xcorr
168167import Base: close, fill, step
169- import Compat . Sockets: connect
168+ import Sockets: connect
170169
171170const 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)
172171
173172for f in plt_funcs
174173 sf = string (f)
175174 @eval @doc LazyHelp (plt,$ sf) function $f (args... ; kws... )
176- if ! haskey (plt, $ sf)
175+ if ! hasproperty (plt, $ sf)
177176 error (" matplotlib " , version, " does not have pyplot." , $ sf)
178177 end
179- return pycall (plt[ $ sf] , PyAny, args... ; kws... )
178+ return pycall (plt. $ sf, PyAny, args... ; kws... )
180179 end
181180end
182181
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... )
184183
185- Base. show (; kws... ) = begin pycall (plt[ " show" ] , PyObject; kws... ); nothing ; end
184+ Base. show (; kws... ) = begin pycall (plt. " show" , PyObject; kws... ); nothing ; end
186185
187- close (f:: Figure ) = close (f[ : number] )
186+ close (f:: Figure ) = close (f. number)
188187function close (f:: Integer )
189188 pop! (withfig_fignums, f, f)
190- pycall (plt[ " close" ] , PyAny, f)
189+ pycall (plt. " close" , PyAny, f)
191190end
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)
194193
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)
196195
197196@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... )
199198
200199# 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... )
202201
203202include (" colormaps.jl" )
204203
@@ -212,9 +211,9 @@ function bar(x::AbstractVector{T}, y; kws...) where T<:AbstractString
212211 end
213212 p = bar (xi, y; kws... )
214213 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)
218217 return p
219218end
220219
274273
275274function withfig (actions:: Function , f:: Figure ; clear= true )
276275 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)
280279 try
281280 if clear && ! isempty (f)
282281 clf ()
0 commit comments