How to generate random colors in matplotlib?

前端 未结 11 2226
长情又很酷
长情又很酷 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条回答
  •  眼角桃花
    2020-12-12 16:24

    Based on Ali's and Champitoad's answer:

    If you want to try different palettes for the same, you can do this in a few lines:

    cmap=plt.cm.get_cmap(plt.cm.viridis,143)

    ^143 being the number of colours you're sampling

    I picked 143 because the entire range of colours on the colormap comes into play here. What you can do is sample the nth colour every iteration to get the colormap effect.

    n=20 for i,(x,y) in enumerate(points): plt.scatter(x,y,c=cmap(n*i))

提交回复
热议问题