JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
475 stars 87 forks source link

Error with bbox=tight option for savefig #452

Closed akazachk closed 4 years ago

akazachk commented 5 years ago

I am encountering a possible bug when running the following code using Julia 1.1.1 on OSX 10.14.5. Without the line rc("savefig", bbox="tight"), there is no issue.

using PyCall
pygui(:qt5)
using PyPlot
rc("savefig", bbox="tight")

fig = figure(frameon=false)
titlestring = "Test"
title(titlestring)
PyPlot.savefig("test.png")
close()

The error I get is below:

ERROR: LoadError: PyError ($(Expr(:escape, :(ccall(#= /Users/username/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'ValueError'>
ValueError('list.remove(x): x not in list')
  File "/Users/username/.julia/conda/3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 716, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/Users/username/.julia/conda/3/lib/python3.7/site-packages/matplotlib/figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/Users/username/.julia/conda/3/lib/python3.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 88, in print_figure
    super().print_figure(*args, **kwargs)
  File "/Users/username/.julia/conda/3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2060, in print_figure
    bbox_extra_artists=bbox_artists)
  File "/Users/username/.julia/conda/3/lib/python3.7/site-packages/matplotlib/figure.py", line 2361, in get_tightbbox
    artists = self.get_default_bbox_extra_artists()
  File "/Users/username/.julia/conda/3/lib/python3.7/site-packages/matplotlib/figure.py", line 2332, in get_default_bbox_extra_artists
    bbox_artists.remove(self.patch)

Stacktrace:
 [1] pyerr_check at /Users/username/.julia/packages/PyCall/ttONZ/src/exception.jl:60 [inlined]
 [2] pyerr_check at /Users/username/.julia/packages/PyCall/ttONZ/src/exception.jl:64 [inlined]
 [3] macro expansion at /Users/username/.julia/packages/PyCall/ttONZ/src/exception.jl:84 [inlined]
 [4] __pycall!(::PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyObject, ::Ptr{Nothing}) at /Users/username/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44
 [5] _pycall!(::PyObject, ::PyObject, ::Tuple{String}, ::Int64, ::Ptr{Nothing}) at /Users/username/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:29
 [6] _pycall! at /Users/username/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:11 [inlined]
 [7] #pycall#110 at /Users/username/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:86 [inlined]
 [8] pycall at /Users/username/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:86 [inlined]
 [9] #savefig#97(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::String) at /Users/username/.julia/packages/PyPlot/cdCMF/src/PyPlot.jl:177
 [10] savefig(::String) at /Users/username/.julia/packages/PyPlot/cdCMF/src/PyPlot.jl:174
 [11] top-level scope at none:0
 [12] include at ./boot.jl:326 [inlined]
 [13] include_relative(::Module, ::String) at ./loading.jl:1038
 [14] include(::Module, ::String) at ./sysimg.jl:29
 [15] include(::String) at ./client.jl:403
 [16] top-level scope at none:0
in expression starting at /Users/username/test.jl:9
stevengj commented 5 years ago

I can't replicate your problem on my machine.

It looks like a problem on the Python side. Does it work in Python? That is, try running MPLBACKEND=qt5agg /Users/username/.julia/conda/3/bin/python and then

import matplotlib.pyplot as plt
plt.rc('savefig', bbox='tight')
fig = plt.figure(frameon=False)
plt.title('Test')
plt.savefig("test.png")
plt.close()

PS. This is an exception, not a crash.

akazachk commented 5 years ago

You are correct, it seems to be a problem with the way Python is set up with Julia, but I am having a hard time figuring out a solution. Namely, my system-wide Python3 installation runs the code without issues. However, when using /Users/username/.julia/conda/3/bin/python as you instructed, the exception is encountered.

Maybe something is wrong in my installation steps? I do have XQuartz 2.7.11 installed. The following is what I used to install PyPlot.

using Pkg
ENV["MPLBACKEND"]="Qt5Agg"
ENV["PYTHON"]=""
Pkg.add("PyCall")
Pkg.add("PyPlot")
using PyCall
using PyPlot
akazachk commented 4 years ago

I fixed this error by removing my .julia directory, I guess causing Julia (the next time I ran the above code) to reinstall conda/Python and whatever setting I had that caused the above exception.