Skip to content

Commit 7736288

Browse files
support multiple markers in make_addplot(type="scatter")
1 parent 4e7396a commit 7736288

File tree

3 files changed

+488
-6
lines changed

3 files changed

+488
-6
lines changed

examples/scratch_pad/scatter_multiple_markers.ipynb

Lines changed: 463 additions & 0 deletions
Large diffs are not rendered by default.

src/mplfinance/_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import matplotlib.dates as mdates
88
import datetime
99

10+
from itertools import cycle
11+
1012
from matplotlib import colors as mcolors
1113
from matplotlib.patches import Ellipse
1214
from matplotlib.collections import LineCollection, PolyCollection, PatchCollection
@@ -1136,3 +1138,19 @@ def __call__(self, x, pos=0):
11361138
#print('x=',x,'pos=',pos,'dates[',ix,']=',date,'dateformat=',dateformat)
11371139
return dateformat
11381140

1141+
def _mscatter(x,y,ax=None, m=None, **kw):
1142+
import matplotlib.markers as mmarkers
1143+
if not ax: ax=plt.gca()
1144+
sc = ax.scatter(x,y,**kw)
1145+
if (m is not None) and (len(m)==len(x)):
1146+
paths = []
1147+
for marker in m:
1148+
if isinstance(marker, mmarkers.MarkerStyle):
1149+
marker_obj = marker
1150+
else:
1151+
marker_obj = mmarkers.MarkerStyle(marker)
1152+
path = marker_obj.get_path().transformed(
1153+
marker_obj.get_transform())
1154+
paths.append(path)
1155+
sc.set_paths(paths)
1156+
return sc

src/mplfinance/plotting.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from mplfinance._utils import _updown_colors
2525
from mplfinance._utils import IntegerIndexDateTimeFormatter
26+
from mplfinance._utils import _mscatter
2627

2728
from mplfinance import _styles
2829

@@ -506,11 +507,10 @@ def plot( data, **kwargs ):
506507
size = apdict['markersize']
507508
mark = apdict['marker']
508509
color = apdict['color']
509-
# -------------------------------------------------------- #
510-
# This fixes Issue#77, but breaks other stuff:
511-
# ax.set_ylim(ymin=(miny - 0.4*stdy),ymax=(maxy + 0.4*stdy))
512-
# -------------------------------------------------------- #
513-
ax.scatter(xdates, ydata, s=size, marker=mark, color=color)
510+
if isinstance(mark,(list,tuple,np.ndarray)):
511+
_mscatter(xdates, ydata, ax=ax, m=mark, s=size, color=color)
512+
else:
513+
ax.scatter(xdates, ydata, s=size, marker=mark, color=color)
514514
elif aptype == 'bar':
515515
width = apdict['width']
516516
bottom = apdict['bottom']
@@ -668,7 +668,8 @@ def _valid_addplot_kwargs():
668668
'Validator' : lambda value: isinstance(value,(int,float)) },
669669

670670
'color' : { 'Default' : None,
671-
'Validator' : lambda value: mcolors.is_color_like(value) },
671+
'Validator' : lambda value: mcolors.is_color_like(value) or
672+
(isinstance(value,(list,tuple,np.ndarray)) and all([mcolors.is_color_like(v) for v in value])) },
672673

673674
'linestyle' : { 'Default' : None,
674675
'Validator' : lambda value: value in valid_linestyles },

0 commit comments

Comments
 (0)