Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Minor cleanups to pyplot. #11205

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 1 commit into from
May 9, 2018
Merged
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
58 changes: 24 additions & 34 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import inspect
from numbers import Number
import re
import sys
import time
import warnings
Expand Down Expand Up @@ -121,8 +122,7 @@ def install_repl_displayhook():
Install a repl display hook so that any stale figure are automatically
redrawn when control is returned to the repl.

This works with IPython terminals and kernels,
as well as vanilla python shells.
This works both with IPython and with vanilla python shells.
"""
global _IP_REGISTERED
global _INSTALL_FIG_OBSERVER
Expand Down Expand Up @@ -844,13 +844,13 @@ def axes(arg=None, **kwargs):

def delaxes(ax=None):
"""
Remove the given `Axes` *ax* from the current figure. If *ax* is *None*,
the current axes is removed. A KeyError is raised if the axes doesn't
exist.
Remove the `Axes` *ax* (defaulting to the current axes) from its figure.

A KeyError is raised if the axes doesn't exist.
"""
if ax is None:
ax = gca()
gcf().delaxes(ax)
ax.figure.delaxes(ax)


def sca(ax):
Expand All @@ -865,7 +865,7 @@ def sca(ax):
_pylab_helpers.Gcf.set_active(m)
m.canvas.figure.sca(ax)
return
raise ValueError("Axes instance argument was not found in a figure.")
raise ValueError("Axes instance argument was not found in a figure")


def gca(**kwargs):
Expand Down Expand Up @@ -992,7 +992,7 @@ def subplot(*args, **kwargs):
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, gridspec_kw=None, **fig_kw):
"""
Create a figure and a set of subplots
Create a figure and a set of subplots.

This utility wrapper makes it convenient to create common layouts of
subplots, including the enclosing figure object, in a single call.
Expand Down Expand Up @@ -1269,8 +1269,7 @@ def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
figure coordinate that the whole subplots area (including
labels) will fit into. Default is (0, 0, 1, 1).
"""
fig = gcf()
fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)


def box(on=None):
Expand Down Expand Up @@ -1885,53 +1884,44 @@ def colormaps():

def _setup_pyplot_info_docstrings():
"""
Generates the plotting and docstring.
Generates the plotting docstring.

These must be done after the entire module is imported, so it is
called from the end of this module, which is generated by
boilerplate.py.
"""
# Generate the plotting docstring
import re

def pad(s, l):
"""Pad string *s* to length *l*."""
if l < len(s):
return s[:l]
return s + ' ' * (l - len(s))

commands = get_plot_commands()

first_sentence = re.compile(r"(?:\s*).+?\.(?:\s+|$)", flags=re.DOTALL)

# Collect the first sentence of the docstring for all of the
# plotting commands.
rows = []
max_name = 0
max_summary = 0
max_name = len("Function")
max_summary = len("Description")
for name in commands:
doc = globals()[name].__doc__
summary = ''
if doc is not None:
match = first_sentence.match(doc)
if match is not None:
summary = match.group(0).strip().replace('\n', ' ')
summary = inspect.cleandoc(match.group(0)).replace('\n', ' ')
name = '`%s`' % name
rows.append([name, summary])
max_name = max(max_name, len(name))
max_summary = max(max_summary, len(summary))

lines = []
sep = '=' * max_name + ' ' + '=' * max_summary
lines.append(sep)
lines.append(' '.join([pad("Function", max_name),
pad("Description", max_summary)]))
lines.append(sep)
for name, summary in rows:
lines.append(' '.join([pad(name, max_name),
pad(summary, max_summary)]))
lines.append(sep)

separator = '=' * max_name + ' ' + '=' * max_summary
lines = [
separator,
'{:{}} {:{}}'.format('Function', max_name, 'Description', max_summary),
separator,
] + [
'{:{}} {:{}}'.format(name, max_name, summary, max_summary)
for name, summary in rows
] + [
separator,
]
plotting.__doc__ = '\n'.join(lines)


Expand Down