Skip to content

Commit bd13307

Browse files
committed
BUG: fix line plot color assignment issues from 0.8.0 close #1711
1 parent cf673a4 commit bd13307

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pandas 0.8.2
4545

4646
**Bug fixes**
4747

48+
- Fix matplotlib auto-color assignment when no custom spectrum passed. Also
49+
respect passed color keyword argument (#1711)
4850
- Fix critical DatetimeIndex.union bugs (#1730, #1719, #1745, #1702)
4951
- Fix critical DatetimeIndex.intersection bug with unanchored offsets (#1708)
5052
- Fix MM-YYYY time series indexing case (#1672)

pandas/tools/plotting.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def _get_style(self, i, col_name):
793793
else:
794794
style = self.style
795795

796-
return style
796+
return style or None
797797

798798
class KdePlot(MPLPlot):
799799
def __init__(self, data, **kwargs):
@@ -813,12 +813,19 @@ def _make_plot(self):
813813
ind = np.linspace(min(y) - 0.5 * sample_range,
814814
max(y) + 0.5 * sample_range, 1000)
815815
ax.set_ylabel("Density")
816-
plotf(ax, ind, gkde.evaluate(ind), style, label=label, **self.kwds)
816+
817+
y = gkde.evaluate(ind)
818+
kwds = self.kwds.copy()
819+
kwds['label'] = label
820+
if style is None:
821+
args = (ax, ind, y)
822+
else:
823+
args = (ax, ind, y, style)
824+
825+
plotf(*args, **kwds)
817826
ax.grid(self.grid)
818827

819828
def _post_plot_logic(self):
820-
df = self.data
821-
822829
if self.subplots and self.legend:
823830
for ax in self.axes:
824831
ax.legend(loc='best')
@@ -871,19 +878,25 @@ def _make_plot(self):
871878
import matplotlib.pyplot as plt
872879
cycle = ''.join(plt.rcParams.get('axes.color_cycle',
873880
list('bgrcmyk')))
881+
has_colors = 'colors' in self.kwds
874882
colors = self.kwds.pop('colors', cycle)
875883
lines = []
876884
labels = []
877885
x = self._get_xticks(convert_period=True)
878886

887+
def _maybe_add_color(kwargs, style, i):
888+
if (has_colors and
889+
(style is None or re.match('[a-z]+', style) is None)):
890+
kwargs['color'] = colors[i % len(colors)]
891+
879892
plotf = self._get_plot_function()
880893

881894
for i, (label, y) in enumerate(self._iter_data()):
882895
ax = self._get_ax(i)
883896
style = self._get_style(i, label)
884897
kwds = self.kwds.copy()
885-
if re.match('[a-z]+', style) is None:
886-
kwds['color'] = colors[i % len(colors)]
898+
899+
_maybe_add_color(kwds, style, i)
887900

888901
label = _stringify(label)
889902

@@ -892,7 +905,13 @@ def _make_plot(self):
892905
y = np.ma.array(y)
893906
y = np.ma.masked_where(mask, y)
894907

895-
newline = plotf(ax, x, y, style, label=label, **kwds)[0]
908+
kwds['label'] = label
909+
if style is None:
910+
args = (ax, x, y)
911+
else:
912+
args = (ax, x, y, style)
913+
914+
newline = plotf(*args, **kwds)[0]
896915
lines.append(newline)
897916
leg_label = label
898917
if self.mark_right and self.on_right(i):
@@ -907,12 +926,19 @@ def _make_ts_plot(self, data, **kwargs):
907926
import matplotlib.pyplot as plt
908927
kwargs = kwargs.copy()
909928
cycle = ''.join(plt.rcParams.get('axes.color_cycle', list('bgrcmyk')))
929+
930+
has_colors = 'colors' in kwargs
910931
colors = kwargs.pop('colors', ''.join(cycle))
911932

912933
plotf = self._get_plot_function()
913934
lines = []
914935
labels = []
915936

937+
def _maybe_add_color(kwargs, style, i):
938+
if (has_colors and
939+
(style is None or re.match('[a-z]+', style) is None)):
940+
kwargs['color'] = colors[i % len(colors)]
941+
916942
def to_leg_label(label, i):
917943
if self.mark_right and self.on_right(i):
918944
return label + ' (right)'
@@ -922,8 +948,8 @@ def to_leg_label(label, i):
922948
ax = self._get_ax(0) #self.axes[0]
923949
style = self.style or ''
924950
label = com._stringify(self.label)
925-
if re.match('[a-z]+', style) is None:
926-
kwargs['color'] = colors[0]
951+
952+
_maybe_add_color(kwargs, style, 0)
927953

928954
newlines = tsplot(data, plotf, ax=ax, label=label, style=self.style,
929955
**kwargs)
@@ -937,8 +963,8 @@ def to_leg_label(label, i):
937963
ax = self._get_ax(i)
938964
style = self._get_style(i, col)
939965
kwds = kwargs.copy()
940-
if re.match('[a-z]+', style) is None:
941-
kwds['color'] = colors[i % len(colors)]
966+
967+
_maybe_add_color(kwargs, style, i)
942968

943969
newlines = tsplot(data[col], plotf, ax=ax, label=label,
944970
style=style, **kwds)

0 commit comments

Comments
 (0)