Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyPlot equivalent? #105

Closed
henry2004y opened this issue Feb 10, 2022 · 7 comments
Closed

PyPlot equivalent? #105

henry2004y opened this issue Feb 10, 2022 · 7 comments

Comments

@henry2004y
Copy link

Hi,

Thanks for sharing this package! Since I haven't found anything related to Matplotlib in the doc, it makes me wondering if there is kind of a PyPlot.jl equivalent workflow that is enabled by this package? If so, will there be any limitations compared with PyPlot.jl?

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 10, 2022

Matplotlib should just work:

plt = pyimport("matplotlib.pyplot")
plt.plot(rand(100))
plt.show()

There are a few helpers noted here: https://cjdoris.github.io/PythonCall.jl/stable/compat/#MatPlotLib-/-PyPlot-/-Seaborn

@henry2004y
Copy link
Author

Sorry for bothering again! I somehow got stuck at installing Matplotlib with CondaPkg.

When importing Matplotlib, it complained to me first about The TkAgg3 backends require cairo, and later The GTK3 backends require PyGObject.
After trying to install several dependencies for Matplotlib, this is where I am:

(v1.7) pkg> conda status
CondaPkg Status /home/hongyang/.julia/environments/v1.7/CondaPkg.toml
Packages
  pygobject v3.42.0
  matplotlib v3.5.1
  cairo v1.16.0
  numpy v1.22.2
  python v3.10.2
  pycairo v1.20.1
  pyqt v5.12.3

julia> plt = pyimport("matplotlib.pyplot")
ERROR: Python: ImportError: 
Python stacktrace:
 [1] <module>
   @ ~/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/backends/backend_gtk3.py:29
 [2] <module>
   @ ~/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/backends/backend_gtk3agg.py:8
 [3] import_module
   @ importlib ~/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/importlib/__init__.py:126
 [4] backend_mod
   @ ~/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/pyplot.py:268
 [5] switch_backend
   @ ~/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/pyplot.py:267
 [6] <module>
   @ ~/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/pyplot.py:2230
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/B5irB/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/B5irB/src/err.jl:10 [inlined]
 [3] pyimport(m::String)
   @ PythonCall ~/.julia/packages/PythonCall/B5irB/src/concrete/import.jl:11
 [4] top-level scope
   @ REPL[6]:1

Anything I shall try to fix this Conda dependency issue?

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 10, 2022

Does this happen in a fresh Julia session? What about if you do pkg> conda run python and import matplotlib there?

@henry2004y
Copy link
Author

henry2004y commented Feb 10, 2022

Yes this is a fresh installation of CondaPkg. With pkg> conda run python:

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "/home/hongyang/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/backends/backend_gtk3.py", line 25, in <module>
    gi.require_version("Gtk", "3.0")
  File "/home/hongyang/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/gi/__init__.py", line 126, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available

Now it seems that I need GTK+ libs for Python, but I don't know what exact libraries they are...

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 10, 2022

Try a different MPL backend, like Qt5Agg?

Actually can you try again in a fresh Conda environment outside of Julia? I'm trying to establish if this is an issue with MPL or PythonCall.

@henry2004y
Copy link
Author

Switching to the Qt+ backend works for me under the following conda environments:

(v1.7) pkg> conda status
CondaPkg Status /home/hongyang/.julia/environments/v1.7/CondaPkg.toml
Packages
  pygobject v3.42.0
  matplotlib v3.5.1
  numpy v1.22.2
  python v3.10.2
  pycairo v1.20.1
julia> using PythonCall

julia> mpl = pyimport("matplotlib")
Python module: <module 'matplotlib' from '/home/hongyang/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/__init__.py'>

julia> mpl.use("qtagg")
Python None

julia> plt = pyimport("matplotlib.pyplot")
Python module: <module 'matplotlib.pyplot' from '/home/hongyang/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/matplotlib/pyplot.py'>

julia> plt.plot(rand(100))
Python list: [<matplotlib.lines.Line2D object at 0x7f932fef91e0>]

julia> plt.show()
Python None

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 11, 2022

Ok great. Closing the issue but feel free to ask more.

@cjdoris cjdoris closed this as completed Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants