Set rgba color of dots to matplotlib

According to the docs, this should work. Is not. I must be reading documents wrong. Does anyone have a fix?

from pylab import * N = 100 x = randn(N) y = randn(N) c = rand(N, 4) plot(x, y, 'o', c=c) 

Error in IPython laptop (python3):

 lib/python3.3/site-packages/IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter: to_rgba: Invalid rgba arg "[[ 0.29844256 0.96853857 0.75812229 0.22794978] [ 0.81606887 0.31641358 0.53254456 0.44198844] [ 0.06961026 0.3265891 0.03006253 0.00485412] [ 0.32580911 0.86645991 0.04140443 0.35550554] 

Error in simple IPython (python3):

 ValueError: to_rgba: Invalid rgba arg "[[ -2.78401664e-01 -8.33924015e-01 7.54508871e-01] [ -3.02839674e-01 -1.18292516e+00 -7.71274654e-01] [ -3.58099013e-01 -1.18899472e+00 1.39868995e+00] 

Docs:

 help(plot) .... In addition, you can specify colors in many weird and wonderful ways, including full names (``'green'``), hex strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or grayscale intensities as a string (``'0.8'``). Of these, the string specifications can be used in place of a ``fmt`` group, but the tuple forms can be used only as ``kwargs``. 
+5
source share
2 answers

From your description, does it sound like you need dots with several different colors?

If so, use a scatter , not a graph. (Basically the difference between the two. plot more efficient, but limited to one size / color for all points.)

For instance:

 import matplotlib.pyplot as plt import numpy as np N = 100 x = np.random.normal(0, 1, N) y = np.random.normal(0, 1, N) c = np.random.random((N, 4)) plt.scatter(x, y, c=c) plt.show() 
+11
source

Your call to rand ( numpy.random.rand ) returns numpy ndarray:

numpy.random.rand (d0, d1, ..., dn) Random values ​​in the given form.

Create an array of this form and distribute it with random samples from a uniform distribution over [0, 1).

Parameters:
d0, d1, ..., dn: int, optional

The dimensions of the returned array must be positive. If no argument is given, one floating Python is returned.

Return:
out: ndarray, shape (d0, d1, ..., dn)

Random values.

The matplotlib color argument must adhere to one of the following formats. In particular, you want to pass a python RGBA tuple.

Try something else like this:

 color = tuple(numpy.random.rand(4)) # 4 random vals between 0.0-1.0 
+3
source

Source: https://habr.com/ru/post/1202627/


All Articles