@@ -752,6 +752,8 @@ def plot( data, **kwargs ):
752
752
753
753
elif not _list_of_dict (addplot ):
754
754
raise TypeError ('addplot must be `dict`, or `list of dict`, NOT ' + str (type (addplot )))
755
+
756
+ contains_legend_label = [] # a list of axes that contains legend labels
755
757
756
758
for apdict in addplot :
757
759
@@ -779,6 +781,10 @@ def plot( data, **kwargs ):
779
781
ydata = apdata .loc [:,column ] if havedf else column
780
782
ax = _addplot_columns (panid ,panels ,ydata ,apdict ,xdates ,config )
781
783
_addplot_apply_supplements (ax ,apdict ,xdates )
784
+ if apdict ["label" ]: # not supported for aptype == 'ohlc' or 'candle'
785
+ contains_legend_label .append (ax )
786
+ for ax in set (contains_legend_label ): # there will be duplicates,
787
+ ax .legend () # but its ok to call ax.legend() multiple times
782
788
783
789
# fill_between is NOT supported for external_axes_mode
784
790
# (caller can easily call ax.fill_between() themselves).
@@ -1088,46 +1094,38 @@ def _addplot_columns(panid,panels,ydata,apdict,xdates,config):
1088
1094
ax = apdict ['ax' ]
1089
1095
1090
1096
aptype = apdict ['type' ]
1097
+ label = apdict ['label' ]
1091
1098
if aptype == 'scatter' :
1092
1099
size = apdict ['markersize' ]
1093
1100
mark = apdict ['marker' ]
1094
1101
color = apdict ['color' ]
1095
1102
alpha = apdict ['alpha' ]
1096
- labels = apdict ['labels' ]
1097
1103
edgecolors = apdict ['edgecolors' ]
1098
1104
linewidths = apdict ['linewidths' ]
1099
1105
1100
1106
if isinstance (mark ,(list ,tuple ,np .ndarray )):
1101
1107
_mscatter (xdates , ydata , ax = ax , m = mark , s = size , color = color , alpha = alpha , edgecolors = edgecolors , linewidths = linewidths )
1102
- else :
1103
- ax .scatter (xdates , ydata , s = size , marker = mark , color = color , alpha = alpha , edgecolors = edgecolors , linewidths = linewidths )
1104
- if labels is not None :
1105
- ax .legend (labels = labels )
1108
+ else :
1109
+ ax .scatter (xdates , ydata , s = size , marker = mark , color = color , alpha = alpha , edgecolors = edgecolors , linewidths = linewidths ,label = label )
1106
1110
elif aptype == 'bar' :
1107
1111
width = 0.8 if apdict ['width' ] is None else apdict ['width' ]
1108
1112
bottom = apdict ['bottom' ]
1109
1113
color = apdict ['color' ]
1110
1114
alpha = apdict ['alpha' ]
1111
- ax .bar (xdates ,ydata ,width = width ,bottom = bottom ,color = color ,alpha = alpha )
1112
- if apdict ['labels' ] is not None :
1113
- ax .legend (labels = apdict ['labels' ])
1115
+ ax .bar (xdates ,ydata ,width = width ,bottom = bottom ,color = color ,alpha = alpha ,label = label )
1114
1116
elif aptype == 'line' :
1115
1117
ls = apdict ['linestyle' ]
1116
1118
color = apdict ['color' ]
1117
1119
width = apdict ['width' ] if apdict ['width' ] is not None else 1.6 * config ['_width_config' ]['line_width' ]
1118
1120
alpha = apdict ['alpha' ]
1119
- ax .plot (xdates ,ydata ,linestyle = ls ,color = color ,linewidth = width ,alpha = alpha )
1120
- if apdict ['labels' ] is not None :
1121
- ax .legend (labels = apdict ['labels' ])
1121
+ ax .plot (xdates ,ydata ,linestyle = ls ,color = color ,linewidth = width ,alpha = alpha ,label = label )
1122
1122
elif aptype == 'step' :
1123
1123
stepwhere = apdict ['stepwhere' ]
1124
1124
ls = apdict ['linestyle' ]
1125
1125
color = apdict ['color' ]
1126
1126
width = apdict ['width' ] if apdict ['width' ] is not None else 1.6 * config ['_width_config' ]['line_width' ]
1127
1127
alpha = apdict ['alpha' ]
1128
- ax .step (xdates ,ydata ,where = stepwhere ,linestyle = ls ,color = color ,linewidth = width ,alpha = alpha )
1129
- if apdict ['labels' ] is not None :
1130
- ax .legend (labels = apdict ['labels' ])
1128
+ ax .step (xdates ,ydata ,where = stepwhere ,linestyle = ls ,color = color ,linewidth = width ,alpha = alpha ,label = label )
1131
1129
else :
1132
1130
raise ValueError ('addplot type "' + str (aptype )+ '" NOT yet supported.' )
1133
1131
@@ -1380,8 +1378,8 @@ def _valid_addplot_kwargs():
1380
1378
'fill_between' : { 'Default' : None , # added by Wen
1381
1379
'Description' : " fill region" ,
1382
1380
'Validator' : _fill_between_validator },
1383
- "labels " : { 'Default' : None ,
1384
- 'Description' : 'Labels for the added plot.' ,
1381
+ "label " : { 'Default' : None ,
1382
+ 'Description' : 'Label for the added plot. One per added plot.' ,
1385
1383
'Validator' : _label_validator },
1386
1384
1387
1385
}
0 commit comments