Matplotlib-将颜色条添加到折线图序列 [英] Matplotlib - add colorbar to a sequence of line plots
问题描述
对于变量z的许多不同值,我有两个变量(x,y)的折线图序列.我通常会添加带有以下图例的折线图:
I have a sequence of line plots for two variables (x,y) for a number of different values of a variable z. I would normally add the line plots with legends like this:
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值.我在画廊中找不到任何类似的东西,而我的所有尝试都确实处理了色标.显然,在尝试添加颜色条之前,必须创建一个绘图集合.
But I have too many graphs and the legends will cover the graph. I'd rather have a colorbar indicating the value of z corresponding to the color. I can't find anything like that in the galery and all my attempts do deal with the colorbar failed. Apparently I must create a collection of plots before trying to add a colorbar.
有没有简单的方法可以做到这一点?谢谢.
Is there an easy way to do this? Thanks.
编辑(说明):
我想做这样的事情:
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参考,这将不起作用,因为无论这意味着什么,绘图列表都不是可映射的".
But this won't work according to the Matplotlib reference because a list of plots is not a "mappable", whatever this means.
我已经使用LineCollection
创建了另一种绘图功能:
I've created an alternative plot function using LineCollection
:
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
xs
和ys
是x和y坐标列表的列表,而zs
是使每行着色的不同条件的列表.不过,这感觉有点像杂事……我认为会有一种更简洁的方法来做到这一点.我喜欢plt.plot()
函数的灵活性.
xs
and ys
are lists of lists of x and y coordinates and zs
is a list of the different conditions to colorize each line. It feels a bit like a cludge though... I thought that there would be a more neat way to do this. I like the flexibility of the plt.plot()
function.
推荐答案
(我知道这是一个老问题,但是...)彩条要求使用matplotlib.cm.ScalarMappable
,plt.plot
生成的线不是标量可映射的,因此,为了制作一个颜色条,我们将需要制作一个标量可映射对象.
(I know this is an old question but...) Colorbars require a matplotlib.cm.ScalarMappable
, plt.plot
produces lines which are not scalar mappable, therefore, in order to make a colorbar, we are going to need to make a scalar mappable.
好的.因此,ScalarMappable
的构造函数采用一个cmap
和一个norm
实例. (规范将数据缩放到0-1范围,您已经使用过的cmap并在0-1之间取一个数字,并返回一种颜色).因此,在您的情况下:
Ok. So the constructor of a ScalarMappable
takes a cmap
and a norm
instance. (norms scale data to the range 0-1, cmaps you have already worked with and take a number between 0-1 and returns a color). So in your case:
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
的创建简化为:
Because your data is in the range 0-1 already, you can simplify the sm
creation to:
sm = plt.cm.ScalarMappable(cmap=my_cmap)
希望对某人有帮助.
编辑:对于matplotlib v1.2或更高版本,代码变为:
EDIT: For matplotlib v1.2 or greater the code becomes:
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或更高版本,代码变为:
EDIT: For matplotlib v1.3 or greater the code becomes:
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 v3.1或更高版本,简化为:
EDIT: For matplotlib v3.1 or greater simplifies to:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=1))
plt.colorbar(sm)
这篇关于Matplotlib-将颜色条添加到折线图序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!