Skip to content

Commit 5f5a0f2

Browse files
committed
first pass at PythonCall port
1 parent 67305af commit 5f5a0f2

File tree

9 files changed

+492
-324
lines changed

9 files changed

+492
-324
lines changed

CondaPkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
channels = ["conda-forge"]
2+
3+
[deps]
4+
matplotlib = ">=1"

Project.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
name = "PyPlot"
2-
uuid = "d330b81b-6aea-500a-939a-2ce795aea3ee"
3-
version = "2.10.0"
1+
name = "PythonPlot"
2+
uuid = "274fc56d-3b97-40fa-a1cd-1b4a50311bf9"
3+
version = "1.0"
44

55
[deps]
66
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
77
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
8-
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
8+
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
9+
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
910
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
1011
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1112
VersionParsing = "81def892-9a0e-5fdd-b105-ffc91e053289"
1213

1314
[compat]
14-
PyCall = "1.90"
15-
julia = "1"
15+
PythonCall = "0.5"
16+
CondaPkg = "0.2"
17+
julia = "1.4"
1618
LaTeXStrings = "1"
1719
Colors = "0.9, 0.10, 0.11, 0.12, 1"
1820
VersionParsing = "1"

README.md

Lines changed: 33 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
[![Build Status](https://travis-ci.org/JuliaPy/PyPlot.jl.svg?branch=master)](https://travis-ci.org/JuliaPy/PyPlot.jl)
2-
[![Build status](https://ci.appveyor.com/api/projects/status/github/JuliaPy/PyPlot.jl?branch=master)](https://ci.appveyor.com/project/StevenGJohnson/pyplot-jl/branch/master)
3-
4-
# The PyPlot module for Julia
1+
# The PythonPlot module for Julia
52

63
This module provides a Julia interface to the
74
[Matplotlib](http://matplotlib.org/) plotting library from Python, and
85
specifically to the `matplotlib.pyplot` module.
96

10-
PyPlot uses the Julia [PyCall](https://github.com/stevengj/PyCall.jl)
7+
PythonPlot uses the Julia [PythonCall.jl](https://github.com/cjdoris/PythonCall.jl)
118
package to call Matplotlib directly from Julia with little or no
12-
overhead (arrays are passed without making a copy).
9+
overhead (arrays are passed without making a copy). It is based on a fork of the [PyPlot.jl](https://github.com/JuliaPy/PyPlot.jl) package, which uses the older [PyCall.jl](https://github.com/JuliaPy/PyCall.jl) interface to Python, and is intended to function as a mostly drop-in replacement for PyPlot.jl.
1310

1411
This package takes advantage of Julia's [multimedia
1512
I/O](https://docs.julialang.org/en/latest/base/io-network/#Multimedia-I/O-1)
@@ -18,74 +15,27 @@ inline graphics in [IJulia](https://github.com/JuliaLang/IJulia.jl).
1815
Alternatively, you can use a Python-based graphical Matplotlib
1916
backend to support interactive plot zooming etcetera.
2017

21-
(This PyPlot package replaces an earlier package of the same name by
22-
[Junfeng Li](https://github.com/autozimu/), which used PyPlot over a
23-
ZeroMQ socket with IPython.)
24-
2518
## Installation
2619

27-
You will need to have the Python [Matplotlib](http://matplotlib.org/)
28-
library installed on your machine in order to use PyPlot. You can either
20+
The PythonPlot package uses the [CondaPkg.jl](https://github.com/cjdoris/CondaPkg.jl) package to automatically install Matplotlib as needed.
21+
22+
(If you configure PythonCall to use some custom Python installation, you will need to install Matplotlib yourself.)
23+
24+
You can either
2925
do inline plotting with [IJulia](https://github.com/JuliaLang/IJulia.jl),
3026
which doesn't require a GUI backend, or use the Qt, wx, or GTK+ backends
3127
of Matplotlib as described below.
3228

33-
Once Matplotlib is installed, then you can just use
34-
`Pkg.add("PyPlot")` in Julia to install PyPlot and its dependencies.
35-
36-
### Automated Matplotlib installation
37-
38-
If you set up PyCall to use the
39-
[Conda.jl](https://github.com/Luthaf/Conda.jl) package to install a
40-
private (not in the system `PATH`) Julia Python distribution (via
41-
Miniconda), then PyPlot will automatically install Matplotlib as needed.
42-
43-
If you are installing PyCall and PyPlot for the first time, just do `ENV["PYTHON"]=""` before running `Pkg.add("PyPlot")`. Otherwise, you can reconfigure PyCall to use Conda via:
44-
```
45-
ENV["PYTHON"]=""
46-
Pkg.build("PyCall")
47-
```
48-
The next time you import `PyPlot`, it will tell Conda to install Matplotlib.
49-
50-
### OS X
51-
52-
On MacOS, you should either install
53-
[XQuartz](http://xquartz.macosforge.org/landing/) for MacOS 10.9 or
54-
later or install the [Anaconda](http://continuum.io/downloads) Python
55-
distribution in order to get a fully functional PyPlot.
56-
57-
MacOS 10.9 comes with Python and Matplotlib, but this version of
58-
Matplotlib defaults to with the Cocoa GUI backend, which is [not
59-
supported by PyPlot](https://github.com/stevengj/PyPlot.jl/issues/11).
60-
It also has a Tk backend, which is supported, but the Tk backend does
61-
not work unless you install XQuartz.
62-
63-
Alternatively, you can install the
64-
[Anaconda](http://continuum.io/downloads) Python distribution
65-
(which also includes `ipython` and other IJulia dependencies).
66-
67-
Otherwise, you can use the [Homebrew](http://brew.sh/) package manager:
68-
```
69-
brew install python gcc freetype pyqt
70-
brew link --force freetype
71-
export PATH="/usr/local/bin:$PATH"
72-
export PYTHONPATH="/usr/local/lib/python2.7:$PYTHONPATH"
73-
pip install numpy scipy matplotlib
74-
```
75-
(You may want to add the two `export` commands to your `~/.profile` file so that they
76-
are automatically executed whenever you start a shell.)
77-
7829
## Basic usage
7930

80-
Once Matplotlib and PyPlot are installed, and you are using a
31+
Once Matplotlib and PythonPlot are installed, and you are using a
8132
graphics-capable Julia environment such as IJulia, you can simply type
82-
`using PyPlot` and begin calling functions in the
33+
`using PythonPlot` and begin calling functions in the
8334
[matplotlib.pyplot](http://matplotlib.org/api/pyplot_api.html) API.
8435
For example:
8536

8637
```
87-
using PyPlot
88-
# use x = linspace(0,2*pi,1000) in Julia 0.6
38+
using PythonPlot
8939
x = range(0; stop=2*pi, length=1000); y = sin.(3 * x + 4 * cos.(2 * x));
9040
plot(x, y, color="red", linewidth=2.0, linestyle="--")
9141
title("A sinusoidally modulated sinusoid")
@@ -99,29 +49,29 @@ e.g. Julia uses `true` and `nothing` instead of Python's `True` and
9949
The full `matplotlib.pyplot` API is far too extensive to describe here;
10050
see the [matplotlib.pyplot documentation for more
10151
information](http://matplotlib.org/api/pyplot_api.html). The Matplotlib
102-
version number is returned by `PyPlot.version`.
52+
version number is returned by `PythonPlot.version`.
10353

10454
### Exported functions
10555

10656
Only the currently documented `matplotlib.pyplot` API is exported. To use
10757
other functions in the module, you can also call `matplotlib.pyplot.foo(...)`
10858
as `plt.foo(...)`. For example, `plt.plot(x, y)` also works. (And
109-
the raw `PyObject` for the `matplotlib` modules is also accessible
110-
as `PyPlot.matplotlib`.)
59+
the raw `Py` object for the `matplotlib` modules is also accessible
60+
as `PythonPlot.matplotlib`.)
11161

11262
Matplotlib is somewhat inconsistent about capitalization: it has
11363
`contour3D` but `bar3d`, etcetera. PyPlot renames all such functions
11464
to use a capital *D* (e.g. it has `hist2D`, `bar3D`, and so on).
11565

11666
You must also explicitly qualify some functions
117-
built-in Julia functions. In particular, `PyPlot.xcorr`,
118-
`PyPlot.axes`, and `PyPlot.isinteractive`
67+
built-in Julia functions. In particular, `PythonPlot.xcorr`,
68+
`PythonPlot.axes`, and `PythonPlot.isinteractive`
11969
must be used to access `matplotlib.pyplot.xcorr`
12070
etcetera.
12171

12272
If you wish to access *all* of the PyPlot functions exclusively
12373
through `plt.somefunction(...)`, as is conventional in Python, you can
124-
do `import PyPlot; const plt = PyPlot` instead of `using PyPlot`.
74+
do `import PythonPlot as plt` instead of `using PythonPlot`.
12575

12676
### Figure objects
12777

@@ -137,20 +87,20 @@ Matplotlib backend.
13787

13888
## Non-interactive plotting
13989

140-
If you use PyPlot from an interactive Julia prompt, such as the Julia
90+
If you use PythonPlot from an interactive Julia prompt, such as the Julia
14191
[command-line prompt](http://docs.julialang.org/en/latest/manual/interacting-with-julia/)
14292
or an IJulia notebook, then plots appear immediately after a plotting
14393
function (`plot` etc.) is evaluated.
14494

145-
However, if you use PyPlot from a Julia script that is run non-interactively
95+
However, if you use PythonPlot from a Julia script that is run non-interactively
14696
(e.g. `julia myscript.jl`), then Matplotlib is executed in
14797
[non-interactive mode](http://matplotlib.org/faq/usage_faq.html#what-is-interactive-mode):
14898
a plot window is not opened until you run `show()` (equivalent to `plt.show()`
14999
in the Python examples).
150100

151101
## Interactive versus Julia graphics
152102

153-
PyPlot can use any Julia graphics backend capable of displaying PNG,
103+
PythonPlot can use any Julia graphics backend capable of displaying PNG,
154104
SVG, or PDF images, such as the IJulia environment. To use a
155105
different backend, simply call `pushdisplay` with the desired
156106
`Display`; see the [Julia multimedia display
@@ -168,7 +118,7 @@ while `pygui(false)` will return to the Julia backend. Even when a
168118
Python GUI is running, you can display the current figure with the
169119
Julia backend by running `display(gcf())`.
170120

171-
If no Julia graphics backend is available when PyPlot is imported, then
121+
If no Julia graphics backend is available when PythonPlot is imported, then
172122
`pygui(true)` is the default.
173123

174124
### Choosing a Python GUI toolkit
@@ -177,24 +127,24 @@ Only the [Tk](http://www.tcl.tk/), [wxWidgets](http://www.wxwidgets.org/),
177127
[GTK+](http://www.gtk.org/) (version 2 or 3), and [Qt](http://qt-project.org/) (version 4 or 5; via the PyQt5,
178128
[PyQt4](http://wiki.python.org/moin/PyQt4) or
179129
[PySide](http://qt-project.org/wiki/PySide)), Python GUI backends are
180-
supported by PyPlot. (Obviously, you must have installed one of these
181-
toolkits for Python first.) By default, PyPlot picks one of these
130+
supported by PythonPlot. (Obviously, you must have installed one of these
131+
toolkits for Python first.) By default, PythonPlot picks one of these
182132
when it starts up (based on what you have installed), but you can
183133
force a specific toolkit to be chosen by importing the PyCall module
184134
and using its `pygui` function to set a Python backend *before*
185-
importing PyPlot:
135+
importing PythonPlot:
186136
```
187137
using PyCall
188138
pygui(gui)
189-
using PyPlot
139+
using PythonPlot
190140
```
191141
where `gui` can currently be one of `:tk`, `:gtk3`, `:gtk`, `:qt5`, `:qt4`, `:qt`, or `:wx`. You can
192142
also set a default via the Matplotlib `rcParams['backend']` parameter in your
193143
[matplotlibrc](http://matplotlib.org/users/customizing.html) file.
194144

195145
## Color maps
196146

197-
The PyPlot module also exports some functions and types based on the
147+
The PythonPlot module also exports some functions and types based on the
198148
[matplotlib.colors](http://matplotlib.org/api/colors_api.html) and
199149
[matplotlib.cm](http://matplotlib.org/api/cm_api.html) modules to
200150
simplify management of color maps (which are used to assign values to
@@ -223,7 +173,7 @@ try `get_cmaps()`!
223173

224174
## 3d Plotting
225175

226-
The PyPlot package also imports functions from Matplotlib's
176+
The PythonPlot package also imports functions from Matplotlib's
227177
[mplot3d](http://matplotlib.org/mpl_toolkits/mplot3d/) toolkit.
228178
Unlike Matplotlib, however, you can create 3d plots directly without
229179
first creating an
@@ -260,7 +210,7 @@ interpreting the dollar signs and backslashes itself; for example, the
260210
LaTeX equation `$\alpha + \beta$` would be the literal string
261211
`"\$\\alpha + \\beta\$"` in Julia.
262212

263-
To simplify this, PyPlot uses the [LaTeXStrings package](https://github.com/stevengj/LaTeXStrings.jl) to provide a new `LaTeXString` type that
213+
To simplify this, PythonPlot uses the [LaTeXStrings package](https://github.com/stevengj/LaTeXStrings.jl) to provide a new `LaTeXString` type that
264214
be constructed via `L"...."` without escaping backslashes or dollar
265215
signs. For example, one can simply write `L"$\alpha + \beta$"` for the
266216
abovementioned equation, and thus you can do things like:
@@ -276,21 +226,21 @@ information.
276226
## SVG output in IJulia
277227

278228
By default, plots in IJulia are sent to the notebook as PNG images.
279-
Optionally, you can tell PyPlot to display plots in the browser as
229+
Optionally, you can tell PythonPlot to display plots in the browser as
280230
[SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) images,
281231
which have the advantage of being resolution-independent (so that they
282232
display without pixellation at high-resolutions, for example if you
283233
convert an IJulia notebook to PDF), by running:
284234
```
285-
PyPlot.svg(true)
235+
PythonPlot.svg(true)
286236
```
287237
This is not the default because SVG plots in the browser are much
288238
slower to display (especially for complex plots) and may display
289-
inaccurately in some browsers with buggy SVG support. The `PyPlot.svg()`
239+
inaccurately in some browsers with buggy SVG support. The `PythonPlot.svg()`
290240
method returns whether SVG display is currently enabled.
291241

292242
Note that this is entirely separate from manually exporting plots to SVG
293-
or any other format. Regardless of whether PyPlot uses SVG for
243+
or any other format. Regardless of whether PythonPlot uses SVG for
294244
browser display, you can export a plot to SVG at any time by using the
295245
Matplotlib
296246
[savefig](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig)
@@ -299,11 +249,8 @@ command, e.g. `savefig("plot.svg")`.
299249
## Modifying matplotlib.rcParams
300250
You can mutate the `rcParams` dictionary that Matplotlib uses for global parameters following this example:
301251
```jl
302-
rcParams = PyPlot.PyDict(PyPlot.matplotlib."rcParams")
303-
rcParams["font.size"] = 15
252+
PythonPlot.matplotlib.rcParams["font.size"] = 15
304253
```
305-
(If you instead used `PyPlot.matplotlib.rcParams`, PyCall would make a copy of the dictionary
306-
so that the Python `rcParams` wouldn't be modified.)
307254

308255
## Author
309256

0 commit comments

Comments
 (0)