4

For example i want to supply an array of alpha values that is the same length of the amount of points i want to plot with scatter.

I saw there was a work around by manually calculating rbg color values and using a color map instead, is there an easier way?

If this is the only way to go how can i abstract that function so that i can just supply # of elements, color, and transparency values to get the color map i need? I don't know what colors i may be using and the transparency weighting will be changing so i'd want something more generic.

  • 3
    You need to supply the colors containing the alpha of your choice to the `c` argument of `scatter`. – ImportanceOfBeingErnest Nov 20 '19 at 00:53
  • Does this answer your question? [Individual alpha values in scatter plot](https://stackoverflow.com/questions/24767355/individual-alpha-values-in-scatter-plot) – William Miller Nov 20 '19 at 03:55
  • @WilliamMiller I did see that answer, so that is the only way then? And sort of answers the question, i still need to not only figure out how to scale an arbitrary choice of color as well as figure out how to scale properly with my weight array (might might not be a sequence of equal values like in the example). I'm not sure if that answer helps much in that regard, seems very specific to that particular use case. – learningthemachine Nov 20 '19 at 04:31
  • 1
    @learningthemachine The array in which the alpha values are specified can be created to suits your needs. In that question the line `alphas = np.linspace(0.1, 1, 10)` is for demonstration only, you can supply any array of the correct size `alphas` and it will work as you want it to – William Miller Nov 20 '19 at 04:35

1 Answers1

12

You can use matplotlib.colors.to_rgb(a).

import matplotlib.pyplot as plt
from matplotlib.colors import to_rgb, to_rgba
import numpy as np

def scatter(x, y, color, alpha_arr, **kwarg):
    r, g, b = to_rgb(color)
    # r, g, b, _ = to_rgba(color)
    color = [(r, g, b, alpha) for alpha in alpha_arr]
    plt.scatter(x, y, c=color, **kwarg)

N = 500
x = np.random.rand(N)
y = np.random.rand(N)
alpha_arr = x * y
scatter(x, y, 'red', alpha_arr, s=10) # ok
scatter(x, y, '#efefef', alpha_arr, s=10) # ok

ref.

eduidl
  • 221
  • 1
  • 6
  • I love the simplicity of that. Plus it can be adapted in a few lines to support a color **array** in input. – Guimoute Feb 19 '20 at 10:33