How can i change color of a specific bin using histogram2 or anything similar

조회 수: 11 (최근 30일)
suppose i have some data which i want to cluster into 3D histogram
x = 100*randn(10000,1);
y = 100*randn(10000,1);
h2 = histogram2(x,y,-100:20:100,-100:20:100,'FaceColor','flat')
h2 =
Histogram2 with properties: Data: [10000×2 double] Values: [10×10 double] NumBins: [10 10] XBinEdges: [-100 -80 -60 -40 -20 0 20 40 60 80 100] YBinEdges: [-100 -80 -60 -40 -20 0 20 40 60 80 100] BinWidth: [20 20] Normalization: 'count' FaceColor: 'flat' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
i want to change the color of a specific bin lets say bin which belongs to row and column 5
i would want something like this:
h2.Cdata(5,5,:) = [1 0 0] % set bin color to red
Unrecognized method, property, or field 'Cdata' for class 'matlab.graphics.chart.primitive.Histogram2'.
but theres no Cdata property

채택된 답변

Adam Danz
Adam Danz 2023년 3월 26일
One way to do this is to plot a histogram for each group.
rng('default') % For reproducibility of this example
x = 100*randn(10000,1);
y = 100*randn(10000,1);
[N, xEdges, yEdges] = histcounts2(x,y,-100:20:100,-100:20:100);
% Plot group 1
N1 = N;
N1(5,5) = 0;
h0 = histogram2('XBinEdges',xEdges,'YBinEdges',yEdges,'BinCounts',N1,'FaceColor','Flat');
% Plot group 2
N2 = N(5,5);
hold on
h1 = histogram2('XBinEdges',xEdges(4:5),'YBinEdges',yEdges(4:5),'BinCounts',N2,'FaceColor','r');
Another solution may be to use bar3 with a width of 1 but that has the following issues
  • bar3 does not have an x input. Solution is to adjust the x values after plotting.
  • the y input to bar3 defines the center of each bin, not the edge. Solution is to compute the centers.
  • defining colors to individual bars might be tricky since a handle is returned for each row along the x axis
  댓글 수: 2
daniel mitchell
daniel mitchell 2023년 3월 27일
thank you this workes perfectly.
Probably it will be a good suggestion for MATLAB to add a property for histogram2 class which will enable modifying colors directly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by