Skip to content

Cannot specify the colors of each point in scatter plots #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kinyatoride opened this issue Dec 31, 2021 · 1 comment
Closed

Cannot specify the colors of each point in scatter plots #316

kinyatoride opened this issue Dec 31, 2021 · 1 comment
Labels

Comments

@kinyatoride
Copy link

Description

Proplot does not accept a list of colors for scatter plots.

Steps to reproduce

import proplot as pplt
fig, ax = pplt.subplots()
ax.scatter([0, 1, 2], [0, 1, 2], c=['b', 'r', 'k'])

Actual behavior: [What actually happened]

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_81289/542382403.py in <module>
      1 fig, ax = pplt.subplots()
      2 
----> 3 ax.scatter([0, 1, 2], [0, 1, 2], c=['b', 'r', 'k'])

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/internals/process.py in _redirect_or_standardize(self, *args, **kwargs)
    282 
    283                 # Call main function
--> 284                 return func(self, *args, **kwargs)  # call unbound method
    285 
    286         return _redirect_or_standardize

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/axes/plot.py in scatter(self, *args, **kwargs)
   3257         """
   3258         kwargs = _parse_vert(default_vert=True, **kwargs)
-> 3259         return self._apply_scatter(*args, **kwargs)
   3260 
   3261     @process._preprocess_args(

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/axes/plot.py in _apply_scatter(self, xs, ys, ss, cc, vert, **kwargs)
   3218             ):
   3219                 infer_rgb = False
-> 3220         cc, kw = self._parse_color(
   3221             xs, ys, cc, inbounds=inbounds, apply_cycle=False, infer_rgb=infer_rgb, **kw
   3222         )

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/axes/plot.py in _parse_color(self, x, y, c, apply_cycle, infer_rgb, **kwargs)
   2080                 c = list(map(pcolors.to_hex, c))  # avoid iterating over columns
   2081             else:
-> 2082                 kwargs = self._parse_cmap(
   2083                     x, y, c, plot_lines=True, default_discrete=False, **kwargs
   2084                 )

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/internals/warnings.py in _deprecate_kwargs(*args, **kwargs)
     94                     f'be removed in a future release. Please use {key_new!r} instead.'
     95                 )
---> 96             return func_orig(*args, **kwargs)
     97         return _deprecate_kwargs
     98     return decorator

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/axes/plot.py in _parse_cmap(self, cmap, cmap_kw, c, color, colors, default_cmap, norm, norm_kw, extend, vmin, vmax, discrete, default_discrete, skip_autolev, plot_lines, plot_contours, min_levels, *args, **kwargs)
   2643         isdiverging = False
   2644         if not discrete and not skip_autolev:
-> 2645             vmin, vmax, kwargs = self._parse_vlim(
   2646                 *args, vmin=vmin, vmax=vmax, **kwargs
   2647             )

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/axes/plot.py in _parse_vlim(self, vmin, vmax, robust, inbounds, negative, positive, symmetric, to_centers, *args, **kwargs)
   2156             if inbounds and x is not None and y is not None:  # ignore if None coords
   2157                 z = self._inbounds_vlim(x, y, z, to_centers=to_centers)
-> 2158             imin, imax = process._safe_range(z, pmin, pmax)
   2159             if automin and imin is not None:
   2160                 vmins.append(imin)

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/internals/process.py in _safe_range(data, lo, hi)
    488     """
    489     _load_objects()
--> 490     data, units = _to_masked_array(data)
    491     data = data.compressed()  # remove all invalid values
    492     min_ = max_ = None

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/proplot/internals/process.py in _to_masked_array(data, copy)
    141     if ndarray is not Quantity and isinstance(data, Quantity):
    142         data, units = data.magnitude, data.units
--> 143     data = ma.masked_invalid(data, copy=copy)
    144     if np.issubdtype(data.dtype, np.integer):
    145         data = data.astype(np.float)

~/miniconda3/envs/basicf8/lib/python3.9/site-packages/numpy/ma/core.py in masked_invalid(a, copy)
   2369         cls = type(a)
   2370     else:
-> 2371         condition = ~(np.isfinite(a))
   2372         cls = MaskedArray
   2373     result = a.view(cls)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Equivalent steps in matplotlib

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter([0, 1, 2], [0, 1, 2], c=['b', 'r', 'k'])

Screen Shot 2021-12-31 at 13 08 22

Proplot version

3.4.3
0.9.5

@lukelbd lukelbd added the bug label Jan 1, 2022
@lukelbd lukelbd closed this as completed in d0bc9c0 Jan 1, 2022
@lukelbd
Copy link
Collaborator

lukelbd commented Jan 1, 2022

Thanks -- this was a one-line fix. The c scatter keyword can be either a list of colors or a list of values that get scaled by cmap. I have to differentiate between these possibilities, but I didn't account for the case where string colors are used rather than RGB[A] tuples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants