为matplotlib图分配随机颜色

时间:2013-11-12 21:02:36

标签: python python-2.7 matplotlib rgb

我试图在matplotlib中绘制一些多边形,并且我已经能够使用预定义的HTML颜色,但我想使用随机RGB元组。但是,我无法弄明白。

以下是我一直在尝试的代码:

>>> import matplotlib
>>> matplotlib.use('agg')
>>> import matplotlib.pyplot as plt
>>> import random
>>> fig, ax = plt.subplots()
>>> ax.plot([1,2,3], [4,5,6], (.4, .5, .6))

我收到此错误:raise ValueError('third arg must be a format string')

我最终想做的是:

>>> import matplotlib
>>> import random
>>> matplotlib.use('agg')
>>> import matplotlib.pyplot as plt
>>> import random
>>> fig, ax = plt.subplots()
>>> ax.plot([1,2,3], [4,5,6], (random.random(), random.random(), random.random()))

有人可以帮帮我吗?感谢

2 个答案:

答案 0 :(得分:7)

获得随机颜色:

ax.plot([1,2,3], [4,5,6], color=random.rand(3,1))

答案 1 :(得分:2)

这个怎么样:

ax.plot([1,2,3], [4,5,6], color=(.4, .5, .6))

the documentation阅读,

  

您不需要使用格式字符串,它只是缩写。所有行属性都可以通过关键字参数控制。例如,您可以使用以下颜色设置颜色,标记,线条样式和标记颜色:

plot(x, y, color='green', linestyle='dashed', marker='o',
     markerfacecolor='blue', markersize=12).
相关问题