如何解决谁能告诉我 MatPlotLib 中这个 ValueError 的解决方案?
当我绘制一些随机散点图时出现错误。有人能解决吗?
代码是
# 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,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()
而 ValueError 是
ValueError: 'color' kwarg 必须是颜色或颜色规格序列。对于要进行颜色映射的值序列,请改用 'c' 参数。
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],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.
在这个错误之后我也用 c 代替了颜色
# 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,c=rd_color())
plt.show()
但又发生了错误 这次的错误是
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",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,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
请任何人为我解决/拼写出来。
解决方法
这会起作用:
for i in range(1000):
myColor=rd_color()
plt.scatter(arr1_for_x[i:i+1],arr1_for_y[i:i+1],s=5,linewidths=0,color=[myColor])
plt.show()
因为您无法在此颜色规范中调用函数,并且您的颜色必须在列表中,因此请为该颜色定义一个变量并将其作为列表传递给您的颜色参数。
,我读 Matplotlib's Tutorial on Specifying Colours 的方式,十六进制文字需要正好有 6 或 3 个“十六进制数字”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。