Azzera filtri
Azzera filtri

Fill color inside stair.

54 visualizzazioni (ultimi 30 giorni)
Yang Hu
Yang Hu il 17 Ago 2024 alle 4:30
Commentato: Umar il 18 Ago 2024 alle 18:01
Hi all, I has two figures are plotted together. I want to fille colors within stairs but don't know how to do.
figure;
h1 = histogram(data1, 'BinWidth', 0.1,'DisplayStyle','stairs','Normalization', 'probability');
h1.EdgeColor = 'k';
h1.LineWidth = 2;
% h1.FaceAlpha = 0.2;
hold on;
h2 = histogram(data2, 'BinWidth', 0.1,'DisplayStyle','stairs','Normalization', 'probability');
h2.EdgeColor = [0.772, 0.012, 0.314];
h2.LineWidth = 2;
% h2.FaceAlpha = 0.2; %
xlim([0 2]);
xticks(0:0.1:2);
% Customize font and frame properties
ax = gca;
ax.FontSize = 16;
ax.FontWeight = 'bold';
ax.LineWidth = 4;

Risposta accettata

Star Strider
Star Strider il 17 Ago 2024 alle 11:05
The stairs plots are relatively straightforward to work with, however it is necessary to get their outputs in order to plot them correctly with the patch function. This approach gets the information from the histogram plots and then uses them with the stairs function to get the information necessary to plot them correctly with the patch function.
Try this —
data1 = randn(1E+3,1)*0.25+1.2; % Create Data
data2 = randn(1E+3,1)*0.2+1; % Create Data
figure;
h1 = histogram(data1, 'BinWidth', 0.1,'DisplayStyle','stairs','Normalization', 'probability');
h1.EdgeColor = 'k';
h1.LineWidth = 2;
hold on
h2 = histogram(data2, 'BinWidth', 0.1,'DisplayStyle','stairs','Normalization', 'probability');
h2.EdgeColor = [0.772, 0.012, 0.314];
h2.LineWidth = 2;
xv1 = h1.BinEdges; % First 'histogram' Independent Variable Vector
yv1 = h1.Values; % 'histogram' Dependent Variable Vector
[sx1,sy1] = stairs(xv1(1:end-1), yv1); % Re-Create The 'stairs' (x,y) Values
patch([sx1; flip(sx1)], [zeros(size(sy1)); flip(sy1)], 'k', 'FaceAlpha',0.2) % Use Them To Plot The Appropriate 'patch' Object
xv2 = h2.BinEdges; % Do The Same With The Second 'histogram'
yv2 = h2.Values;
[sx2,sy2] = stairs(xv2(1:end-1), yv2);
patch([sx2; flip(sx2)], [zeros(size(sy2)); flip(sy2)], h2.EdgeColor, 'FaceAlpha',0.2)
hold off
xlim([0 2]);
xticks(0:0.1:2);
ax = gca;
ax.FontSize = 16;
ax.FontWeight = 'bold';
ax.LineWidth = 4;
It would help to have ‘data1’ and ‘data2’ however this should work with them, regardless.
.
  8 Commenti
Star Strider
Star Strider il 18 Ago 2024 alle 10:20
@Umar — Please stop.
Umar
Umar il 18 Ago 2024 alle 18:01
Hi @Yang Hu, Thank you for your kind words regarding the code. I truly appreciate your feedback and am glad to hear that you found it amazing. If you have any further questions or require additional assistance, please do not hesitate to reach out. I am here to help.

Accedi per commentare.

Più risposte (1)

Umar
Umar il 17 Ago 2024 alle 6:05
Modificato: Umar il 17 Ago 2024 alle 6:07

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.

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!

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Tag

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by