Define vector to use for colorbar in MATLAB -
i plotting bar
+scatter
plot scatter points colored according separate variable. problem having colorbar
using wrong values @ moment. if plot scatter
plot , add colorbar
, range of colorbar
correct.
i using matlab 2016a.
please find working example of code below:
figure subplot(2,1,1) = 1; b = 2; r = (b-a).*rand(1,7) + a; y = r; rr = (b-a).*rand(1,7) + a; z = rr; x = [1:7]; zz = rand(1,7) yyaxis left hold on = 1:7 h=bar(i,y(i), 'facecolor',[1 1 1], 'linewidth',3); yb(i) = cat(1, h.ydata); xb(i) = bsxfun(@plus, h(1).xdata, [h.xoffset]'); if zz(i) < 0.0300000 set(h,'edgecolor','k'); elseif zz(i) < 0.050000000 set(h,'edgecolor','b'); elseif zz(i) < 0.070000000 set(h,'edgecolor','g'); else set(h,'edgecolor','r'); end end ylabel('hm', 'fontsize', 12, 'fontweight', 'bold') i1=1:7 t = text(xb(i1)-0.2,yb(i1),num2str(yb(i1),'%0.3f'),... 'horizontalalignment','center',... 'verticalalignment','bottom') s = t.fontsize; t.fontsize = 12; t.fontweight = 'bold'; end yyaxis right pointsize = 40; hh = scatter(x,z,pointsize, zz,'filled') cc = colormap([hsv(20)]) c = colorbar c.label.string = 'pos'; set(gca,'ydir','reverse') ylabel('ok', 'fontsize', 12, 'fontweight', 'bold') lgd = legend([h, hh], 'hm', 'ok') subplot(2,1,2) x = [1:8] = 1; b = 2; r = (b-a).*rand(1,8) + a; y = r; rr = (b-a).*rand(1,8) + a; z = rr; zz = rand(1,8); yyaxis left hold on = 1:8 h=bar(i,y(i), 'facecolor',[1 1 1], 'linewidth',3); yb(i) = cat(1, h.ydata); xb(i) = bsxfun(@plus, h(1).xdata, [h.xoffset]'); if zz(i) < 0.0300000 set(h,'edgecolor','k'); elseif zz(i) < 0.050000000 set(h,'edgecolor','b'); elseif zz(i) < 0.070000000 set(h,'edgecolor','g'); else set(h,'edgecolor','r'); end end i1=1:8 t = text(xb(i1)-0.2,yb(i1),num2str(yb(i1),'%0.3f'),... 'horizontalalignment','center',... 'verticalalignment','bottom') s = t.fontsize; t.fontsize = 12; t.fontweight = 'bold'; end ylabel('hm', 'fontsize', 12, 'fontweight', 'bold') yyaxis right pointsize = 40; hh = scatter(x,z,pointsize, zz,'filled') set(gca,'ydir','reverse') ylabel('ok', 'fontsize', 12, 'fontweight', 'bold') c = colorbar c.label.string = 'pos'; lgd = legend([h, hh], 'hm', 'ok') %title(lgd,'my legend title') hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
edit working example of 1 solution question.
figure = 1; b = 2; r = (b-a).*rand(1,7) + a; y = r; rr = (b-a).*rand(1,7) + a; z = rr; x = [1:7]; zz = rand(1,7) colormap(jet) yyaxis left hold on = 1:length(y) h=bar(i,y(i), 'facecolor',[1 1 1], 'linewidth',3); yb(i) = cat(1, h.ydata); xb(i) = bsxfun(@plus, h(1).xdata, [h.xoffset]'); if zz(i) < 0.0300000 set(h,'edgecolor','k'); elseif zz(i) < 0.050000000 set(h,'edgecolor','k'); elseif zz(i) < 0.070000000 set(h,'edgecolor','k'); else set(h,'edgecolor','k'); end end cco = min(zz) cct = max(zz) caxis([cco cct]) coloo = colorbar coloo.label.string = 'cbar'; %h = bar(y, 0.2, 'facecolor',[1 1 1], 'edgecolor',[0 0 0],'linewidth',2); %yb = cat(1, h.ydata); %xb = bsxfun(@plus, h(1).xdata, [h.xoffset]'); i1=1:7 % numel(yb) t = text(xb(i1)-0.3,yb(i1),num2str(yb(i1),'%0.3f'),... 'horizontalalignment','center',... 'verticalalignment','bottom') s = t.fontsize; t.fontsize = 12; t.fontweight = 'bold'; end ylabel('ok', 'fontsize', 12, 'fontweight', 'bold') yyaxis right pointsize = 80; hh = scatter(x,z,pointsize, zz,'filled') set(gca,'ydir','reverse') ylabel('mm', 'fontsize', 12, 'fontweight', 'bold') c = colorbar c.label.string = 'cbar'; lgd = legend([h, hh], 'ok', 'mm') %title(lgd,'my legend title') hold off
the problem zz
. define vector, scatter
uses figures colormap choose colors reltive values in zz
. instead, define color matrix, i.e. n-by-3 matrix each row define 1 color in rgb values between 0 1.
in example, try this: zz = rand(7,3)
first subplot, , zz = rand(8,3)
second.
here code, set zz
matrix of colors, , use both scatters:
figure subplot(2,1,1) = 1; b = 2; r = (b-a).*rand(1,7) + a; y = r; rr = (b-a).*rand(1,7) + a; z = rr; x = 1:7; %%%% part new: zz = [0 0.447 0.741; 0.85 0.325 0.098; 0.929 0.694 0.125; 0.494 0.184 0.556; 0.466 0.674 0.188; 0.301 0.745 0.933; 0.635 0.078 0.184; 0.432 0.915 0.121]; %%%%%%%%%%%%%%%%%%%%%%%% yyaxis left hold on = 1:7 h=bar(i,y(i), 'facecolor',[1 1 1], 'linewidth',3); yb(i) = cat(1, h.ydata); xb(i) = bsxfun(@plus, h(1).xdata, [h.xoffset]'); if zz(i) < 0.0300000 set(h,'edgecolor','k'); elseif zz(i) < 0.050000000 set(h,'edgecolor','b'); elseif zz(i) < 0.070000000 set(h,'edgecolor','g'); else set(h,'edgecolor','r'); end end ylabel('hm', 'fontsize', 12, 'fontweight', 'bold') i1=1:7 t = text(xb(i1)-0.2,yb(i1),num2str(yb(i1),'%0.3f'),... 'horizontalalignment','center',... 'verticalalignment','bottom'); s = t.fontsize; t.fontsize = 12; t.fontweight = 'bold'; end yyaxis right pointsize = 40; hh = scatter(x,z,pointsize, zz(1:numel(x),:),'filled'); cc = colormap(hsv(20)); c = colorbar; c.label.string = 'pos'; set(gca,'ydir','reverse') ylabel('ok', 'fontsize', 12, 'fontweight', 'bold') legend([h, hh], 'hm', 'ok'); subplot(2,1,2) x = 1:8; = 1; b = 2; r = (b-a).*rand(1,8) + a; y = r; rr = (b-a).*rand(1,8) + a; z = rr; yyaxis left hold on = 1:8 h=bar(i,y(i), 'facecolor',[1 1 1], 'linewidth',3); yb(i) = cat(1, h.ydata); xb(i) = bsxfun(@plus, h(1).xdata, [h.xoffset]'); if zz(i) < 0.0300000 set(h,'edgecolor','k'); elseif zz(i) < 0.050000000 set(h,'edgecolor','b'); elseif zz(i) < 0.070000000 set(h,'edgecolor','g'); else set(h,'edgecolor','r'); end end i1=1:8 t = text(xb(i1)-0.2,yb(i1),num2str(yb(i1),'%0.3f'),... 'horizontalalignment','center',... 'verticalalignment','bottom'); s = t.fontsize; t.fontsize = 12; t.fontweight = 'bold'; end ylabel('hm', 'fontsize', 12, 'fontweight', 'bold') yyaxis right pointsize = 40; hh = scatter(x,z,pointsize, zz(1:numel(x),:),'filled'); set(gca,'ydir','reverse') ylabel('ok', 'fontsize', 12, 'fontweight', 'bold') c = colorbar; c.label.string = 'pos'; lgd = legend([h, hh], 'hm', 'ok'); %title(lgd,'my legend title') hold off
and notice zz(1:numel(x),:)
within scatter
command, suite size of data.
this result:
edit: after response see want scatter in colors colormap still there many improvements can add code, publish here second version works second edit, shorter , simpler. know tricks, they'll save time ;)
a = 1; b = 2; r = (b-a).*rand(1,7) + a; y = r; rr = (b-a).*rand(1,7) + a; z = rr; x = 1:7; zz = rand(1,7); colormap(jet) caxis([min(zz) max(zz)]) bary = diag(y); bary(bary==0) = nan; col_thresh = [0.03 0.05 0.07 1]; bar_color = {'r'; 'g'; 'b'; 'k'}; bar_group = sum(bsxfun(@lt,zz.',col_thresh),2); yyaxis left hold on h = bar(bary,'stacked','facecolor',[1 1 1], 'linewidth',3); set(h,{'edgecolor'},bar_color(bar_group)) text(x-0.3,y,num2str(y.','%0.3f'),... 'horizontalalignment','center',... 'verticalalignment','bottom','fontsize',12,... 'fontweight','bold'); ylabel('ok', 'fontsize', 12, 'fontweight', 'bold') yyaxis right pointsize = 80; hh = scatter(x,z,pointsize,zz,'filled'); hh.parent.ydir = 'reverse'; ylabel('mm', 'fontsize', 12, 'fontweight', 'bold') c = colorbar; c.label.string = 'cbar'; legend([h(1), hh], 'ok', 'mm'); hold off
Comments
Post a Comment