Fill color inside stair.
0 Commenti
Risposta accettata
8 Commenti
Più risposte (1)
Hi @Yang Hu,
You have already created the histograms using the histogram function. This part of your code is correct and will generate the stair plots. To fill the area under the stairs, you need to extract the x and y data from the histogram objects. The Values property of the histogram will give you the heights of the bars, and the BinEdges property will provide the x-coordinates. Then use the fill function to create filled areas under the stair plots. This function will require the x-coordinates and y-coordinates of the vertices of the polygon you want to fill. Let me illustrate by implementing these steps in code below. For more information on this function, please refer to
https://www.mathworks.com/help/matlab/ref/fill.html
% data
data1 = randn(1000, 1); % Replace with your actual data
data2 = randn(1000, 1); % Replace with your actual data
figure;
% Create the first histogram
h1 = histogram(data1, 'BinWidth', 0.1, 'DisplayStyle', 'stairs', 'Normalization',
'probability');
h1.EdgeColor = 'k';
h1.LineWidth = 2;
% Hold on to plot the second histogram
hold on;
% Create the second histogram
h2 = histogram(data2, 'BinWidth', 0.1, 'DisplayStyle', 'stairs', 'Normalization',
'probability');
h2.EdgeColor = [0.772, 0.012, 0.314];
h2.LineWidth = 2;
% Set x-axis limits and ticks
xlim([0 2]);
xticks(0:0.1:2);
% Customize font and frame properties
ax = gca;
ax.FontSize = 16;
ax.FontWeight = 'bold';
ax.LineWidth = 4;
% Fill the area under the first histogram
x1 = [h1.BinEdges, h1.BinEdges(end)]; % X-coordinates
y1 = [0, h1.Values, 0]; % Y-coordinates
fill(x1, y1, 'k', 'FaceAlpha', 0.2, 'EdgeColor', 'none'); % Fill with black color
% Fill the area under the second histogram
x2 = [h2.BinEdges, h2.BinEdges(end)]; % X-coordinates
y2 = [0, h2.Values, 0]; % Y-coordinates
% Fill with specified color
fill(x2, y2, [0.772, 0.012, 0.314], 'FaceAlpha', 0.2, 'EdgeColor', 'none');
hold off; % Release the hold on the current figure
Please see attached.
![](/matlabcentral/answers/uploaded_files/1754449/IMG_7481.jpeg)
![](/matlabcentral/answers/uploaded_files/1754454/IMG_7479.jpeg)
Note: Replace data1 and data2 with your actual datasets. Hope this helps.
If you have any further questions or need additional assistance, feel free to ask!
0 Commenti
Vedere anche
Categorie
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!