@@ -497,21 +497,27 @@ def plot( data, **kwargs ):
497
497
498
498
addplot = config ['addplot' ]
499
499
if addplot is not None and ptype not in VALID_PMOVE_TYPES :
500
- # Calculate the Order of Magnitude Range ('mag')
501
- # If addplot['secondary_y'] == 'auto', then: If the addplot['data']
502
- # is out of the Order of Magnitude Range, then use secondary_y.
503
- # Calculate omrange for Main panel, and for Lower (volume) panel:
504
- lo = math .log (max (math .fabs (np .nanmin (lows )),1e-7 ),10 ) - 0.5
505
- hi = math .log (max (math .fabs (np .nanmax (highs )),1e-7 ),10 ) + 0.5
500
+ # NOTE: If in external_axes_mode, then all code relating
501
+ # to panels and secondary_y becomes irrrelevant.
502
+ # If the user wants something on a secondary_y then user should
503
+ # determine that externally, and pass in the appropriate axes.
506
504
507
- panels ['mag' ] = [None ]* len (panels ) # create 'mag'nitude column
505
+ if not external_axes_mode :
506
+ # Calculate the Order of Magnitude Range ('mag')
507
+ # If addplot['secondary_y'] == 'auto', then: If the addplot['data']
508
+ # is out of the Order of Magnitude Range, then use secondary_y.
508
509
509
- panels .at [config ['main_panel' ],'mag' ] = {'lo' :lo ,'hi' :hi } # update main panel magnitude range
510
+ lo = math .log (max (math .fabs (np .nanmin (lows )),1e-7 ),10 ) - 0.5
511
+ hi = math .log (max (math .fabs (np .nanmax (highs )),1e-7 ),10 ) + 0.5
510
512
511
- if config ['volume' ]:
512
- lo = math .log (max (math .fabs (np .nanmin (volumes )),1e-7 ),10 ) - 0.5
513
- hi = math .log (max (math .fabs (np .nanmax (volumes )),1e-7 ),10 ) + 0.5
514
- panels .at [config ['volume_panel' ],'mag' ] = {'lo' :lo ,'hi' :hi }
513
+ panels ['mag' ] = [None ]* len (panels ) # create 'mag'nitude column
514
+
515
+ panels .at [config ['main_panel' ],'mag' ] = {'lo' :lo ,'hi' :hi } # update main panel magnitude range
516
+
517
+ if config ['volume' ]:
518
+ lo = math .log (max (math .fabs (np .nanmin (volumes )),1e-7 ),10 ) - 0.5
519
+ hi = math .log (max (math .fabs (np .nanmax (volumes )),1e-7 ),10 ) + 0.5
520
+ panels .at [config ['volume_panel' ],'mag' ] = {'lo' :lo ,'hi' :hi }
515
521
516
522
if isinstance (addplot ,dict ):
517
523
addplot = [addplot ,] # make list of dict to be consistent
@@ -521,11 +527,12 @@ def plot( data, **kwargs ):
521
527
522
528
for apdict in addplot :
523
529
524
- panid = apdict ['panel' ]
525
- if panid == 'main' : panid = 0 # for backwards compatibility
526
- elif panid == 'lower' : panid = 1 # for backwards compatibility
527
- if apdict ['y_on_right' ] is not None :
528
- panels .at [panid ,'y_on_right' ] = apdict ['y_on_right' ]
530
+ panid = apdict ['panel' ]
531
+ if not external_axes_mode :
532
+ if panid == 'main' : panid = 0 # for backwards compatibility
533
+ elif panid == 'lower' : panid = 1 # for backwards compatibility
534
+ if apdict ['y_on_right' ] is not None :
535
+ panels .at [panid ,'y_on_right' ] = apdict ['y_on_right' ]
529
536
530
537
aptype = apdict ['type' ]
531
538
if aptype == 'ohlc' or aptype == 'candle' :
@@ -545,6 +552,8 @@ def plot( data, **kwargs ):
545
552
ax = _addplot_columns (panid ,panels ,ydata ,apdict ,xdates ,config )
546
553
_addplot_apply_supplements (ax ,apdict )
547
554
555
+ # fill_between is NOT supported for external_axes_mode
556
+ # (caller can easily call ax.fill_between() themselves).
548
557
if config ['fill_between' ] is not None and not external_axes_mode :
549
558
fb = config ['fill_between' ]
550
559
panid = config ['main_panel' ]
@@ -696,6 +705,7 @@ def _addplot_collections(panid,panels,apdict,xdates,config):
696
705
697
706
apdata = apdict ['data' ]
698
707
aptype = apdict ['type' ]
708
+ external_axes_mode = apdict ['ax' ] is not None
699
709
700
710
#--------------------------------------------------------------#
701
711
# Note: _auto_secondary_y() sets the 'magnitude' column in the
@@ -716,16 +726,21 @@ def _addplot_collections(panid,panels,apdict,xdates,config):
716
726
raise TypeError ('addplot type "' + aptype + '" MUST be accompanied by addplot data of type `pd.DataFrame`' )
717
727
d ,o ,h ,l ,c ,v = _check_and_prepare_data (apdata ,config )
718
728
collections = _construct_mpf_collections (aptype ,d ,xdates ,o ,h ,l ,c ,v ,config ,config ['style' ])
719
- lo = math .log (max (math .fabs (np .nanmin (l )),1e-7 ),10 ) - 0.5
720
- hi = math .log (max (math .fabs (np .nanmax (h )),1e-7 ),10 ) + 0.5
721
- secondary_y = _auto_secondary_y ( panels , panid , lo , hi )
722
- if 'auto' != apdict ['secondary_y' ]:
723
- secondary_y = apdict ['secondary_y' ]
724
- if secondary_y :
725
- ax = panels .at [panid ,'axes' ][1 ]
726
- panels .at [panid ,'used2nd' ] = True
727
- else :
728
- ax = panels .at [panid ,'axes' ][0 ]
729
+
730
+ if not external_axes_mode :
731
+ lo = math .log (max (math .fabs (np .nanmin (l )),1e-7 ),10 ) - 0.5
732
+ hi = math .log (max (math .fabs (np .nanmax (h )),1e-7 ),10 ) + 0.5
733
+ secondary_y = _auto_secondary_y ( panels , panid , lo , hi )
734
+ if 'auto' != apdict ['secondary_y' ]:
735
+ secondary_y = apdict ['secondary_y' ]
736
+ if secondary_y :
737
+ ax = panels .at [panid ,'axes' ][1 ]
738
+ panels .at [panid ,'used2nd' ] = True
739
+ else :
740
+ ax = panels .at [panid ,'axes' ][0 ]
741
+ else :
742
+ ax = apdict ['ax' ]
743
+
729
744
for coll in collections :
730
745
ax .add_collection (coll )
731
746
if apdict ['mav' ] is not None :
@@ -734,21 +749,25 @@ def _addplot_collections(panid,panels,apdict,xdates,config):
734
749
return ax
735
750
736
751
def _addplot_columns (panid ,panels ,ydata ,apdict ,xdates ,config ):
737
- secondary_y = False
738
- if apdict ['secondary_y' ] == 'auto' :
739
- yd = [y for y in ydata if not math .isnan (y )]
740
- ymhi = math .log (max (math .fabs (np .nanmax (yd )),1e-7 ),10 )
741
- ymlo = math .log (max (math .fabs (np .nanmin (yd )),1e-7 ),10 )
742
- secondary_y = _auto_secondary_y ( panels , panid , ymlo , ymhi )
752
+ external_axes_mode = apdict ['ax' ] is not None
753
+ if not external_axes_mode :
754
+ secondary_y = False
755
+ if apdict ['secondary_y' ] == 'auto' :
756
+ yd = [y for y in ydata if not math .isnan (y )]
757
+ ymhi = math .log (max (math .fabs (np .nanmax (yd )),1e-7 ),10 )
758
+ ymlo = math .log (max (math .fabs (np .nanmin (yd )),1e-7 ),10 )
759
+ secondary_y = _auto_secondary_y ( panels , panid , ymlo , ymhi )
760
+ else :
761
+ secondary_y = apdict ['secondary_y' ]
762
+ #print("apdict['secondary_y'] says secondary_y is",secondary_y)
763
+
764
+ if secondary_y :
765
+ ax = panels .at [panid ,'axes' ][1 ]
766
+ panels .at [panid ,'used2nd' ] = True
767
+ else :
768
+ ax = panels .at [panid ,'axes' ][0 ]
743
769
else :
744
- secondary_y = apdict ['secondary_y' ]
745
- #print("apdict['secondary_y'] says secondary_y is",secondary_y)
746
-
747
- if secondary_y :
748
- ax = panels .at [panid ,'axes' ][1 ]
749
- panels .at [panid ,'used2nd' ] = True
750
- else :
751
- ax = panels .at [panid ,'axes' ][0 ]
770
+ ax = apdict ['ax' ]
752
771
753
772
aptype = apdict ['type' ]
754
773
if aptype == 'scatter' :
0 commit comments