您可以创建自己的legend:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.lines import Line2D
import numpy as np
list_integers = range(0,100) #the range and variance of this list will vary
x = range(0,100)
y = range(0,100)
cmap=cm.rainbow(np.array(list_integers)/np.mean(list_integers))
custom_lines = [Line2D([0], [0], color=cmap[i], lw=2) for i in range(len(x))]
fig, ax = plt.subplots()
ax.scatter(x, y, color=cmap)
ax.legend(custom_lines, [str(s) for s in x], ncol=5,bbox_to_anchor=(1.1, 1.05))
编辑:
如果您想创建自己的彩条,那么您可以使用我在我的 cmets 中提到的知识(来源:1、colorbar、3、4)和ListedColormap:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.lines import Line2D
import matplotlib.colors
import numpy as np
list_integers = range(0,100) #the range and variance of this list will vary
x = range(0,100)
y = range(0,100)
# create rgb value mapping
cmap=cm.rainbow(np.array(list_integers)/np.mean(list_integers))
# create cmap
my_cmap = matplotlib.colors.ListedColormap(cmap, name='my_colormap')
# create norm
norm = matplotlib.colors.BoundaryNorm(x, len(x))
# create lines for the legend
custom_lines = [Line2D([0], [0], color=cmap[i], lw=2) for i in range(len(x))]
fig, ax = plt.subplots()
ax.scatter(x, y, c=cmap, norm=norm)
ax.legend(custom_lines, [str(s) for s in x], ncol=5,bbox_to_anchor=(1.2, 1.05))
# create the colourbar
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=norm)
plt.colorbar(sm, ticks=np.arange(0,len(x),10))