十六进制颜色列表可用于饼图,但不能用于p

2024-06-28 20:47:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试创建一个与matplotlib一起使用的颜色主题,它可以与饼图一起使用,但是在运行plt.plot(x,y,color = color_theme)时收到一条错误消息,说它是一个无效的RGBA参数

作品

import matplotlib.pyplot as plt
color_theme = ['#998166', '#bacfd9', '#bfbaa6', '#a0bab8', '#63605b', 
'#8f8a83', '#bdb6af', '#e8e5e3', '#634632']
x = list(range(1,10))
y = [1,2,3,4,.5,4,3,2,1]
plt.pie(x, colors = color_theme)

不工作

import matplotlib.pyplot as plt
color_theme = ['#998166', '#bacfd9', '#bfbaa6', '#a0bab8', '#63605b', 
'#8f8a83', '#bdb6af', '#e8e5e3', '#634632']
x = list(range(1,10))
y = [1,2,3,4,.5,4,3,2,1]
plt.plot(x,y,color = color_theme)

省去color参数也可以

我错过了什么


Tags: import参数plotmatplotlibaspltthemelist
1条回答
网友
1楼 · 发布于 2024-06-28 20:47:34

使用plt.plot绘制线。绘图中只有一行,因此在color=参数中只能给出一个值(可以有一行有多种颜色,还有其他问题)

您可以从color_theme列表中选择一个元素并选择该元素

plt.plot(x,y,color = color_theme[0]) # uses '#998166' for the color.

您可能需要使用散点图,它可以接受颜色列表:

color_theme = ['#998166', '#bacfd9', '#bfbaa6', '#a0bab8', '#63605b',
'#8f8a83', '#bdb6af', '#e8e5e3', '#634632']
x = list(range(1,10))
y = [1,2,3,4,.5,4,3,2,1]
plt.scatter(x, y, color = color_theme)
plt.show()

enter image description here

相关问题 更多 >