How to generate random colors in matplotlib?

前端 未结 11 2241
长情又很酷
长情又很酷 2020-12-12 16:13

What\'s the trivial example of how to generate random colors for passing to plotting functions?

I\'m calling scatter inside a loop and want each plot a different col

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 16:18

    elaborating @john-mee 's answer, if you have arbitrarily long data but don't need strictly unique colors:

    for python 2:

    from itertools import cycle
    cycol = cycle('bgrcmk')
    
    for X,Y in data:
        scatter(X, Y, c=cycol.next())
    

    for python 3:

    from itertools import cycle
    cycol = cycle('bgrcmk')
    
    for X,Y in data:
        scatter(X, Y, c=next(cycol))
    

    this has the advantage that the colors are easy to control and that it's short.

提交回复
热议问题