54
54
)
55
55
from pandas .core .dtypes .missing import isna
56
56
57
+ from pandas import Series
57
58
import pandas .core .common as com
58
59
from pandas .util .version import Version
59
60
64
65
from pandas .plotting ._matplotlib .misc import unpack_single_str_list
65
66
from pandas .plotting ._matplotlib .style import get_standard_colors
66
67
from pandas .plotting ._matplotlib .timeseries import (
67
- decorate_axes ,
68
68
format_dateaxis ,
69
69
maybe_convert_index ,
70
- maybe_resample ,
70
+ prepare_ts_data ,
71
71
use_dynamic_x ,
72
72
)
73
73
from pandas .plotting ._matplotlib .tools import (
95
95
from pandas import (
96
96
DataFrame ,
97
97
Index ,
98
- Series ,
99
98
)
100
99
101
100
@@ -288,6 +287,21 @@ def __init__(
288
287
289
288
self .data = self ._ensure_frame (self .data )
290
289
290
+ from pandas .plotting import plot_params
291
+
292
+ self .x_compat = plot_params ["x_compat" ]
293
+ if "x_compat" in self .kwds :
294
+ self .x_compat = bool (self .kwds .pop ("x_compat" ))
295
+
296
+ @final
297
+ def _is_ts_plot (self ) -> bool :
298
+ # this is slightly deceptive
299
+ return not self .x_compat and self .use_index and self ._use_dynamic_x ()
300
+
301
+ @final
302
+ def _use_dynamic_x (self ) -> bool :
303
+ return use_dynamic_x (self ._get_ax (0 ), self .data .index )
304
+
291
305
@final
292
306
@staticmethod
293
307
def _validate_sharex (sharex : bool | None , ax , by ) -> bool :
@@ -1324,10 +1338,17 @@ def __init__(
1324
1338
c = self .data .columns [c ]
1325
1339
self .c = c
1326
1340
1341
+ @register_pandas_matplotlib_converters
1327
1342
def _make_plot (self , fig : Figure ) -> None :
1328
1343
x , y , c , data = self .x , self .y , self .c , self .data
1329
1344
ax = self .axes [0 ]
1330
1345
1346
+ x_data = Series (index = data [x ])
1347
+ if use_dynamic_x (ax , x_data .index ):
1348
+ x_data = maybe_convert_index (ax , x_data )
1349
+ freq , x_data = prepare_ts_data (x_data , ax , self .kwds )
1350
+ x_data = x_data .index
1351
+
1331
1352
c_is_column = is_hashable (c ) and c in self .data .columns
1332
1353
1333
1354
color_by_categorical = c_is_column and isinstance (
@@ -1344,7 +1365,7 @@ def _make_plot(self, fig: Figure) -> None:
1344
1365
else :
1345
1366
label = None
1346
1367
1347
- # if a list of non color strings is passed in as c, color points
1368
+ # if a list of non- color strings is passed in as c, color points
1348
1369
# by uniqueness of the strings, such same strings get same color
1349
1370
create_colors = not self ._are_valid_colors (c_values )
1350
1371
if create_colors :
@@ -1360,7 +1381,7 @@ def _make_plot(self, fig: Figure) -> None:
1360
1381
)
1361
1382
1362
1383
scatter = ax .scatter (
1363
- data [ x ] .values ,
1384
+ x_data .values ,
1364
1385
data [y ].values ,
1365
1386
c = c_values ,
1366
1387
label = label ,
@@ -1520,23 +1541,9 @@ def _kind(self) -> Literal["line", "area", "hist", "kde", "box"]:
1520
1541
return "line"
1521
1542
1522
1543
def __init__ (self , data , ** kwargs ) -> None :
1523
- from pandas .plotting import plot_params
1524
-
1525
1544
MPLPlot .__init__ (self , data , ** kwargs )
1526
1545
if self .stacked :
1527
1546
self .data = self .data .fillna (value = 0 )
1528
- self .x_compat = plot_params ["x_compat" ]
1529
- if "x_compat" in self .kwds :
1530
- self .x_compat = bool (self .kwds .pop ("x_compat" ))
1531
-
1532
- @final
1533
- def _is_ts_plot (self ) -> bool :
1534
- # this is slightly deceptive
1535
- return not self .x_compat and self .use_index and self ._use_dynamic_x ()
1536
-
1537
- @final
1538
- def _use_dynamic_x (self ) -> bool :
1539
- return use_dynamic_x (self ._get_ax (0 ), self .data )
1540
1547
1541
1548
def _make_plot (self , fig : Figure ) -> None :
1542
1549
if self ._is_ts_plot ():
@@ -1626,15 +1633,8 @@ def _ts_plot(self, ax: Axes, x, data: Series, style=None, **kwds):
1626
1633
# accept x to be consistent with normal plot func,
1627
1634
# x is not passed to tsplot as it uses data.index as x coordinate
1628
1635
# column_num must be in kwds for stacking purpose
1629
- freq , data = maybe_resample (data , ax , kwds )
1636
+ freq , data = prepare_ts_data (data , ax , kwds )
1630
1637
1631
- # Set ax with freq info
1632
- decorate_axes (ax , freq )
1633
- # digging deeper
1634
- if hasattr (ax , "left_ax" ):
1635
- decorate_axes (ax .left_ax , freq )
1636
- if hasattr (ax , "right_ax" ):
1637
- decorate_axes (ax .right_ax , freq )
1638
1638
# TODO #54485
1639
1639
ax ._plot_data .append ((data , self ._kind , kwds )) # type: ignore[attr-defined]
1640
1640
0 commit comments