Matplotlib – 将颜色条添加到一系列线图

对于variablesz的许多不同值,我有两个variables(x,y)的线图序列。 我通常会添加像这样的传说的线图:

import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) # suppose mydata is a list of tuples containing (xs, ys, z) # where xs and ys are lists of x's and y's and z is a number. legns = [] for(xs,ys,z) in mydata: pl = ax.plot(xs,ys,color = (z,0,0)) legns.append("z = %f"%(z)) ax.legends(legns) plt.show() 

但是我有太多的图表,传说会覆盖图表。 我宁愿有一个颜色条指示与颜色对应的z值。 我无法在Galery中find类似的东西,我所有的尝试都是在颜色条失败的情况下处理的。 显然我必须在尝试添加一个颜色条之前创build一个图的集合。

是否有捷径可寻? 谢谢。

编辑(澄清):

我想要做这样的事情:

 import matplotlib.pyplot as plt import matplotlib.cm as cm fig = plt.figure() ax = fig.add_subplot(111) mycmap = cm.hot # suppose mydata is a list of tuples containing (xs, ys, z) # where xs and ys are lists of x's and y's and z is a number between 0 and 1 plots = [] for(xs,ys,z) in mydata: pl = ax.plot(xs,ys,color = mycmap(z)) plots.append(pl) fig.colorbar(plots) plt.show() 

但是这不会根据Matplotlib引用工作,因为一张图的列表不是一个“可映射的”,无论这意味着什么。

我使用LineCollection创build了另一个绘图函数:

 def myplot(ax,xs,ys,zs, cmap): plot = lc([zip(x,y) for (x,y) in zip(xs,ys)], cmap = cmap) plot.set_array(array(zs)) x0,x1 = amin(xs),amax(xs) y0,y1 = amin(ys),amax(ys) ax.add_collection(plot) ax.set_xlim(x0,x1) ax.set_ylim(y0,y1) return plot 

xsys是x和y坐标列表的列表, zs是每行着色不同条件的列表。 这感觉有点像一个混乱,虽然…我认为会有一个更加干净的方式来做到这一点。 我喜欢plt.plot()函数的灵活性。

这里有一个方法来做,同时仍然使用plt.plot()。 基本上,你做一个扔掉的阴谋,并从那里得到彩条。

 import matplotlib as mpl import matplotlib.pyplot as plt min, max = (-40, 30) step = 10 # Setting up a colormap that's a simple transtion mymap = mpl.colors.LinearSegmentedColormap.from_list('mycolors',['blue','red']) # Using contourf to provide my colorbar info, then clearing the figure Z = [[0,0],[0,0]] levels = range(min,max+step,step) CS3 = plt.contourf(Z, levels, cmap=mymap) plt.clf() # Plotting what I actually want X=[[1,2],[1,2],[1,2],[1,2]] Y=[[1,2],[1,3],[1,4],[1,5]] Z=[-40,-20,0,30] for x,y,z in zip(X,Y,Z): # setting rgb color based on z normalized to my range r = (float(z)-min)/(max-min) g = 0 b = 1-r plt.plot(x,y,color=(r,g,b)) plt.colorbar(CS3) # using the colorbar info I got from contourf plt.show() 

这有点浪费,但方便。 如果您制作多个图块,也不会浪费太多,因为您可以调用plt.colorbar()而不重新生成信息。

在这里输入图像说明

(我知道这是一个老问题,但是…)Colorbars需要一个matplotlib.cm.ScalarMappableplt.plot产生的行不是标量可映射的,因此,为了制作一个colorbar,我们需要做一个标量可映射。

好。 所以ScalarMappable的构造ScalarMappable需要一个cmap和一个norm实例。 (规范将数据缩放到0-1的范围内,你已经使用了cmaps并取0-1之间的数字并返回一个颜色)。 所以在你的情况下:

 import matplotlib.pyplot as plt sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.normalize(min=0, max=1)) plt.colorbar(sm) 

由于您的数据已经在0-1范围内,因此可以将sm创build简化为:

 sm = plt.cm.ScalarMappable(cmap=my_cmap) 

希望能帮助别人。

编辑 :对于matplotlib v1.2或更高的代码变成:

 import matplotlib.pyplot as plt sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.normalize(vmin=0, vmax=1)) # fake up the array of the scalar mappable. Urgh... sm._A = [] plt.colorbar(sm) 

编辑 :对于matplotlib v1.3或更高的代码变成:

 import matplotlib.pyplot as plt sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=1)) # fake up the array of the scalar mappable. Urgh... sm._A = [] plt.colorbar(sm)