[英]How to have a large matplotlib colormap without having to manually define each color?
I have a bunch of data I want to graph. 我有一堆要绘制的数据。 Right now, there are 500 possible values.
现在有500个可能的值。 I want to color the data based on value.
我想根据值为数据着色。 To draw a scatterplot now, I use the following for plt.scatter:
现在要绘制散点图,我对plt.scatter使用以下代码:
c = colormap = np.array(['red', 'lime', 'black'])
Hardcoding 500 colors in that np.array seems impractical. 在该np.array中硬编码500种颜色似乎是不切实际的。
plt.scatter(pandaframe['WIDTH'], pandaframe['LENGTH'], c=colormap[model.labels_], s=40)
Is there a way I can make it so that I can get 500 different colors without having to hardcode the colormap? 有没有一种方法可以使我获得500种不同的颜色而不必对颜色图进行硬编码?
If you don't mind using built-in colormaps, you can find guidance here . 如果您不介意使用内置的颜色表,则可以在此处找到指南。 To use it, your
c
should be your 500 possible values which determines corresponding color. 要使用它,您的
c
应该是您的500个可能的值,该值确定相应的颜色。 Use cmap
to specify which colormap you want to use. 使用
cmap
指定要使用的颜色图。 You can also draw a colorbar to give an idea about the colormap. 您还可以绘制一个颜色条以提供有关颜色图的想法。 A simple complete example:
一个简单的完整示例:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(500)
y = x.copy()
np.random.shuffle(x)
plt.scatter(x, y, c=y, cmap='summer')
plt.colorbar()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.