如何将rgb颜色值传递给python的matplotlib eventplot?

2024-03-29 07:50:59 发布

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

我只是想用matplotlib的eventplot绘制一些特定颜色的记号。我在Jupyter笔记本中运行Python 3,并使用%matplotlib inline。

下面是一个示例代码:

import numpy as np
import matplotlib.pyplot as plt    
spikes = 100*np.random.random(100)
plt.eventplot(spikes, orientation='horizontal', linelengths=0.9, color=[0.3,0.3,0.5])

它输出以下错误:

ValueError: colors and positions are unequal sized sequences

这个错误可能是因为我没有提供与数据长度相同的颜色列表(但我不希望它们都是相同的颜色!)。当我使用像“深红色”或“兰花”这样的颜色字符串时,也会出现错误。但当我使用一个简单的单字母字符串如“r”时,它就起作用了。

我真的只限于使用极为有限的一个字母的颜色字符串'r'、'b'、'g'、'k'、'm'、'y'等等。。。或者在使用此事件图时制作一个长颜色列表?


Tags: 字符串import列表matplotlib颜色as错误np
1条回答
网友
1楼 · 发布于 2024-03-29 07:50:59

根据docs

you can pass an (r, g, b) or (r, g, b, a) tuple, where each of r, g, b and a are in the range [0,1].

import numpy as np
import matplotlib.pyplot as plt

spikes = 100*np.random.random(100)
plt.eventplot(spikes, orientation='horizontal', linelengths=0.9, color = [(0.3,0.3,0.5)])

plt.show()

相关问题 更多 >