PyPlot: How to change the aspect ratio of a plot?

2,466 views
Skip to first unread message

Daniel Carrera

unread,
Aug 26, 2015, 8:01:07 AM8/26/15
to julia-users
Hello,

I am using PyPlot and I cannot figure out how to change the aspect ratio of a plot. I googled for some answers and I found a thread from Python users about aspect ratios in Matplotlib:


I am trying to make sense of that thread, and imagine how to do something similar in Julia, but so far my attempts have failed. The closest I got is to make a "subplot", but I don't know how to plot inside a subplot. Here is what I tried:

julia> using PyPlot
INFO: Loading help data...

julia> aspect
ERROR: aspect not defined

julia> ax = axes()
PyObject <matplotlib.axes.AxesSubplot object at 0x7f5c378fc4d0>

julia> methods(ax)
ERROR: `methods` has no method matching methods(::PyObject)

julia> fig = figure(2)
Figure(PyObject <matplotlib.figure.Figure object at 0x7f5c376cda10>)

julia> methods(fig)
ERROR: `methods` has no method matching methods(::Figure)

julia> subplot(111, aspect=3)
PyObject <matplotlib.axes.AxesSubplot object at 0x7f5c376d3090>


AHA! So the last line correctly creates a plot with the desired aspect ratio. But I don't know where to go from there. The moment I try to plot anything, the aspect ratio resets. I do have the basic idea of how subplots work:

--------------------------
using PyPlot

x = linspace(0,2pi);

subplot(211)
plot(x,sin(x))
subplot(212)
plot(x,cos(x))
--------------------------

So I was expecting to be able to do this:

--------------------------
using PyPlot

x = linspace(0,2pi);

subplot(111, aspect=3)
plot(x,sin(x))
--------------------------

I was expecting that this would give me a plot that looks like a very tall sine wave. But it doesn't. The moment I run the plot() command, the aspect ratio resets.


Can anyone help me?

Cheers,
Daniel.

Steven G. Johnson

unread,
Aug 26, 2015, 10:46:18 AM8/26/15
to julia-users
The answers in the stackoverflow thread you linked work for me, e.g.

w, h = plt[:figaspect](0.5)
figure(figsize=(w,h))
plot(rand(10))

Daniel Carrera

unread,
Aug 26, 2015, 10:59:34 AM8/26/15
to julia...@googlegroups.com
I often have trouble converting from Python+Matplotlib to Julia+PyPlot. Even with your code example, I don't really know what to do. Where do I get that "plt" variable? For future reference, how would I know that the figaspect() function would be located in "plt[:figaspect]". Is there a general rule I can follow?

I'm sure you are busy, but is there a chance you might write a tutorial for PyPlot, showing how to convert Python code one finds around the web into equivalent Julia code?

Thanks for the help.

Cheers,
Daniel.

Nils Gudat

unread,
Aug 26, 2015, 11:10:31 AM8/26/15
to julia-users

Nils Gudat

unread,
Aug 26, 2015, 11:14:10 AM8/26/15
to julia-users
FWIW, I always think the fig/ax syntax aligns best with Python, e.g.

fig, ax = PyPlot.subplots(2,2, ...)
ax[1,1][:plot](..., linestyle = ":", label = "...")
ax[1,1][:legend](loc="best")
ax[1,2][:hist](..., alpha = 0.5, label = "...")
fig[:title]("Some title")


with this syntax, you can more or less convert Python code to Julia 1-for-1 by just replacing all the dot method calls with [:method]-calls.

Daniel Carrera

unread,
Aug 26, 2015, 11:28:19 AM8/26/15
to julia...@googlegroups.com
Thanks. That is very helpful.

Cheers,
Daniel.
Reply all
Reply to author
Forward
0 new messages