Skip to content

TYP: annotate plotting._matplotlib.tools #35968

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
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4c5eddd
REF: remove unnecesary try/except
jbrockmendel Aug 21, 2020
c632c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
9e64be3
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
42649fb
TST: add test for agg on ordered categorical cols (#35630)
mathurk1 Aug 21, 2020
47121dd
TST: resample does not yield empty groups (#10603) (#35799)
tkmz-n Aug 21, 2020
1decb3e
revert accidental rebase
jbrockmendel Aug 22, 2020
57c5dd3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 22, 2020
a358463
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
ffa7ad7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
e5e98d4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
408db5a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
d3493cf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
75a805a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
9f61070
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
2d10f6e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
3e20187
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
e27d07f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 27, 2020
c52bed4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 27, 2020
b69d4d7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 28, 2020
1c5f8fd
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 28, 2020
061c9e2
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 28, 2020
78dfa6e
TYP: plotting._matplotlib.tools
jbrockmendel Aug 28, 2020
7a8dacb
Merge branch 'master' of https://github.com/pandas-dev/pandas into an…
jbrockmendel Aug 29, 2020
420206e
List->Iterable
jbrockmendel Aug 29, 2020
a94acea
Merge branch 'master' of https://github.com/pandas-dev/pandas into an…
jbrockmendel Aug 30, 2020
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
33 changes: 25 additions & 8 deletions pandas/plotting/_matplotlib/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# being a bit too dynamic
from math import ceil
from typing import TYPE_CHECKING, Tuple
from typing import TYPE_CHECKING, Iterable, List, Sequence, Tuple, Union
import warnings

import matplotlib.table
Expand All @@ -15,10 +15,13 @@
from pandas.plotting._matplotlib import compat

if TYPE_CHECKING:
from matplotlib.axes import Axes
from matplotlib.axis import Axis
from matplotlib.lines import Line2D # noqa:F401
from matplotlib.table import Table


def format_date_labels(ax, rot):
def format_date_labels(ax: "Axes", rot):
# mini version of autofmt_xdate
for label in ax.get_xticklabels():
label.set_ha("right")
Expand Down Expand Up @@ -278,7 +281,7 @@ def _subplots(
return fig, axes


def _remove_labels_from_axis(axis):
def _remove_labels_from_axis(axis: "Axis"):
for t in axis.get_majorticklabels():
t.set_visible(False)

Expand All @@ -294,7 +297,15 @@ def _remove_labels_from_axis(axis):
axis.get_label().set_visible(False)


def _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey):
def _handle_shared_axes(
axarr: Iterable["Axes"],
nplots: int,
naxes: int,
nrows: int,
ncols: int,
sharex: bool,
sharey: bool,
):
if nplots > 1:
if compat._mpl_ge_3_2_0():
row_num = lambda x: x.get_subplotspec().rowspan.start
Expand Down Expand Up @@ -340,15 +351,21 @@ def _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey):
_remove_labels_from_axis(ax.yaxis)


def _flatten(axes):
def _flatten(axes: Union["Axes", Sequence["Axes"]]) -> Sequence["Axes"]:
Copy link
Member

Choose a reason for hiding this comment

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

return is 1d np.ndarray? axes can also be (np.ndarray, IndexClass)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Index.ravel returns an ndarray, so we always get ndarray here

Copy link
Member

Choose a reason for hiding this comment

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

can you update return type

Copy link
Member Author

Choose a reason for hiding this comment

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

to what?

Copy link
Member

Choose a reason for hiding this comment

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

np.ndarray?

Copy link
Member Author

Choose a reason for hiding this comment

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

If there were a way to indicate ndarray["Axes"] I'd agree. until then, it is the "Axes" that is most relevant

Copy link
Member

Choose a reason for hiding this comment

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

so mypy is green since np.ndarray resolves to Any, so Any is compatible with Sequence["Axes"].

I guess that we could add result = cast(Sequence["Axes"], result) when numpy types are available.

if not is_list_like(axes):
return np.array([axes])
elif isinstance(axes, (np.ndarray, ABCIndexClass)):
return axes.ravel()
return np.array(axes)


def _set_ticks_props(axes, xlabelsize=None, xrot=None, ylabelsize=None, yrot=None):
def _set_ticks_props(
axes: Union["Axes", Sequence["Axes"]],
xlabelsize=None,
xrot=None,
ylabelsize=None,
yrot=None,
):
import matplotlib.pyplot as plt

for ax in _flatten(axes):
Expand All @@ -363,7 +380,7 @@ def _set_ticks_props(axes, xlabelsize=None, xrot=None, ylabelsize=None, yrot=Non
return axes


def _get_all_lines(ax):
def _get_all_lines(ax: "Axes") -> List["Line2D"]:
lines = ax.get_lines()

if hasattr(ax, "right_ax"):
Expand All @@ -375,7 +392,7 @@ def _get_all_lines(ax):
return lines


def _get_xlim(lines) -> Tuple[float, float]:
def _get_xlim(lines: List["Line2D"]) -> Tuple[float, float]:
left, right = np.inf, -np.inf
for l in lines:
x = l.get_xdata(orig=False)
Expand Down