How can I use multiple colormaps for contour plots ?

34 次查看(过去 30 天)
In a 2D plot,I computed the probability distribution of some clusters. Now i want, for each cluster, to plot a contour plot of the cluster distribution. In order to be able to differentiate the different clusters, I want to have different color for the lines of each clusters. Moreover, I want lines that depends on the level of distribution.
For example, if a cluster color is red, I want to plot 20 contour lines (designed by contour) :
  • For low level lines : black lines
  • For high level lines : red lines
By now, I manage to choose the line color but it's the same color for every line of one cluster. I also tried to create one axis for each cluster but the colormap applied at the end is the the one from the last cluster.
To give an exemple, this image shows the scattered points and three clusters
What I'm trying to obtain is this (I merged 3 images in the following) :

采纳的回答

Dave B
Dave B 2021-11-16
编辑:Dave B 2021-11-16
You can totally do this with three axes, I'm guessing the only bit you needed was to target the specific axes when setting the colormap. In theory, you could maybe pull this off with one axes, making one contour, and offsetting the values that go into that contour...but it would be a huge pain!
Here's the three axes solution:
clf
ax1=axes;hold on
x=randn(10000,1)/20;
y=randn(10000,1)/5 + 2;
scatter(x,y,'.','MarkerEdgeColor',[.7 .7 .7])
[z,xb,yb]=histcounts2(x,y);
[~,h]=contour(xb(1:end-1)+diff(xb/2),yb(1:end-1)+diff(yb/2),z','LineWidth',1);
ax2=axes;hold on
x=randn(10000,1)/5 + 2;
y=randn(10000,1)/20;
scatter(x,y,'.','MarkerEdgeColor',[.7 .7 .7])
[z,xb,yb]=histcounts2(x,y);
[~,h]=contour(xb(1:end-1)+diff(xb/2),yb(1:end-1)+diff(yb/2),z','LineWidth',1);
ax2.Visible='off';
ax3=axes;hold on
x=randn(10000,1)/10;
y=randn(10000,1)/10;
scatter(x,y,'.','MarkerEdgeColor',[.7 .7 .7])
[z,xb,yb]=histcounts2(x,y);
[~,h]=contour(xb(1:end-1)+diff(xb/2),yb(1:end-1)+diff(yb/2),z','LineWidth',1);
ax3.Visible='off';
linkaxes([ax1 ax2 ax3]) % this bit is critical, it makes sure that the limits match up
cvec=linspace(0,1,255)';
z=zeros(255,1);
colormap(ax1,[cvec z z])
colormap(ax2,[z cvec z])
colormap(ax3,[z z cvec ])
A neat trick you can consider, that I didn't do here, it to start off with a tiledlayout:
t = tiledlayout(1,1)
ax1=axes(t);
ax2=axes(t);
ax3=axes(t);
The tiledlayout is intended for making multiple axes in different tiles, but because its job is to align axes, it will (I think) make sure that the axes stay aligned if you add titles and such. Also you can put one tiledlayout in another, so you could make 6 plots that look like this in one figure!

更多回答(0 个)

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by