Skip to content

Commit f2827fd

Browse files
author
Christopher Doris
committed
auto show matplotlib plots
1 parent 6527f87 commit f2827fd

File tree

5 files changed

+66
-62
lines changed

5 files changed

+66
-62
lines changed

src/PythonCall.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module External
55
end
66

77
using Base: @propagate_inbounds
8-
using MacroTools, Dates, Tables, Markdown, Serialization
8+
using MacroTools, Dates, Tables, Markdown, Serialization, Requires
99

1010
include("utils.jl")
1111
include("conda.jl")
@@ -76,6 +76,7 @@ include("compat/with.jl")
7676
include("compat/multimedia.jl")
7777
include("compat/serialization.jl")
7878
include("compat/gui.jl")
79+
include("compat/matplotlib.jl")
7980

8081
function __init__()
8182
C.with_gil() do
@@ -99,6 +100,7 @@ function __init__()
99100
init_juliacall_2()
100101
init_stdlib()
101102
init_gui()
103+
init_matplotlib()
102104
end
103105
end
104106

src/compat/matplotlib.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
pyplot_show([fig]; close=true, [format])
3+
4+
Show the matplotlib/pyplot/seaborn/etc figure `fig`, or all open figures if not given, using Julia's display mechanism.
5+
6+
If `close` is true, the figure is also closed.
7+
8+
The `format` specifies the file format of the generated image.
9+
By default this is `pyplot.rcParams["savefig.format"]` or `"png"`.
10+
It can be one of `"png"`, `"jpg"`, `"jpeg"`, `"tif"`, `"tiff"`, `"svg"` or `"pdf"`.
11+
"""
12+
function pyplot_show(fig; close::Bool = true, format::String = "")
13+
plt = pyimport("matplotlib.pyplot")
14+
io = pyimport("io")
15+
if !pyisinstance(fig, plt.Figure)
16+
fig = plt.figure(fig)
17+
end
18+
buf = io.BytesIO()
19+
if isempty(format)
20+
format = pyconvert(String, plt.rcParams.get("savefig.format", "png"))
21+
end
22+
if format ("png", "jpg", "jpeg", "tif", "tiff", "svg", "pdf")
23+
error("invalid format: $format")
24+
end
25+
fig.savefig(buf, format=format)
26+
data = pyconvert(Vector{UInt8}, buf.getvalue())
27+
if close
28+
plt.close(fig)
29+
end
30+
if format == "png"
31+
display(MIME("image/png"), data)
32+
elseif format in ("jpg", "jpeg")
33+
display(MIME("image/jpeg"), data)
34+
elseif format in ("tif", "tiff")
35+
display(MIME("image/tiff"), data)
36+
elseif format == "svg"
37+
display(MIME("image/svg+xml"), String(data))
38+
elseif format == "pdf"
39+
display(MIME("application/pdf"), data)
40+
else
41+
@assert false
42+
end
43+
nothing
44+
end
45+
function pyplot_show(; opts...)
46+
plt = pysysmodule.modules.get("matplotlib.pyplot", nothing)
47+
if !pyisnone(plt)
48+
for fig in plt.get_fignums()
49+
pyplot_show(fig; opts...)
50+
end
51+
end
52+
end
53+
export pyplot_show
54+
55+
function init_matplotlib()
56+
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
57+
IJulia.push_postexecute_hook() do
58+
CONFIG.auto_pyplot_show && pyplot_show()
59+
nothing
60+
end
61+
end
62+
end

src/compat/stdlib.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ function init_stdlib()
4040
sys.meta_path.insert(0, JULIA_COMPAT_HOOKS)
4141
""" g
4242
pycopy!(pymodulehooks, g["JULIA_COMPAT_HOOKS"])
43-
44-
# @require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
45-
# IJulia.push_postexecute_hook() do
46-
# CONFIG.pyplotautoshow && pyplotshow()
47-
# end
48-
# end
4943
end
5044

5145
end

src/config.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Base.@kwdef mutable struct Config
22
auto_sys_last_traceback :: Bool = true
33
auto_fix_qt_plugin_path :: Bool = true
4+
auto_pyplot_show :: Bool = true
45
end
56

67
const CONFIG = Config()

src/old/matplotlib.jl

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)