[Matplotlib-users] axes properties

vincent.adrien at gmail.com vincent.adrien at gmail.com
Wed Mar 22 15:49:26 EDT 2017

Hello Jean-Philippe,

>From my point of view, coding style only (really) matters if you intend to share
your code with other people. If you are and will ever be the only writer and
reader of your own code, then I would be inclined to say that as long as you are
comfortable with it, you can code as you wish ;). A remaining problem that may
still remain though is if you go asking questions on forums, Stack Overflow,
etc., where people may expect you to write code that follows some guidelines. In
Python, the guidelines are commonly those from the
[PEP8](https://www.python.org/dev/peps/pep-0008/). A very loose summary and yet
quite sufficient for a day-to-day use might be:

- 'my_variable' and 'my_function' vs 'MyClass' for the naming schemes;
- '_nonpublic_method()' vs 'public_method()' for classe method names
- wrap long lines into smaller ones (usually < 80 characters);
- use 4 spaces (instead of tab) per indentation level;
- mostly use whitespaces around binary operators and except in a few situations:
    - my_function(arg_1, kwarg_1=val_1, kwarg_2=val_2) (no space around '=')
    - 'res = a*b +c*d' is better than 'res = a * b + c * d'
- my_dict = {key_1: item_1, key_2: item_2}, i.e. 'key<no_space>:<space> item'

Concerning Matplotlib conventions, I would say that the common use is to import
pyplot as 'plt', not 'pt'.
import matplotlib.pyplot as plt
It is true that we try in our current documentation to promote the
object-oriented interface (OO-interface), which usually make things clearer when
you write scripts (you explicitely know where the drawing will be performed for
# Either with the most recent wrapper:
fig, ax = plt.subplots()
ax. plot([0, 2, 1])

# or following the older but still perfectly OK fashion:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0, 2, 1])

However, at least for 2D plots, you can definitively use the more MATLAB-like
fashion, i.e.
plt.plot([0, 2, 1])
which will implicitly draw in the current Axes instance, and create one , as
well as a Figure one, if none already exists.

In the case of mplot3d, I may be wrong but I think that you have to use at least
partially the OO-interface. Except for a few points stated above, I would say
that your code snippet should still be perfectly OK for a long time :). Here is
a version with the small style fixes I would suggest:
import numpy as np
import matplotlib.pyplot as plt  # 'pt' <- 'plt'
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()  # 'pt' <- 'plt'
ax = fig.gca(projection='3d')
# yada yada yada
surf = ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)  # no space around '='

And if you really want concise code (for example during an interactive session),
I think you can achieve things that are rather close to MATLAB (at least from
what I remember), like:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Just for demonstration sake, we create dummy data with a convenient helper
from mpl_toolkits.mplot3d.axes3d import get_test_data
X, Y, Z = get_test_data(0.05)

# Straightforward case with a single 3D-plot

ax = plt.gca(projection='3d')  # a figure is created implicitly if none exists
ax.plot_wireframe(X, Y, Z)
plt.title("Single plot")  # <=> ax.set_title("Single plot")

# Straightforward case with 1x2 subplots.
plt.figure()  # new figure, just to avoid drawing in the previous one

ax = plt.subplot(121, projection='3d')
ax.plot_wireframe(X, Y, Z)
plt.title("1st subplot:\ndefault parameters")

ax = plt.subplot(122, projection='3d')
ax.plot_wireframe(X, Y, Z, cstride=10, rstride=5)
plt.title("2nd subplot:\ncustom parameters")


On 22/03/2017 18:57, Jean-Philippe Grivet wrote:
> Thanks a lot Benjamin and Vincent for your most kind and detailed answers.
> In reply to Vincent, I regret that the authors of Matplotkib did not pursue
> the wrapper idea and propose a function plot3d(x,y,z); it would make life simpler.
> I copied from the net (I can't remember where) the following code fragment:
> from mpl_toolkits.mplot3d import Axes3D
> import numpy as np
> import matplotlib.pyplot as pt
> fig = pt.figure()
> ax = fig.gca(projection='3d')
> ................
> surf = ax.plot_wireframe(X, Y, Z, rstride = 10,cstride = 10)
> which looks rather similar to Matlab syntax and works well for me.
>  Is that considered to be obsolete or in bad style ?
> Thanks again,
> Jean-Philippe

More information about the Matplotlib-users mailing list