@@ -253,10 +253,12 @@ def plot( data, **kwargs ):
253
253
254
254
style = config ['style' ]
255
255
if isinstance (style ,str ):
256
- style = _styles ._get_mpfstyle (style )
256
+ style = config [ 'style' ] = _styles ._get_mpfstyle (style )
257
257
258
258
if isinstance (style ,dict ):
259
259
_styles ._apply_mpfstyle (style )
260
+ else :
261
+ raise TypeError ('style should be a `dict`; why is it not?' )
260
262
261
263
if config ['figsize' ] is None :
262
264
w ,h = config ['figratio' ]
@@ -320,39 +322,17 @@ def plot( data, **kwargs ):
320
322
for collection in collections :
321
323
axA1 .add_collection (collection )
322
324
323
- mavgs = config ['mav' ]
324
- if mavgs is not None :
325
- if isinstance (mavgs ,int ):
326
- mavgs = mavgs , # convert to tuple
327
- if len (mavgs ) > 7 :
328
- mavgs = mavgs [0 :7 ] # take at most 7
329
-
330
- if style ['mavcolors' ] is not None :
331
- mavc = cycle (style ['mavcolors' ])
332
- else :
333
- mavc = None
334
-
335
- # Get rcParams['lines.linewidth'] and scale it
336
- # according to the deinsity of data??
337
-
338
- for mav in mavgs :
339
- if ptype in VALID_PMOVE_TYPES :
340
- mavprices = pd .Series (brick_values ).rolling (mav ).mean ().values
341
- else :
342
- mavprices = pd .Series (closes ).rolling (mav ).mean ().values
343
-
344
- lw = config ['_width_config' ]['line_width' ]
345
- if mavc :
346
- axA1 .plot (xdates , mavprices , linewidth = lw , color = next (mavc ))
347
- else :
348
- axA1 .plot (xdates , mavprices , linewidth = lw )
325
+ if ptype in VALID_PMOVE_TYPES :
326
+ mavprices = _plot_mav (axA1 ,config ,xdates ,brick_values )
327
+ else :
328
+ mavprices = _plot_mav (axA1 ,config ,xdates ,closes )
349
329
350
330
avg_dist_between_points = (xdates [- 1 ] - xdates [0 ]) / float (len (xdates ))
351
331
if not config ['tight_layout' ]:
352
332
minx = xdates [0 ] - avg_dist_between_points
353
333
maxx = xdates [- 1 ] + avg_dist_between_points
354
334
else :
355
- minx = xdates [0 ] - (0.45 * avg_dist_between_points )
335
+ minx = xdates [0 ] - (0.45 * avg_dist_between_points )
356
336
maxx = xdates [- 1 ] + (0.45 * avg_dist_between_points )
357
337
358
338
if len (xdates ) == 1 : # kludge special case
@@ -387,10 +367,13 @@ def plot( data, **kwargs ):
387
367
retdict [prekey + '_size' ] = size
388
368
if config ['volume' ]:
389
369
retdict [prekey + '_volumes' ] = volumes
390
- if mavgs is not None :
391
- # This is WRONG! Returning the same mavprices for all mavgs ??!
392
- for i in range (0 , len (mavgs )):
393
- retdict ['mav' + str (mavgs [i ])] = mavprices
370
+ if config ['mav' ] is not None :
371
+ mav = config ['mav' ]
372
+ if len (mav ) != len (mavprices ):
373
+ warnings .warn ('len(mav)=' + str (len (mav ))+ ' BUT len(mavprices)=' + str (len (mavprices )))
374
+ else :
375
+ for jj in range (0 ,len (mav )):
376
+ retdict ['mav' + str (mav [jj ])] = mavprices [jj ]
394
377
retdict ['minx' ] = minx
395
378
retdict ['maxx' ] = maxx
396
379
retdict ['miny' ] = miny
@@ -504,10 +487,13 @@ def plot( data, **kwargs ):
504
487
ax = panels .at [panid ,'axes' ][0 ]
505
488
for coll in collections :
506
489
ax .add_collection (coll )
507
- datalim = (minx , min (l )), (maxx , max (h ))
490
+ if apdict ['mav' ] is not None :
491
+ apmavprices = _plot_mav (ax ,config ,xdates ,c ,apdict ['mav' ])
492
+ #datalim = (minx, min(l)), (maxx, max(h))
508
493
#ax.update_datalim(datalim)
509
- ax .autoscale_view () # Is this really necessary??
510
- #ax.set_ylim(min(l),max(h))
494
+ ax .autoscale_view ()
495
+ if (apdict ["ylabel" ] is not None ):
496
+ ax .set_ylabel (apdict ["ylabel" ])
511
497
continue
512
498
513
499
if isinstance (apdata ,list ) and not isinstance (apdata [0 ],(float ,int )):
@@ -563,7 +549,8 @@ def plot( data, **kwargs ):
563
549
ax .plot (xdates , ydata , linestyle = ls , color = color )
564
550
else :
565
551
raise ValueError ('addplot type "' + str (aptype )+ '" NOT yet supported.' )
566
-
552
+ if apdict ['mav' ] is not None :
553
+ apmavprices = _plot_mav (ax ,config ,xdates ,ydata ,apdict ['mav' ])
567
554
568
555
if config ['fill_between' ] is not None :
569
556
fb = config ['fill_between' ]
@@ -692,6 +679,34 @@ def plot( data, **kwargs ):
692
679
# print('rcpdfhead(3)=',rcpdf.head(3))
693
680
# return # rcpdf
694
681
682
+ def _plot_mav (ax ,config ,xdates ,prices ,apmav = None ):
683
+ style = config ['style' ]
684
+ if apmav is not None :
685
+ mavgs = apmav
686
+ else :
687
+ mavgs = config ['mav' ]
688
+ mavp_list = []
689
+ if mavgs is not None :
690
+ if isinstance (mavgs ,int ):
691
+ mavgs = mavgs , # convert to tuple
692
+ if len (mavgs ) > 7 :
693
+ mavgs = mavgs [0 :7 ] # take at most 7
694
+
695
+ if style ['mavcolors' ] is not None :
696
+ mavc = cycle (style ['mavcolors' ])
697
+ else :
698
+ mavc = None
699
+
700
+ for mav in mavgs :
701
+ mavprices = pd .Series (prices ).rolling (mav ).mean ().values
702
+ lw = config ['_width_config' ]['line_width' ]
703
+ if mavc :
704
+ ax .plot (xdates , mavprices , linewidth = lw , color = next (mavc ))
705
+ else :
706
+ ax .plot (xdates , mavprices , linewidth = lw )
707
+ mavp_list .append (mavprices )
708
+ return mavp_list
709
+
695
710
def _auto_secondary_y ( panels , panid , ylo , yhi ):
696
711
# If mag(nitude) for this panel is not yet set, then set it
697
712
# here, as this is the first ydata to be plotted on this panel:
@@ -711,7 +726,6 @@ def _auto_secondary_y( panels, panid, ylo, yhi ):
711
726
def _valid_addplot_kwargs ():
712
727
713
728
valid_linestyles = ('-' ,'solid' ,'--' ,'dashed' ,'-.' ,'dashdot' ,'.' ,'dotted' ,None ,' ' ,'' )
714
- #valid_types = ('line','scatter','bar','ohlc','candle')
715
729
valid_types = ('line' ,'scatter' ,'bar' , 'ohlc' , 'candle' )
716
730
717
731
vkwargs = {
@@ -721,6 +735,9 @@ def _valid_addplot_kwargs():
721
735
'type' : { 'Default' : 'line' ,
722
736
'Validator' : lambda value : value in valid_types },
723
737
738
+ 'mav' : { 'Default' : None ,
739
+ 'Validator' : _mav_validator },
740
+
724
741
'panel' : { 'Default' : 0 ,
725
742
'Validator' : lambda value : _valid_panel_id (value ) },
726
743
0 commit comments