@@ -2961,8 +2961,9 @@ def clip_lower(self, threshold):
2961
2961
#----------------------------------------------------------------------
2962
2962
# Plotting
2963
2963
2964
- def plot (self , kind = 'line' , subplots = False , sharex = True , sharey = False , use_index = True ,
2965
- figsize = None , grid = True , legend = True , rot = 30 , ax = None , ** kwds ):
2964
+ def plot (self , subplots = False , sharex = True , sharey = False , use_index = True ,
2965
+ figsize = None , grid = True , legend = True , rot = 30 , ax = None ,
2966
+ kind = 'line' , ** kwds ):
2966
2967
"""
2967
2968
Make line plot of DataFrame's series with the index on the x-axis using
2968
2969
matplotlib / pylab.
@@ -2977,6 +2978,7 @@ def plot(self, kind='line', subplots=False, sharex=True, sharey=False, use_index
2977
2978
In case subplots=True, share y axis
2978
2979
use_index : boolean, default True
2979
2980
Use index as ticks for x axis
2981
+ kind : {'line', 'bar'}
2980
2982
kwds : keywords
2981
2983
Options to pass to Axis.plot
2982
2984
@@ -2995,6 +2997,7 @@ def plot(self, kind='line', subplots=False, sharex=True, sharey=False, use_index
2995
2997
if ax is None :
2996
2998
fig = plt .figure (figsize = figsize )
2997
2999
ax = fig .add_subplot (111 )
3000
+ axes = [ax ]
2998
3001
else :
2999
3002
fig = ax .get_figure ()
3000
3003
@@ -3015,46 +3018,61 @@ def plot(self, kind='line', subplots=False, sharex=True, sharey=False, use_index
3015
3018
ax .plot (x , y , label = str (col ), ** kwds )
3016
3019
3017
3020
ax .grid (grid )
3018
- elif kind == 'bar' :
3019
- N = len (self )
3020
- M = len (self .columns )
3021
- xinds = np .arange (N ) + 0.25
3022
- colors = ['red' , 'green' , 'blue' , 'yellow' , 'black' ]
3023
- rects = []
3024
- labels = []
3025
- for i , col in enumerate (_try_sort (self .columns )):
3026
- empty = self [col ].count () == 0
3027
- y = self [col ].values if not empty else np .zeros (x .shape )
3028
- if subplots :
3029
- ax = axes [i ]
3030
- ax .bar (xinds , y , 0.5 ,
3031
- bottom = np .zeros (N ), linewidth = 1 , ** kwds )
3032
- ax .set_title (col )
3033
- else :
3034
- rects .append (ax .bar (xinds + i * 0.5 / M ,y ,0.5 / M ,bottom = np .zeros (N ),color = colors [i % len (colors )], ** kwds ))
3035
- labels .append (col )
3036
3021
3037
- if N < 10 :
3038
- fontsize = 12
3039
- else :
3040
- fontsize = 10
3041
-
3042
- ax .set_xticks (xinds + 0.25 )
3043
- ax .set_xticklabels (self .index , rotation = rot , fontsize = fontsize )
3022
+ if legend and not subplots :
3023
+ ax .legend (loc = 'best' )
3024
+ elif kind == 'bar' :
3025
+ self ._bar_plot (axes , subplots = subplots , grid = grid , rot = rot ,
3026
+ legend = legend )
3044
3027
3045
3028
# try to make things prettier
3046
3029
try :
3047
3030
fig .autofmt_xdate ()
3048
3031
except Exception : # pragma: no cover
3049
3032
pass
3050
3033
3051
- if legend and not subplots :
3052
- if kind == 'line' :
3053
- ax .legend (loc = 'best' )
3034
+ plt .draw_if_interactive ()
3035
+
3036
+ def _bar_plot (self , axes , subplots = False , use_index = True , grid = True ,
3037
+ rot = 30 , legend = True , ** kwds ):
3038
+ N , K = self .shape
3039
+ xinds = np .arange (N ) + 0.25
3040
+ colors = 'rgbyk'
3041
+ rects = []
3042
+ labels = []
3043
+
3044
+ if not subplots :
3045
+ ax = axes [0 ]
3046
+
3047
+ for i , col in enumerate (_try_sort (self .columns )):
3048
+ empty = self [col ].count () == 0
3049
+ y = self [col ].values if not empty else np .zeros (len (self ))
3050
+ if subplots :
3051
+ ax = axes [i ]
3052
+ ax .bar (xinds , y , 0.5 ,
3053
+ bottom = np .zeros (N ), linewidth = 1 , ** kwds )
3054
+ ax .set_title (col )
3054
3055
else :
3055
- ax .legend ([r [0 ] for r in rects ],labels ,loc = 'best' )
3056
+ rects .append (ax .bar (xinds + i * 0.5 / K , y , 0.5 / K ,
3057
+ bottom = np .zeros (N ),
3058
+ color = colors [i % len (colors )], ** kwds ))
3059
+ labels .append (col )
3056
3060
3057
- plt .draw_if_interactive ()
3061
+ if N < 10 :
3062
+ fontsize = 12
3063
+ else :
3064
+ fontsize = 10
3065
+
3066
+ ax .set_xticks (xinds + 0.25 )
3067
+ ax .set_xticklabels (self .index , rotation = rot , fontsize = fontsize )
3068
+
3069
+ if legend and not subplots :
3070
+ fig = ax .get_figure ()
3071
+ fig .legend ([r [0 ] for r in rects ], labels , loc = 'upper center' ,
3072
+ fancybox = True , ncol = 6 , mode = 'expand' )
3073
+
3074
+ import matplotlib .pyplot as plt
3075
+ plt .subplots_adjust (top = 0.8 )
3058
3076
3059
3077
def hist (self , grid = True , ** kwds ):
3060
3078
"""
0 commit comments