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

12 次查看(过去 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

更多回答(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