REF: simplify cycling through colors by ivanovmg · Pull Request #37664 · pandas-dev/pandas (original) (raw)
I tried to address #37604, but it turns out that on some occasions it is absolutely mandatory to return more colors than num_colors says.
In particular, this is the test that cannot be updated and the underlying code cannot be easily modified as well.
pandas/tests/plotting/test_frame.py
...
class TestDataFramePlots(TestPlotBase):
...
def test_bar_user_colors(self):
df = DataFrame(
{"A": range(4), "B": range(1, 5), "color": ["red", "blue", "blue", "red"]}
)
# This should *only* work when `y` is specified, else
# we use one color per column
ax = df.plot.bar(y="A", color=df["color"])
result = [p.get_facecolor() for p in ax.patches]
expected = [
(1.0, 0.0, 0.0, 1.0),
(0.0, 0.0, 1.0, 1.0),
(0.0, 0.0, 1.0, 1.0),
(1.0, 0.0, 0.0, 1.0),
]
assert result == expected
When "y" is specified, then the number of colors must be the length of the series df["A"],
but inside the BarPlot class there is no opportunity to check for that condition.
So, let get_standard_colors return maximum of num_colors and len(colors).
This way all tests work without modifications,
just minor improvement in the cycling function.