Skip to content

Commit 7ae6b8e

Browse files
authored
REF: make BarPlot attributes cache_readonlys (#55878)
* REF: make BarPlot less stateful * less state
1 parent bf248a1 commit 7ae6b8e

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

pandas/plotting/_matplotlib/core.py

+40-18
Original file line numberDiff line numberDiff line change
@@ -1663,42 +1663,63 @@ def _kind(self) -> Literal["bar", "barh"]:
16631663
def orientation(self) -> PlottingOrientation:
16641664
return "vertical"
16651665

1666-
def __init__(self, data, **kwargs) -> None:
1666+
def __init__(
1667+
self,
1668+
data,
1669+
*,
1670+
align="center",
1671+
bottom=0,
1672+
left=0,
1673+
width=0.5,
1674+
position=0.5,
1675+
log=False,
1676+
**kwargs,
1677+
) -> None:
16671678
# we have to treat a series differently than a
16681679
# 1-column DataFrame w.r.t. color handling
16691680
self._is_series = isinstance(data, ABCSeries)
1670-
self.bar_width = kwargs.pop("width", 0.5)
1671-
pos = kwargs.pop("position", 0.5)
1672-
kwargs.setdefault("align", "center")
1681+
self.bar_width = width
1682+
self._align = align
1683+
self._position = position
16731684
self.tick_pos = np.arange(len(data))
16741685

1675-
bottom = kwargs.pop("bottom", 0)
1676-
left = kwargs.pop("left", 0)
16771686
if is_list_like(bottom):
16781687
bottom = np.array(bottom)
16791688
if is_list_like(left):
16801689
left = np.array(left)
16811690
self.bottom = bottom
16821691
self.left = left
16831692

1684-
self.log = kwargs.pop("log", False)
1693+
self.log = log
1694+
16851695
MPLPlot.__init__(self, data, **kwargs)
16861696

1697+
@cache_readonly
1698+
def ax_pos(self) -> np.ndarray:
1699+
return self.tick_pos - self.tickoffset
1700+
1701+
@cache_readonly
1702+
def tickoffset(self):
16871703
if self.stacked or self.subplots:
1688-
self.tickoffset = self.bar_width * pos
1689-
if kwargs["align"] == "edge":
1690-
self.lim_offset = self.bar_width / 2
1691-
else:
1692-
self.lim_offset = 0
1693-
elif kwargs["align"] == "edge":
1704+
return self.bar_width * self._position
1705+
elif self._align == "edge":
16941706
w = self.bar_width / self.nseries
1695-
self.tickoffset = self.bar_width * (pos - 0.5) + w * 0.5
1696-
self.lim_offset = w * 0.5
1707+
return self.bar_width * (self._position - 0.5) + w * 0.5
16971708
else:
1698-
self.tickoffset = self.bar_width * pos
1699-
self.lim_offset = 0
1709+
return self.bar_width * self._position
17001710

1701-
self.ax_pos = self.tick_pos - self.tickoffset
1711+
@cache_readonly
1712+
def lim_offset(self):
1713+
if self.stacked or self.subplots:
1714+
if self._align == "edge":
1715+
return self.bar_width / 2
1716+
else:
1717+
return 0
1718+
elif self._align == "edge":
1719+
w = self.bar_width / self.nseries
1720+
return w * 0.5
1721+
else:
1722+
return 0
17021723

17031724
# error: Signature of "_plot" incompatible with supertype "MPLPlot"
17041725
@classmethod
@@ -1749,6 +1770,7 @@ def _make_plot(self, fig: Figure) -> None:
17491770
start = 1
17501771
start = start + self._start_base
17511772

1773+
kwds["align"] = self._align
17521774
if self.subplots:
17531775
w = self.bar_width / 2
17541776
rect = self._plot(

0 commit comments

Comments
 (0)