您收到的错误是由于您的定义
jet。您正在创建
Colormap名称为’jet’的基类,但这与获取’jet’颜色图的默认定义非常不同。永远不要直接创建此基类,而应仅实例化子类。
在示例中发现的是Matplotlib中的错误行为。运行此代码时,应该会产生一条更清晰的错误消息。
这是您的示例的更新版本:
import matplotlib.pyplot as pltimport matplotlib.colors as colorsimport matplotlib.cm as cmximport numpy as np# define some random data that emulates your indeded pre:NCURVES = 10np.random.seed(101)curves = [np.random.random(20) for i in range(NCURVES)]values = range(NCURVES)fig = plt.figure()ax = fig.add_subplot(111)# replace the next line #jet = colors.Colormap('jet')# withjet = cm = plt.get_cmap('jet') cNorm = colors.Normalize(vmin=0, vmax=values[-1])scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)print scalarMap.get_clim()lines = []for idx in range(len(curves)): line = curves[idx] colorVal = scalarMap.to_rgba(values[idx]) colorText = ( 'color: (%4.2f,%4.2f,%4.2f)'%(colorVal[0],colorVal[1],colorVal[2]) ) retLine, = ax.plot(line, color=colorVal, label=colorText) lines.append(retLine)#added this to get the legend to workhandles,labels = ax.get_legend_handles_labels()ax.legend(handles, labels, loc='upper right')ax.grid()plt.show()
导致:
使用a
ScalarMappable是对我相关答案中提出的方法的一种改进:
使用matplotlib创建20多种独特的图例颜色
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)