简体   繁体   English

具有相同颜色的散点图和等高线图

[英]Scatter plot and contour plot with same colors

Is it possible to force plt.scatter into the same color levels as plt.contourf and plt.contour ?是否可以强制plt.scatter进入与plt.contourfplt.contour相同的颜色级别? For example, I have code that makes a plot like this:例如,我的代码可以绘制如下图:

在此处输入图片说明

to make the first subplot, I use为了制作第一个子图,我使用

cs=m[0].scatter(xs,ys,c=obsData,cmap=plt.cm.jet)
m.colorbar(cs)

To make the second subplot, I use为了制作第二个子图,我使用

cs2=m[1].contourf(x,y,areaData,cmap=cs.cmap)

And for each subsequent subplot, I use对于每个后续的子图,我使用

m[ind].contourf(x,y,areaData,cmap=cs.cmap,levels=cs2.levels

where areaData is recalculated within a loop.其中 areaData 在循环中重新计算。

My question is, how can I force the first subplot to have the same colors as the other subplots?我的问题是,如何强制第一个子图与其他子图具有相同的颜色? I am looking for an equivalent to the levels=cs2.levels keyword argument.我正在寻找与levels=cs2.levels关键字参数等效的参数。

As you noted in a comment, your scatter and contour data are not directly related, but you want to display them on the same colormap.正如您在评论中指出的,您的散点数据和等高线数据没有直接关系,但您希望将它们显示在同一个颜色图上。

I suggest setting a common colour span that contains both sets of data.我建议设置一个包含两组数据的通用颜色范围。 Since obsData refers to the scatter points and areaData to the contours, I'd set由于obsData指的是散点和areaData到轮廓,我会设置

vmin,vmax = (fun(np.concatenate([obsData,areaData])) for fun in (np.min,np.max))

to determine the span of the collected data set (obviously, to be generalized for multiple input data sets).确定所收集数据集的跨度(显然,要推广到多个输入数据集)。 These can be passed to scatter and contourf to set the limits of the colour mapping:这些可以传递给scattercontourf来设置颜色映射的限制:

cs = m[0].scatter(xs,ys,c=obsData,cmap=plt.cm.viridis,vmin=vmin,vmax=vmax)
cs2 = m[1].contourf(x,y,areaData,cmap=cs.cmap,vmin=vmin,vmax=vmax)

Some manual increase of the span might be in order to obtain a pretty result.一些手动增加跨度可能是为了获得漂亮的结果。

Note that I changed the colormap to viridis .请注意,我将颜色图更改为viridis If you really want to fairly represent your data, this should be your first step .如果您真的想公平地表示您的数据,这应该是您的第一步

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM