Skip to content

Scatter accepts marker=MarkerStyle(), but plot does not #17432

@mwaskom

Description

@mwaskom

Bug report

Bug summary

Perhaps not a bug because I'm not sure this is claimed to work, but you can pass a matplotlib.markers.MarkerStyle object as an argument for marker= in Axes.scatter but not Axes.plot.

It fails within matplotlib.markers.MarkerStyle itself, so the easiest thing might be to let that class accept a MarkerStyle object in addition to string/tuple specs. You'd have to define the behavior for when fillstyle is also given and differs from what is set on the passed-in object.

Code for reproduction

import matplotlib as mpl
import matplotlib.pyplot as plt
plt.plot([], [], marker=mpl.markers.MarkerStyle("o"))

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-de767cc56f2d> in <module>
----> 1 plt.plot([], [], marker=mpl.markers.MarkerStyle("o"))

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2759 @docstring.copy(Axes.plot)
   2760 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2761     return gca().plot(
   2762         *args, scalex=scalex, scaley=scaley, **({"data": data} if data
   2763         is not None else {}), **kwargs)

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1644         """
   1645         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1646         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1647         for line in lines:
   1648             self.add_line(line)

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/axes/_base.py in __call__(self, *args, **kwargs)
    214                 this += args[0],
    215                 args = args[1:]
--> 216             yield from self._plot_args(this, kwargs)
    217 
    218     def get_next_color(self):

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    361                 "2.2", message="cycling among columns of inputs with "
    362                 "non-matching shapes is deprecated.")
--> 363         return [func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
    364                 for j in range(max(ncx, ncy))]
    365 

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/axes/_base.py in <listcomp>(.0)
    361                 "2.2", message="cycling among columns of inputs with "
    362                 "non-matching shapes is deprecated.")
--> 363         return [func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
    364                 for j in range(max(ncx, ncy))]
    365 

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/axes/_base.py in _makeline(self, x, y, kw, kwargs)
    253         default_dict = self._getdefaults(set(), kw)
    254         self._setdefaults(default_dict, kw)
--> 255         seg = mlines.Line2D(x, y, **kw)
    256         return seg
    257 

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/lines.py in __init__(self, xdata, ydata, linewidth, linestyle, color, marker, markersize, markeredgewidth, markeredgecolor, markerfacecolor, markerfacecoloralt, fillstyle, antialiased, dash_capstyle, solid_capstyle, dash_joinstyle, solid_joinstyle, pickradius, drawstyle, markevery, **kwargs)
    376         self._color = None
    377         self.set_color(color)
--> 378         self._marker = MarkerStyle(marker, fillstyle)
    379 
    380         self._markevery = None

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/markers.py in __init__(self, marker, fillstyle)
    223         self._marker_function = None
    224         self.set_fillstyle(fillstyle)
--> 225         self.set_marker(marker)
    226 
    227     def _recache(self):

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/markers.py in set_marker(self, marker)
    287         else:
    288             try:
--> 289                 Path(marker)
    290                 self._marker_function = self._set_vertices
    291             except ValueError:

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/path.py in __init__(self, vertices, codes, _interpolation_steps, closed, readonly)
    125             and codes as read-only arrays.
    126         """
--> 127         vertices = _to_unmasked_float_array(vertices)
    128         if vertices.ndim != 2 or vertices.shape[1] != 2:
    129             raise ValueError(

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/matplotlib/cbook/__init__.py in _to_unmasked_float_array(x)
   1315         return np.ma.asarray(x, float).filled(np.nan)
   1316     else:
-> 1317         return np.asarray(x, float)
   1318 
   1319 

~/miniconda3/envs/seaborn-refactor/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

TypeError: float() argument must be a string or a number, not 'MarkerStyle'

Expected outcome

No error; plot uses specified marker.

Matplotlib version

  • Operating system: macos
  • Matplotlib version: 3.2.1
  • Matplotlib backend (print(matplotlib.get_backend())): pylab inline
  • Python version:3.8
  • Jupyter version (if applicable): na
  • Other libraries: na

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions