I want to plot 2 weibull curves( sample and model) on the same graph.
I have worked in my code.
Attached is the results i am having, i do not want the bar chart present, only my 2 weibull curves, but I am not able to do it.
Can someone please help me???
ps: In this case, #Guto has helped me edit my codes and now they work wonderfully.
Thanks
sample=[4.6
3.6
2.3
2.3
3
3.1
];
model=[8.01
6.75
6.57
6.07
5.94
5.58
];
% --- Plot data originally in dataset "sample data"
[CdfF,CdfX] = ecdf(sample,'Function','cdf'); % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(sample,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'sample data';
% Create grid where function will be computed
XLim = get(gca,'XLim');
XLim = XLim + [-1 1] * 0.01 * diff(XLim);
XGrid = linspace(XLim(1),XLim(2),100);
% --- Create fit "Weibull"
% Fit this distribution to get parameter values
pd3 = fitdist(sample, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
dist = makedist('Weibull','a',pd3.ParameterValues(1),'b',pd3.ParameterValues(2))
[h_ad_weibull, p_ad_weibull, adstat_weibull,cv_ad_weibull]= adtest(sample,'Distribution','weibull')
[h_ks_weibull, p_ks_weibull, ksstat_weibull,cv_ks_weibull]= kstest(sample,'CDF',dist)
LegHandles(end+1) = hLine;
LegText{end+1} = 'Weibull';
box on;
figure(1);
hold on;
% --- Plot data originally in dataset "model data"
[CdfF,CdfX] = ecdf(model,'Function','cdf'); % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(model,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'model data';
% Create grid where function will be computed
XLim = get(gca,'XLim');
XLim = XLim + [-1 1] * 0.01 * diff(XLim);
XGrid = linspace(XLim(1),XLim(2),100);
% --- Create fit "Weibull"
% Fit this distribution to get parameter values
pd3 = fitdist(model, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
dist = makedist('model','a',pd3.ParameterValues(1),'b',pd3.ParameterValues(2))
[h_ad_weibull, p_ad_weibull, adstat_weibull,cv_ad_weibull]= adtest(model,'Distribution','weibull')
[h_ks_weibull, p_ks_weibull, ksstat_weibull,cv_ks_weibull]= kstest(model,'CDF',dist)
LegHandles(end+1) = hLine;
LegText{end+1} = 'Weibull';
box on;
figure(2);
There are a few options to solve your problem. There are the two that came to my mind:
Remove the bar plots and make the limit.
If you don't want the bar plots why are you plotting it? Your code use it as input, but there is no reason to not use the limits from the data itself. Maybe you use for other purposes in another part not show. If this is the case, use the option 2.
For this, you just need fitdist and pdf functions. However, you need to check the min and max of both data sets and uses whatever is smaller or bigger. Here is just the necessary code to plot both lines:
box on; %create a figure and hold it
hold on;
%CREATE A GRID FROM DATA
XGrid=linspace(min(min([sample model]))-0.4*min(min([sample model])),...
max(max([sample model]))+0.2*max(max([sample model])),100);
% --- Create fit "Weibull"
% Fit this distribution to get parameter values
pd3 = fitdist(sample, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
% --- Create fit "Weibull"
% Fit this distribution to get parameter values
pd3 = fitdist(model, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
xlabel('Data');
ylabel('Density')
Delete the graph
That is quite straightforward and require only a small change. In the part % --- Plot data originally in dataset "model data", after the command set(hLine,'FaceColor' ... put the another command:
delete(hLine)
It will remove the latest graph assigned to hLine, that are the bars.
This will give the same output (except by small variations on limits) as above.
Related
I need to identify the 99% probability contour of a GMM fitted to data. Following this example, I'd like to be able to specify which contours to plot, and the x,y, of them.
mu1 = [1 2]; Sigma1 = [2 0; 0 0.5];
mu2 = [-3 -5]; Sigma2 = [1 0;0 1];
X = [mvnrnd(mu1,Sigma1,1000); mvnrnd(mu2,Sigma2,1000)];
GMModel = fitgmdist(X,2);
figure
y = [zeros(1000,1);ones(1000,1)];
h = gscatter(X(:,1),X(:,2),y);
hold on
gmPDF = #(x,y) arrayfun(#(x0,y0) pdf(GMModel,[x0 y0]),x,y);
g = gca;
fcontour(gmPDF,[g.XLim g.YLim])
title('{\bf Scatter Plot and Fitted Gaussian Mixture Contours}')
legend(h,'Model 0','Model1')
hold off
So, in the following figure, I'd like to to be able to plot the 99% in dashed black line "k". Any idea how to accomplish this?
You can display and get the coordinates of the given contour line specifying the LevelList property of fcontour, and then reading the ContourMatrix property of the contour handle:
% Random function, insert here yours
f = #(x,y) arrayfun(#(x0,y0) x0.^2 + y0.^2 - 0.1,x,y);
% The function value you want to get the contour for
lvl = 0.99;
% Plot the contour line
cHandle = fcontour(f, '--k', 'LevelList', [lvl]);
hold on
% Get the coordinates
lvlX = cHandle.ContourMatrix(1, 2:end);
lvlY = cHandle.ContourMatrix(2, 2:end);
% For a check:
plot(lvlX, lvlY, '--r')
I have 6 datasets each containing 10000 entries.
I want to plot their CDFs for comparison.
In MATLAB I am using the following code:
figure()
ksdensity(dataset1,'Support','positive','Function','cdf',...
'NumPoints',5)
xlabel('Error')
ylabel('CDF')
But I am not sure, is it the right way or wrong?
How can I do that?
I am getting the following figure.
Update:
This has been made even easier with cdfplot().
% MATLAB R2019a
% Example Data
X = wblrnd(2,3,50000,1);
Y = wblrnd(3,2,50000,1);
Z = wblrnd(2.5,2.5,50000,1);
Data = [X Y Z];
figure, hold on
for k = 1:size(Data,2)
h(k) = cdfplot(Data(:,k));
end
legend('show')
It looks like you've got the result you want except for the legend and markers. If you'd like more control of the plotting features, I'd suggest obtaining the necessary elements to plot from ksdensity using [f,xi] = ksdensity(x) then plotting separately.
% MATLAB R2019a
% Example Data
X = wblrnd(2,3,50000,1);
Y = wblrnd(3,2,50000,1);
Z = wblrnd(2.5,2.5,50000,1);
Data = [X Y Z];
NumPointCDF = 5; % Number of points to estimate CDF with
figure, hold on
for ii = 1:size(Data,2) % for each column of Data
[fii, xii] = ksdensity(Data(:,ii),'Support','positive','Function','cdf',...
'NumPoints',NumPointsCDF);
p(ii) = plot(xii,fii,'LineWidth',1.1,'Marker','.','MarkerSize',12);
end
legend('X','Y','Z')
Alternatively, you could just plot each first,
figure, hold on
for ii = 1:size(Data,2) % for each column of Data
[fii, xii] = ksdensity(Data(:,ii),'Support','positive','Function','cdf',...
'NumPoints',NumPointsCDF);
p(ii) = plot(xii,fii);
end
and then change the properties of each line later with p(1).foo (see here).
For example, one at a time: p(1).Marker = 's' % square
Or all at once:
% Update all properties using the object
for ii = 1:size(Data,2)
p(ii).Marker = '.'; % Adjust specific properties of p(ii) as needed
p(ii).LineWidth = 1.2;
end
Reference:
Graphics Object Properties
Access Property Values
I am doing some analysis and need to produce a histogram plot. I know how to create the standard histogram plot but I need something like the image below, where each point is an interval on the x axis. Each bin is based on a value from x-x for example.
You can use the histogram function, and then set the XTick positions and XTickLabels accordingly. See the comments in the code for explanation.
% random normally distrubuted data
x = 1*randn(1000,1);
edges = -5:1:5;
% create vector with labels (for XTickLabel ... to ...)
labels = [edges(1:end-1); edges(2:end)];
labels = labels(:);
% plot the histogram
figure();
ax = axes;
h = histogram(x, 'BinEdges', edges, 'Normalization', 'Probability');
ax.XTick = edges + mean(diff(edges)/2);
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;
% set yticks to percentage
ax.YTickLabel = cellfun(#(a) sprintf('%i%%', (str2double(a)*100)), ax.YTickLabel, 'UniformOutput', false);
% text above bars
bin_props = h.BinCounts/numel(x); % determine probabilities per bin in axis units
bin_centers = ax.XTick(1:end-1); % get the bin centers
txt_heigts = bin_props + 0.01; % put the text slightly above the bar
txt_labels = split(sprintf('%.1f%% ', bin_props*100), ' ');
txt_labels(end) = []; % remove last cell, is empty because of split.
text(ax, bin_centers, txt_heigts, txt_labels, 'HorizontalAlignment', 'center')
% set ylim to fit all text (otherwise text is outside axes)
ylim([0 .4]);
Putting the text at the right location may require some tweaking. Most important is the 'HorizontalAlignment' option, and the distance to the bars. I also used the 'Normalization', 'probability' option from the histogram function, and set the y axis to also show percentages.
I figure you can make the addition below yourself when needed.
When your data can be outside of the defined binedges, you can clip your data, and set the XTickLabels with less than or greater than signs.
% when data can be outside of defined edges
x = 5*randn(1000,1);
xclip = x;
xclip(x >= max(edges)) = max(edges);
xclip(x <= min(edges)) = min(edges);
% plot the histogram
figure();
ax = axes;
h = histogram(xclip, 'BinEdges', edges);
ax.XTick = edges + mean(diff(edges)/2);
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;
% set boundary labels
ax.XTickLabel{1} = sprintf('\\leq %.1f', edges(2));
ax.XTickLabel{end-1} = sprintf('\\geq %.1f', edges(end-1));
You can also set the outer edges to -Inf and Inf, as user2305193 pointed out. Since the outer bins are then much wider (because they actually extend to Inf on the x axis), which you can correct by setting the axis xlim. By the default the XTickLabels will display -Inf to -5.0, which I personally don't like, so I set them to lesser (and equal) than and greater than signs.
step = 1;
edges = -5:step:5; % your defined range
edges_inf = [-Inf edges Inf]; % for histogram
edges_ext = [edges(1)-step edges]; % for the xticks
x = 5*randn(1000,1);
% plot the histogram
figure();
ax = axes;
h = histogram(x, 'BinEdges', edges_inf, 'Normalization', 'probability');
labels = [edges_inf(1:end-1); edges_inf(2:end)];
labels = labels(:);
ax.XTick = edges_ext + step/2;
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;
% show all bins with equal width (Inf bins are in fact wider)
xlim([min(edges)-step max(edges)+step])
% set boundary labels
ax.XTickLabel{1} = sprintf('\\leq %.1f', edges(1));
ax.XTickLabel{end-1} = sprintf('\\geq %.1f', edges(end));
I would like to draw a boxplot with two sets of data to compare. I am willing to use Hierarchically grouped boxplot. I could just plot one set of my data using this function. I was wondering how I can use this function to plot two sets of data together. I drew the second set of data in red one by hand to show what I am trying to plot!
My problem is that I can't put two sets of data on one graph with hold on.
Well, this is not precisely what you asked for, and does not use the function hierarchicalBoxplot, but it may be a workaround. I demonstrate it using MATLAB example data:
load carsmall
% cleaning the data a little
Origin = categorical(cellstr(Origin));
MPG(Origin=='Italy') = [];
Origin(Origin=='Italy') = [];
% this part is just for readability:
data1 = MPG;
groups1 = Origin;
data2 = MPG*3;
groups2 = Origin;
% And we start:
% =============
% we need a wider figure, with a white background:
figure('Color',[1 1 1],'Position',[178 457 1114 521])
main_ax = axes; % create a tmporary axes
% we get the measurements of the ploting area:
pos = main_ax.Position;
% and divide it to our data:
group_number = 6;
width = pos(3)/group_number; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),group_number+1);
clf % clear the area!
% Now we plot everything in a loop:
for k = 1:group_number
% create a different axes for each group:
ax = axes;
boxplot(ax,data1,groups1); % plot the first set
hold on
boxplot(ax,data2,groups2) % plot the second set
% set the ylim to include all data:
ax.YLim = [min([data1; data2])-5 max([data1; data2])+10];
ax.XTickLabelRotation = 90; % rotate xlables if needed
box off
if k == 1
ylabel('Miles per Gallon (MPG)') % only for the most right axes
else
ax.YTick = [];
end
xlabel(['Group ' num2str(k)])
ax.Position = [corner(k) 0.2 width 0.7];
end
% and finally we place the title:
main_ax = axes('Position',[corner(1) 0.11 width*group_number 0.815]);
title('Miles per Gallon by Vehicle Origin')
axis off
% and this will color the data:
f = gcf;
colors = [1 0 0;0 0 1]; % red and blue
for g = 2:numel(f.Children)
for k = 1:numel(f.Children(g).Children(1).Children)
f.Children(g).Children(1).Children(k).Color = colors(1,:);
f.Children(g).Children(1).Children(k).MarkerEdgeColor = colors(1,:);
f.Children(g).Children(2).Children(k).Color = colors(2,:);
f.Children(g).Children(2).Children(k).MarkerEdgeColor = colors(2,:);
end
end
All this procedure gives:
It will probably need some final tweaks, but it's somthing to start from ;)
Edit
For a side by side view, you can plot all groups together, and just move the x-ticks:
% Making some data:
% MAKE SURE YOU UNDERSTAND HOW THE DATA IS ARRANGED WITHIN THE GRAPH
years = 6; % try to change this number
groups = 5; % try to change this number
data1 = rand(100,years);
data2 = rand(100,years)+0.3;
groups1 = randi(groups,100,1)*2-1; % groups 1 3 5 7 9
groups2 = randi(groups,100,1)*2; % groups 2 4 6 8 10
legendEntries = {'A' 'B'};
colors = [1 0 0;0 0 1]; % red and blue
% And we start:
% =============
% we need a wider figure, with a white background:
figure('Color',[1 1 1],'Position',[178 457 1400 521])
main_ax = axes; % create a temporary axes
% we get the measurements of the plotting area:
pos = main_ax.Position;
% and divide it to our data:
width = pos(3)/years; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),years+1);
clf % clear the area!
% Now we plot everything in a loop:
for k = 1:years
% create a different axes for each group:
ax = axes;
boxplot(ax,[data1(:,k); data2(:,k)],[groups1; groups2]);
ax.XTick = 1.5:2:(groups*2-0.5); % to "combine" the groups in pairs
ax.XTickLabel = {'a','b','c','v','f'};
% set the ylim to include all data:
ax.YLim = [min([data1(:); data2(:)]) max([data1(:); data2(:)])];
box off
if k == 1
ylabel('Miles per Gallon (MPG)') % only for the most right axes
else
ax.YTick = [];
end
xlabel(num2str(2000+k)) % the labels for the years
ax.Position = [corner(k) 0.11 width 0.8];
% this will color the data:
for g = 1:2:numel(ax.Children.Children)-1
ax.Children.Children(g).Color = colors(1,:);
ax.Children.Children(g).MarkerEdgeColor = colors(1,:);
ax.Children.Children(g+1).Color = colors(2,:);
ax.Children.Children(g+1).MarkerEdgeColor = colors(2,:);
end
if k == years
% you can try to change here the index to 1:2 and see if you like it:
leg = legend(ax.Children.Children(20:21),legendEntries);
leg.Position(1) = 0.92;
end
end
% and finally we place the title:
main_ax = axes('Position',[corner(1) 0.11 width*years 0.815]);
title('Miles per Gallon by Vehicle Origin')
axis off
And we get the crowded plot:
I would like to plot constellation diagram similar to the figure below.
.
My approach is something like this
clc;
clear all;
close all;
N=30000;
M=16;
Sr=randint(N,1,[0,(M-1)]);
S=qammod(Sr,16,0,'gray'); S=S(:);
Noisy_Data=awgn(S,20,'measured'); % Add AWGN
figure(2)
subplot(1,2,1)
plot(S,'o','markersize',10);
grid on
subplot(1,2,2)
plot(Noisy_Data,'.');
grid on
May you assist me to make necessary modification to get graph similar to the figure attached above. Thank you.
The first thing to do would be to compute a 2D histogram of your data. This can be done with the following:
% Size of the histogram matrix
Nx = 160;
Ny = 160;
% Choose the bounds of the histogram to match min/max of data samples.
% (you could alternatively use fixed bound, e.g. +/- 4)
ValMaxX = max(real(Noisy_Data));
ValMinX = min(real(Noisy_Data));
ValMaxY = max(imag(Noisy_Data));
ValMinY = min(imag(Noisy_Data));
dX = (ValMaxX-ValMinX)/(Nx-1);
dY = (ValMaxY-ValMinY)/(Ny-1);
% Figure out which bin each data sample fall into
IdxX = 1+floor((real(Noisy_Data)-ValMinX)/dX);
IdxY = 1+floor((imag(Noisy_Data)-ValMinY)/dY);
H = zeros(Ny,Nx);
for i=1:N
if (IdxX(i) >= 1 && IdxX(i) <= Nx && IdxY(i) >= 1 && IdxY(i) <= Ny)
% Increment histogram count
H(IdxY(i),IdxX(i)) = H(IdxY(i),IdxX(i)) + 1;
end
end
Note that you can play around with parameters Nx and Ny to adjust the desired resolution of the plot. Keep in mind that the larger the histogram, the more data samples (controlled by the parameter N of your simulation) you'll need to have enough data in the histogram bins to avoid getting a spotty plot.
You can then plot the histogram as a color map based on this answer. In doing so, you likely would want to add a constant to all non-zero bins of the histogram so that the white band is reserved for zero valued bins. This would provide a better correlation with the scatter plot. This can be done with:
% Colormap that approximate the sample figures you've posted
map = [1 1 1;0 0 1;0 1 1;1 1 0;1 0 0];
% Boost histogram values greater than zero so they don't fall in the
% white band of the colormap.
S = size(map,1);
Hmax = max(max(H));
bias = (Hmax-S)/(S-1);
idx = find(H>0);
H(idx) = H(idx) + bias;
% Plot the histogram
pcolor([0:Nx-1]*dX+ValMinX, [0:Ny-1]*dY+ValMinY, H);
shading flat;
colormap(map);
After increasing N to 1000000, this gives the following plot for the data generated according to your sample: