Skip to content

DOC: simplify API index #19727

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

Merged
merged 9 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<ul>
<li><a href="{{ pathto('users/installing') }}">Installation</a></li>
<li><a href="{{ pathto('contents') }}">Documentation</a></li>
<li><a href="{{ pathto('api/index') }}">API</a></li>
<li><a href="{{ pathto('gallery/index') }}">Examples</a></li>
<li><a href="{{ pathto('tutorials/index') }}">Tutorials</a></li>
<li><a href="{{ pathto('devel/index') }}">Contributing</a></li>
Expand Down
124 changes: 77 additions & 47 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
@@ -1,67 +1,46 @@
API Overview
============
API
===

.. toctree::
:hidden:

api_changes

.. contents:: :local:
For recent changes, see :doc:`api_changes`.

See also the :doc:`api_changes`.
When using the library you will typically create
:doc:`Figure <figure_api>` and :doc:`Axes <axes_api>` objects and
call their methods to add content and modify the apprearance.

Usage patterns
--------------

Below we describe several common approaches to plotting with Matplotlib.

The pyplot API
^^^^^^^^^^^^^^

`matplotlib.pyplot` is a collection of command style functions that make
Matplotlib work like MATLAB. Each pyplot function makes some change to a
figure: e.g., creates a figure, creates a plotting area in a figure, plots
some lines in a plotting area, decorates the plot with labels, etc.
- :doc:`figure_api`: axes creation, figure-level content
- :doc:`axes_api`: most plotting methods, Axes labels, access to
axis styling, etc.

`.pyplot` is mainly intended for interactive plots and simple cases of
programmatic plot generation.
Example: We create a Figure ``fig`` and Axes ``ax``. Then we call
methods on them to plot data, add axis labels and a figure title.

.. plot::
:include-source:
:align: center

Further reading:
import matplotlib.pyplot as plt
import numpy as np

- The `matplotlib.pyplot` function reference
- :doc:`/tutorials/introductory/pyplot`
- :ref:`Pyplot examples <pyplots_examples>`
x = np.arange(0, 4, 0.05)
y = np.sin(x*np.pi)

.. _api-index:
fig, ax = plt.subplots(figsize=(3,2), constrained_layout=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fig, ax = plt.subplots(figsize=(3,2), constrained_layout=True)
fig, ax = plt.subplots()

Keep it simple without parameters. While constrained_layout is nice, IMHO this is not the place to teach it (and the benefit for this simple plot is small).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but then the figure is too large, in my opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could try with

.. plot::
   :scale: 50 %

but I'm not sure if that will look good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wish there was a two-column view.... We could just include the code and the figure as an image as well. I do think the example is useful, though, to make it concrete what we are talking about.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, the ylabel is cutoff, so some formatting is necessary anyway.. Might as well keep what is above. I don't think we need to explain every incantation everywhere it appears.

ax.plot(x, y)
ax.set_xlabel('t [s]')
ax.set_ylabel('S [V]')
ax.set_title('Sine wave')
fig.set_facecolor('lightsteelblue')

The object-oriented API
^^^^^^^^^^^^^^^^^^^^^^^

At its core, Matplotlib is object-oriented. We recommend directly working
with the objects, if you need more control and customization of your plots.

In many cases you will create a `.Figure` and one or more
`~matplotlib.axes.Axes` using `.pyplot.subplots` and from then on only work
on these objects. However, it's also possible to create `.Figure`\ s
explicitly (e.g. when including them in GUI applications).

Further reading:

- `matplotlib.axes.Axes` and `matplotlib.figure.Figure` for an overview of
plotting functions.
- Most of the :ref:`examples <examples-index>` use the object-oriented approach
(except for the pyplot section)

The pylab API (disapproved)
^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: pylab
:no-members:

Modules
-------

Matplotlib consists of the following submodules:
Alphabetical list of modules:

.. toctree::
:maxdepth: 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:maxdepth: 1
:maxdepth: 1
:multicol-toc:

Maybe.
grafik

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unreleased? I get /Users/jklymak/matplotlib/doc/api/index.rst:45: WARNING: Error in "toctree" directive: unknown option: "multicol-toc". on sphinx 3.5.3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this should have been :class: multicol-toc.

multicol-toc is just a CSS class we have added to our mpl.css.

Copy link
Member Author

@jklymak jklymak Mar 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops searched the internet - didn't think to search our source code...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this does not seem to work. .. toctree: does not support a :class: attribute.

I've otherwise only used it for .. contents: so far. The above screenshot was a hacked CSS in the browser, because I didn't want to wait for a docs build.

Seems we have to live with the long list for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! I thought I was going crazy... Certainly adding a class directive to toctree seems like a good idea, but seems like an upstream issue.

Expand Down Expand Up @@ -147,3 +126,54 @@ Matplotlib. The following toolkits are included:
toolkits/axes_grid1.rst
toolkits/axisartist.rst
toolkits/axes_grid.rst


.. _usage_patterns:

Usage patterns
--------------

Below we describe several common approaches to plotting with Matplotlib.

The pyplot API
^^^^^^^^^^^^^^

`matplotlib.pyplot` is a collection of functions that make
Matplotlib work like MATLAB. Each pyplot function makes some change to a
figure: e.g., creates a figure, creates a plotting area in a figure, plots
some lines in a plotting area, decorates the plot with labels, etc.

`.pyplot` is mainly intended for interactive plots and simple cases of
programmatic plot generation.

Further reading:

- The `matplotlib.pyplot` function reference
- :doc:`/tutorials/introductory/pyplot`
- :ref:`Pyplot examples <pyplots_examples>`

.. _api-index:

The object-oriented API
^^^^^^^^^^^^^^^^^^^^^^^

At its core, Matplotlib is object-oriented. We recommend directly working
with the objects, if you need more control and customization of your plots.

In many cases you will create a `.Figure` and one or more
`~matplotlib.axes.Axes` using `.pyplot.subplots` and from then on only work
on these objects. However, it's also possible to create `.Figure`\ s
explicitly (e.g. when including them in GUI applications).

Further reading:

- `matplotlib.axes.Axes` and `matplotlib.figure.Figure` for an overview of
plotting functions.
- Most of the :ref:`examples <examples-index>` use the object-oriented approach
(except for the pyplot section)

The pylab API (disapproved)
^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: pylab
:no-members: