Skip to content

Commit 2b5673e

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
TYP: annotate plotting based on _get_axe_freq (pandas-dev#35960)
1 parent 766bbad commit 2b5673e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pandas/plotting/_matplotlib/core.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import List, Optional
2+
from typing import TYPE_CHECKING, List, Optional
33
import warnings
44

55
from matplotlib.artist import Artist
@@ -43,6 +43,9 @@
4343
table,
4444
)
4545

46+
if TYPE_CHECKING:
47+
from matplotlib.axes import Axes
48+
4649

4750
class MPLPlot:
4851
"""
@@ -1147,7 +1150,7 @@ def _plot(cls, ax, x, y, style=None, column_num=None, stacking_id=None, **kwds):
11471150
return lines
11481151

11491152
@classmethod
1150-
def _ts_plot(cls, ax, x, data, style=None, **kwds):
1153+
def _ts_plot(cls, ax: "Axes", x, data, style=None, **kwds):
11511154
from pandas.plotting._matplotlib.timeseries import (
11521155
_decorate_axes,
11531156
_maybe_resample,

pandas/plotting/_matplotlib/timeseries.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
from pandas.tseries.frequencies import get_period_alias, is_subperiod, is_superperiod
2525

2626
if TYPE_CHECKING:
27-
from pandas import Index, Series # noqa:F401
27+
from matplotlib.axes import Axes
2828

29+
from pandas import Index, Series # noqa:F401
2930

3031
# ---------------------------------------------------------------------
3132
# Plotting functions and monkey patches
3233

3334

34-
def _maybe_resample(series: "Series", ax, kwargs):
35+
def _maybe_resample(series: "Series", ax: "Axes", kwargs):
3536
# resample against axes freq if necessary
3637
freq, ax_freq = _get_freq(ax, series)
3738

@@ -74,7 +75,7 @@ def _is_sup(f1: str, f2: str) -> bool:
7475
)
7576

7677

77-
def _upsample_others(ax, freq, kwargs):
78+
def _upsample_others(ax: "Axes", freq, kwargs):
7879
legend = ax.get_legend()
7980
lines, labels = _replot_ax(ax, freq, kwargs)
8081
_replot_ax(ax, freq, kwargs)
@@ -97,7 +98,7 @@ def _upsample_others(ax, freq, kwargs):
9798
ax.legend(lines, labels, loc="best", title=title)
9899

99100

100-
def _replot_ax(ax, freq, kwargs):
101+
def _replot_ax(ax: "Axes", freq, kwargs):
101102
data = getattr(ax, "_plot_data", None)
102103

103104
# clear current axes and data
@@ -127,7 +128,7 @@ def _replot_ax(ax, freq, kwargs):
127128
return lines, labels
128129

129130

130-
def _decorate_axes(ax, freq, kwargs):
131+
def _decorate_axes(ax: "Axes", freq, kwargs):
131132
"""Initialize axes for time-series plotting"""
132133
if not hasattr(ax, "_plot_data"):
133134
ax._plot_data = []
@@ -143,7 +144,7 @@ def _decorate_axes(ax, freq, kwargs):
143144
ax.date_axis_info = None
144145

145146

146-
def _get_ax_freq(ax):
147+
def _get_ax_freq(ax: "Axes"):
147148
"""
148149
Get the freq attribute of the ax object if set.
149150
Also checks shared axes (eg when using secondary yaxis, sharex=True
@@ -174,7 +175,7 @@ def _get_period_alias(freq) -> Optional[str]:
174175
return freq
175176

176177

177-
def _get_freq(ax, series: "Series"):
178+
def _get_freq(ax: "Axes", series: "Series"):
178179
# get frequency from data
179180
freq = getattr(series.index, "freq", None)
180181
if freq is None:
@@ -192,7 +193,7 @@ def _get_freq(ax, series: "Series"):
192193
return freq, ax_freq
193194

194195

195-
def _use_dynamic_x(ax, data: "FrameOrSeriesUnion") -> bool:
196+
def _use_dynamic_x(ax: "Axes", data: FrameOrSeriesUnion) -> bool:
196197
freq = _get_index_freq(data.index)
197198
ax_freq = _get_ax_freq(ax)
198199

@@ -234,7 +235,7 @@ def _get_index_freq(index: "Index") -> Optional[BaseOffset]:
234235
return freq
235236

236237

237-
def _maybe_convert_index(ax, data):
238+
def _maybe_convert_index(ax: "Axes", data):
238239
# tsplot converts automatically, but don't want to convert index
239240
# over and over for DataFrames
240241
if isinstance(data.index, (ABCDatetimeIndex, ABCPeriodIndex)):
@@ -264,7 +265,7 @@ def _maybe_convert_index(ax, data):
264265
# Do we need the rest for convenience?
265266

266267

267-
def _format_coord(freq, t, y):
268+
def _format_coord(freq, t, y) -> str:
268269
time_period = Period(ordinal=int(t), freq=freq)
269270
return f"t = {time_period} y = {y:8f}"
270271

0 commit comments

Comments
 (0)