Search code examples
matplotlibcolorshexruntime-errorvalueerror

Can anyone tell me the solution of this ValueError in MatPlotLib?


When I'm plotting some random scatter plot an error occurred. Can someone solve it?
The code is

# Date 27-06-2021

import time
import matplotlib.pyplot as plt
import numpy as np
import random as rd


def rd_color():
    random_number = rd.randint(0, 16777215)
    hex_number = str(hex(random_number))
    hex_number = '#' + hex_number[2:]
    return hex_number


arr1_for_x = np.linspace(10, 99, 1000)
arr1_for_y = np.random.uniform(10, 99, 1000)

# print(rd_color())

for i in range(1000):
    plt.scatter(arr1_for_x[i:i+1], arr1_for_y[i:i+1], s=5,
                linewidths=0, color=rd_color())


plt.show()

and the ValueError is

ValueError: 'color' kwarg must be a color or sequence of color specs. For a sequence of values to be color-mapped, use the 'c' argument instead.

Traceback (most recent call last):
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4289, in _parse_scatter_color_args
    mcolors.to_rgba_array(kwcolor)
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\colors.py", line 367, in to_rgba_array
    raise ValueError("Using a string of single character colors as "
ValueError: Using a string of single character colors as a color sequence is not supported. The colors can be passed as an explicit list instead. 

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Google Drive\tp\Programming\Python\tp2.py", line 24, in <module>
    plt.scatter(arr1_for_x[i:i+1], arr1_for_y[i:i+1], s=5,
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 3068, in scatter
    __ret = gca().scatter(
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py", line 1361, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4516, in scatter
    self._parse_scatter_color_args(
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4291, in _parse_scatter_color_args
    raise ValueError(
ValueError: 'color' kwarg must be an color or sequence of color specs.  For a sequence of values to be color-mapped, use the 'c' argument instead.

after this error I also use c in-place of color

# Date 27-06-2021

import time
import matplotlib.pyplot as plt
import numpy as np
import random as rd


def rd_color():
    random_number = rd.randint(0, 16777215)
    hex_number = str(hex(random_number))
    hex_number = '#' + hex_number[2:]
    return str(hex_number)


arr1_for_x = np.linspace(10, 99, 1000)
arr1_for_y = np.random.uniform(10, 99, 1000)

# print(rd_color())

for i in range(1000):
    plt.scatter(arr1_for_x[i:i+1], arr1_for_y[i:i+1], s=5,
                linewidths=0, c=rd_color())


plt.show()

but again error occured this time the error is

Traceback (most recent call last):
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4350, in _parse_scatter_color_args
    colors = mcolors.to_rgba_array(c)
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\colors.py", line 367, in to_rgba_array
    raise ValueError("Using a string of single character colors as "
ValueError: Using a string of single character colors as a color sequence is not supported. The colors can be passed as an explicit list instead.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Google Drive\tp\Programming\Python\tp2.py", line 22, in <module>
    plt.scatter(arr1_for_x[i:i+1], arr1_for_y[i:i+1], s=5,
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 3068, in scatter
    __ret = gca().scatter(
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py", line 1361, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4516, in scatter
    self._parse_scatter_color_args(
  File "C:\Users\amanr\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 4359, in _parse_scatter_color_args
    raise ValueError(
ValueError: 'c' argument must be a color, a sequence of colors, or a sequence of numbers, not #814aa

please anyone resolve/spell it out for me.


Solution

  • The way I read Matplotlib's Tutorial on Specifying Colours, the hex literals need to have exactly 6 or 3 "hex digits".