@@ -74,13 +74,22 @@ def setUp(self):
74
74
'weight' : random .normal (161 , 32 , size = n ),
75
75
'category' : random .randint (4 , size = n )})
76
76
77
- if str (mpl .__version__ ) >= LooseVersion ('1.4' ):
77
+ self .mpl_le_1_2_1 = plotting ._mpl_le_1_2_1 ()
78
+ self .mpl_ge_1_3_1 = plotting ._mpl_ge_1_3_1 ()
79
+ self .mpl_ge_1_4_0 = plotting ._mpl_ge_1_4_0 ()
80
+ self .mpl_ge_1_5_0 = plotting ._mpl_ge_1_5_0 ()
81
+
82
+ if self .mpl_ge_1_4_0 :
78
83
self .bp_n_objects = 7
79
84
else :
80
85
self .bp_n_objects = 8
86
+ if self .mpl_ge_1_5_0 :
87
+ # 1.5 added PolyCollections to legend handler
88
+ # so we have twice as many items.
89
+ self .polycollection_factor = 2
90
+ else :
91
+ self .polycollection_factor = 1
81
92
82
- self .mpl_le_1_2_1 = str (mpl .__version__ ) <= LooseVersion ('1.2.1' )
83
- self .mpl_ge_1_3_1 = str (mpl .__version__ ) >= LooseVersion ('1.3.1' )
84
93
85
94
def tearDown (self ):
86
95
tm .close ()
@@ -183,7 +192,7 @@ def _check_colors(self, collections, linecolors=None, facecolors=None,
183
192
"""
184
193
185
194
from matplotlib .lines import Line2D
186
- from matplotlib .collections import Collection
195
+ from matplotlib .collections import Collection , PolyCollection
187
196
conv = self .colorconverter
188
197
if linecolors is not None :
189
198
@@ -197,6 +206,8 @@ def _check_colors(self, collections, linecolors=None, facecolors=None,
197
206
result = patch .get_color ()
198
207
# Line2D may contains string color expression
199
208
result = conv .to_rgba (result )
209
+ elif isinstance (patch , PolyCollection ):
210
+ result = tuple (patch .get_edgecolor ()[0 ])
200
211
else :
201
212
result = patch .get_edgecolor ()
202
213
@@ -472,6 +483,21 @@ def is_grid_on():
472
483
obj .plot (kind = kind , grid = True , ** kws )
473
484
self .assertTrue (is_grid_on ())
474
485
486
+ def _maybe_unpack_cycler (self , rcParams , field = 'color' ):
487
+ """
488
+ Compat layer for MPL 1.5 change to color cycle
489
+
490
+ Before: plt.rcParams['axes.color_cycle'] -> ['b', 'g', 'r'...]
491
+ After : plt.rcParams['axes.prop_cycle'] -> cycler(...)
492
+ """
493
+ if self .mpl_ge_1_5_0 :
494
+ cyl = rcParams ['axes.prop_cycle' ]
495
+ colors = [v [field ] for v in cyl ]
496
+ else :
497
+ colors = rcParams ['axes.color_cycle' ]
498
+ return colors
499
+
500
+
475
501
@tm .mplskip
476
502
class TestSeriesPlots (TestPlotBase ):
477
503
@@ -536,9 +562,13 @@ def test_plot_figsize_and_title(self):
536
562
537
563
def test_dont_modify_rcParams (self ):
538
564
# GH 8242
539
- colors = self .plt .rcParams ['axes.color_cycle' ]
565
+ if self .mpl_ge_1_5_0 :
566
+ key = 'axes.prop_cycle'
567
+ else :
568
+ key = 'axes.color_cycle'
569
+ colors = self .plt .rcParams [key ]
540
570
Series ([1 , 2 , 3 ]).plot ()
541
- self .assertEqual (colors , self .plt .rcParams ['axes.color_cycle' ])
571
+ self .assertEqual (colors , self .plt .rcParams [key ])
542
572
543
573
def test_ts_line_lim (self ):
544
574
ax = self .ts .plot ()
@@ -1109,7 +1139,8 @@ def test_errorbar_plot(self):
1109
1139
s .plot (yerr = np .arange (11 ))
1110
1140
1111
1141
s_err = ['zzz' ]* 10
1112
- with tm .assertRaises (TypeError ):
1142
+ # in mpl 1.5+ this is a TypeError
1143
+ with tm .assertRaises ((ValueError , TypeError )):
1113
1144
s .plot (yerr = s_err )
1114
1145
1115
1146
def test_table (self ):
@@ -1183,7 +1214,10 @@ def test_time_series_plot_color_kwargs(self):
1183
1214
def test_time_series_plot_color_with_empty_kwargs (self ):
1184
1215
import matplotlib as mpl
1185
1216
1186
- def_colors = mpl .rcParams ['axes.color_cycle' ]
1217
+ if self .mpl_ge_1_5_0 :
1218
+ def_colors = self ._maybe_unpack_cycler (mpl .rcParams )
1219
+ else :
1220
+ def_colors = mpl .rcParams ['axes.color_cycle' ]
1187
1221
index = date_range ('1/1/2000' , periods = 12 )
1188
1222
s = Series (np .arange (1 , 13 ), index = index )
1189
1223
@@ -1292,7 +1326,11 @@ def test_plot(self):
1292
1326
fig , ax = self .plt .subplots ()
1293
1327
axes = df .plot .bar (subplots = True , ax = ax )
1294
1328
self .assertEqual (len (axes ), 1 )
1295
- self .assertIs (ax .get_axes (), axes [0 ])
1329
+ if self .mpl_ge_1_5_0 :
1330
+ result = ax .axes
1331
+ else :
1332
+ result = ax .get_axes () # deprecated
1333
+ self .assertIs (result , axes [0 ])
1296
1334
1297
1335
def test_color_and_style_arguments (self ):
1298
1336
df = DataFrame ({'x' : [1 , 2 ], 'y' : [3 , 4 ]})
@@ -1802,8 +1840,7 @@ def test_area_lim(self):
1802
1840
@slow
1803
1841
def test_bar_colors (self ):
1804
1842
import matplotlib .pyplot as plt
1805
-
1806
- default_colors = plt .rcParams .get ('axes.color_cycle' )
1843
+ default_colors = self ._maybe_unpack_cycler (plt .rcParams )
1807
1844
1808
1845
df = DataFrame (randn (5 , 5 ))
1809
1846
ax = df .plot .bar ()
@@ -2326,6 +2363,7 @@ def test_kde_missing_vals(self):
2326
2363
2327
2364
@slow
2328
2365
def test_hist_df (self ):
2366
+ from matplotlib .patches import Rectangle
2329
2367
if self .mpl_le_1_2_1 :
2330
2368
raise nose .SkipTest ("not supported in matplotlib <= 1.2.x" )
2331
2369
@@ -2346,11 +2384,14 @@ def test_hist_df(self):
2346
2384
2347
2385
ax = series .plot .hist (normed = True , cumulative = True , bins = 4 )
2348
2386
# height of last bin (index 5) must be 1.0
2349
- self .assertAlmostEqual (ax .get_children ()[5 ].get_height (), 1.0 )
2387
+ rects = [x for x in ax .get_children () if isinstance (x , Rectangle )]
2388
+ self .assertAlmostEqual (rects [- 1 ].get_height (), 1.0 )
2350
2389
tm .close ()
2351
2390
2352
2391
ax = series .plot .hist (cumulative = True , bins = 4 )
2353
- self .assertAlmostEqual (ax .get_children ()[5 ].get_height (), 100.0 )
2392
+ rects = [x for x in ax .get_children () if isinstance (x , Rectangle )]
2393
+
2394
+ self .assertAlmostEqual (rects [- 2 ].get_height (), 100.0 )
2354
2395
tm .close ()
2355
2396
2356
2397
# if horizontal, yticklabels are rotated
@@ -2626,7 +2667,7 @@ def test_line_colors(self):
2626
2667
def test_line_colors_and_styles_subplots (self ):
2627
2668
# GH 9894
2628
2669
from matplotlib import cm
2629
- default_colors = self .plt .rcParams . get ( 'axes.color_cycle' )
2670
+ default_colors = self ._maybe_unpack_cycler ( self . plt .rcParams )
2630
2671
2631
2672
df = DataFrame (randn (5 , 5 ))
2632
2673
@@ -2698,7 +2739,8 @@ def test_area_colors(self):
2698
2739
2699
2740
handles , labels = ax .get_legend_handles_labels ()
2700
2741
# legend is stored as Line2D, thus check linecolors
2701
- self ._check_colors (handles , linecolors = custom_colors )
2742
+ linehandles = [x for x in handles if not isinstance (x , PolyCollection )]
2743
+ self ._check_colors (linehandles , linecolors = custom_colors )
2702
2744
for h in handles :
2703
2745
self .assertTrue (h .get_alpha () is None )
2704
2746
tm .close ()
@@ -2710,12 +2752,13 @@ def test_area_colors(self):
2710
2752
self ._check_colors (poly , facecolors = jet_colors )
2711
2753
2712
2754
handles , labels = ax .get_legend_handles_labels ()
2713
- self ._check_colors (handles , linecolors = jet_colors )
2755
+ linehandles = [x for x in handles if not isinstance (x , PolyCollection )]
2756
+ self ._check_colors (linehandles , linecolors = jet_colors )
2714
2757
for h in handles :
2715
2758
self .assertTrue (h .get_alpha () is None )
2716
2759
tm .close ()
2717
2760
2718
- # When stacked=True , alpha is set to 0.5
2761
+ # When stacked=False , alpha is set to 0.5
2719
2762
ax = df .plot .area (colormap = cm .jet , stacked = False )
2720
2763
self ._check_colors (ax .get_lines (), linecolors = jet_colors )
2721
2764
poly = [o for o in ax .get_children () if isinstance (o , PolyCollection )]
@@ -2724,13 +2767,13 @@ def test_area_colors(self):
2724
2767
2725
2768
handles , labels = ax .get_legend_handles_labels ()
2726
2769
# Line2D can't have alpha in its linecolor
2727
- self ._check_colors (handles , linecolors = jet_colors )
2770
+ self ._check_colors (handles [: len ( jet_colors )] , linecolors = jet_colors )
2728
2771
for h in handles :
2729
2772
self .assertEqual (h .get_alpha (), 0.5 )
2730
2773
2731
2774
@slow
2732
2775
def test_hist_colors (self ):
2733
- default_colors = self .plt .rcParams . get ( 'axes.color_cycle' )
2776
+ default_colors = self ._maybe_unpack_cycler ( self . plt .rcParams )
2734
2777
2735
2778
df = DataFrame (randn (5 , 5 ))
2736
2779
ax = df .plot .hist ()
@@ -2791,7 +2834,7 @@ def test_kde_colors_and_styles_subplots(self):
2791
2834
_skip_if_no_scipy_gaussian_kde ()
2792
2835
2793
2836
from matplotlib import cm
2794
- default_colors = self .plt .rcParams . get ( 'axes.color_cycle' )
2837
+ default_colors = self ._maybe_unpack_cycler ( self . plt .rcParams )
2795
2838
2796
2839
df = DataFrame (randn (5 , 5 ))
2797
2840
@@ -2853,7 +2896,7 @@ def _check_colors(bp, box_c, whiskers_c, medians_c, caps_c='k', fliers_c='b'):
2853
2896
self ._check_colors (bp ['fliers' ], linecolors = [fliers_c ] * len (bp ['fliers' ]))
2854
2897
self ._check_colors (bp ['caps' ], linecolors = [caps_c ] * len (bp ['caps' ]))
2855
2898
2856
- default_colors = self .plt .rcParams . get ( 'axes.color_cycle' )
2899
+ default_colors = self ._maybe_unpack_cycler ( self . plt .rcParams )
2857
2900
2858
2901
df = DataFrame (randn (5 , 5 ))
2859
2902
bp = df .plot .box (return_type = 'dict' )
@@ -2900,12 +2943,17 @@ def _check_colors(bp, box_c, whiskers_c, medians_c, caps_c='k', fliers_c='b'):
2900
2943
2901
2944
def test_default_color_cycle (self ):
2902
2945
import matplotlib .pyplot as plt
2903
- plt .rcParams ['axes.color_cycle' ] = list ('rgbk' )
2946
+ colors = list ('rgbk' )
2947
+ if self .mpl_ge_1_5_0 :
2948
+ import cycler
2949
+ plt .rcParams ['axes.prop_cycle' ] = cycler .cycler ('color' , colors )
2950
+ else :
2951
+ plt .rcParams ['axes.color_cycle' ] = colors
2904
2952
2905
2953
df = DataFrame (randn (5 , 3 ))
2906
2954
ax = df .plot ()
2907
2955
2908
- expected = plt .rcParams [ 'axes.color_cycle' ] [:3 ]
2956
+ expected = self . _maybe_unpack_cycler ( plt .rcParams ) [:3 ]
2909
2957
self ._check_colors (ax .get_lines (), linecolors = expected )
2910
2958
2911
2959
def test_unordered_ts (self ):
@@ -3125,7 +3173,7 @@ def test_errorbar_plot(self):
3125
3173
df .plot (yerr = np .random .randn (11 ))
3126
3174
3127
3175
df_err = DataFrame ({'x' : ['zzz' ]* 12 , 'y' : ['zzz' ]* 12 })
3128
- with tm .assertRaises (TypeError ):
3176
+ with tm .assertRaises (( ValueError , TypeError ) ):
3129
3177
df .plot (yerr = df_err )
3130
3178
3131
3179
@slow
0 commit comments