Skip to content

Commit 390fc86

Browse files
author
Moritz Schreiber
committed
Add Tests
1 parent 68f2551 commit 390fc86

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

pandas/plotting/_core.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,10 @@ def bar(self, x=None, y=None, **kwargs):
11321132
if c is not None and color is not None:
11331133
raise TypeError("Specify exactly one of `c` and `color`")
11341134
if c is not None or color is not None:
1135-
kwargs.setdefault('c', color or c)
1135+
if color is not None:
1136+
kwargs.setdefault('color', color)
1137+
else:
1138+
kwargs.setdefault('color', c)
11361139

11371140
return self(kind="bar", x=x, y=y, **kwargs)
11381141

@@ -1225,7 +1228,10 @@ def barh(self, x=None, y=None, **kwargs):
12251228
if c is not None and color is not None:
12261229
raise TypeError("Specify exactly one of `c` and `color`")
12271230
if c is not None or color is not None:
1228-
kwargs.setdefault('c', color or c)
1231+
if color is not None:
1232+
kwargs.setdefault('color', color)
1233+
else:
1234+
kwargs.setdefault('color', c)
12291235

12301236
return self(kind="barh", x=x, y=y, **kwargs)
12311237

@@ -1684,14 +1690,20 @@ def scatter(self, x, y, **kwargs):
16841690
if s is not None and size is not None:
16851691
raise TypeError("Specify exactly one of `s` and `size`")
16861692
if s is not None or size is not None:
1687-
kwargs.setdefault('s', s or size)
1693+
if s is not None:
1694+
kwargs.setdefault('s', s)
1695+
else:
1696+
kwargs.setdefault('s', size)
16881697

16891698
c = kwargs.pop('c', None)
16901699
color = kwargs.pop('color', None)
16911700
if c is not None and color is not None:
16921701
raise TypeError("Specify exactly one of `c` and `color`")
16931702
if c is not None or color is not None:
1694-
kwargs.setdefault('c', c or color)
1703+
if c is not None:
1704+
kwargs.setdefault('c', c)
1705+
else:
1706+
kwargs.setdefault('c', color)
16951707

16961708
return self(kind="scatter", x=x, y=y, **kwargs)
16971709

pandas/tests/plotting/frame/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ def test_plot_scatter(self):
674674
with pytest.raises(TypeError, match=msg):
675675
df.plot.scatter(y="y")
676676

677+
with pytest.raises(TypeError, match="Specify exactly one of `s` and `size`"):
678+
df.plot.scatter(x="x", y="y", s=2, size=2)
679+
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):
680+
df.plot.scatter(x="a", y="b", c="red", color="green")
681+
682+
default_colors = self._unpack_cycler(self.plt.rcParams)
683+
default_colors = self._unpack_cycler(self.plt.rcParams)
684+
677685
# GH 6951
678686
axes = df.plot(x="x", y="y", kind="scatter", subplots=True)
679687
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
@@ -827,6 +835,10 @@ def test_plot_bar(self):
827835
_check_plot_works(df.plot.bar)
828836

829837
df = DataFrame({"a": [0, 1], "b": [1, 0]})
838+
839+
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):
840+
df.plot.bar(x="a", y="b", c="red", color="green")
841+
830842
ax = _check_plot_works(df.plot.bar)
831843
self._check_ticks_props(ax, xrot=90)
832844

pandas/tests/plotting/frame/test_frame_color.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ def test_bar_colors(self):
123123
self._check_colors(ax.patches[::5], facecolors=custom_colors)
124124
tm.close()
125125

126+
custom_colors = "rgcby"
127+
ax = df.plot.bar(c=custom_colors)
128+
self._check_colors(ax.patches[::5], facecolors=custom_colors)
129+
tm.close()
130+
126131
from matplotlib import cm
127132

128133
# Test str -> colormap functionality
@@ -141,6 +146,10 @@ def test_bar_colors(self):
141146
self._check_colors([ax.patches[0]], facecolors=["DodgerBlue"])
142147
tm.close()
143148

149+
ax = df.loc[:, [0]].plot.bar(c="DodgerBlue")
150+
self._check_colors([ax.patches[0]], facecolors=["DodgerBlue"])
151+
tm.close()
152+
144153
ax = df.plot(kind="bar", color="green")
145154
self._check_colors(ax.patches[::5], facecolors=["green"] * 5)
146155
tm.close()
@@ -151,15 +160,20 @@ def test_bar_user_colors(self):
151160
)
152161
# This should *only* work when `y` is specified, else
153162
# we use one color per column
154-
ax = df.plot.bar(y="A", color=df["color"])
155-
result = [p.get_facecolor() for p in ax.patches]
156163
expected = [
157164
(1.0, 0.0, 0.0, 1.0),
158165
(0.0, 0.0, 1.0, 1.0),
159166
(0.0, 0.0, 1.0, 1.0),
160167
(1.0, 0.0, 0.0, 1.0),
161168
]
169+
170+
ax = df.plot.bar(y="A", color=df["color"])
171+
result = [p.get_facecolor() for p in ax.patches]
162172
assert result == expected
173+
174+
ax = df.plot.bar(y="A", c=df["color"])
175+
result = [p.get_facecolor() for p in ax.patches]
176+
assert result == expected
163177

164178
def test_if_scatterplot_colorbar_affects_xaxis_visibility(self):
165179
# addressing issue #10611, to ensure colobar does not
@@ -223,6 +237,9 @@ def test_scatter_with_c_column_name_with_colors(self, cmap):
223237
ax = df.plot.scatter(x=0, y=1, c="species", cmap=cmap)
224238
assert ax.collections[0].colorbar is None
225239

240+
ax = df.plot.scatter(x=0, y=1, color="species", cmap=cmap)
241+
assert ax.collections[0].colorbar is None
242+
226243
def test_scatter_colors(self):
227244
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})
228245
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):

pandas/tests/plotting/test_series.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,20 @@ def test_bar_ignore_index(self):
270270

271271
def test_bar_user_colors(self):
272272
s = Series([1, 2, 3, 4])
273-
ax = s.plot.bar(color=["red", "blue", "blue", "red"])
274-
result = [p.get_facecolor() for p in ax.patches]
273+
275274
expected = [
276275
(1.0, 0.0, 0.0, 1.0),
277276
(0.0, 0.0, 1.0, 1.0),
278277
(0.0, 0.0, 1.0, 1.0),
279278
(1.0, 0.0, 0.0, 1.0),
280279
]
280+
281+
ax = s.plot.bar(color=["red", "blue", "blue", "red"])
282+
result = [p.get_facecolor() for p in ax.patches]
283+
assert result == expected
284+
285+
ax = s.plot.bar(c=["red", "blue", "blue", "red"])
286+
result = [p.get_facecolor() for p in ax.patches]
281287
assert result == expected
282288

283289
def test_rotation(self):

0 commit comments

Comments
 (0)