Plot mean and standard deviations along with data on a bell curve

I have columns of data, numbering approximately 120 rows. The data is 1 thru 5, representing survey data. I am working on analyzing the data columns. The column data also has some NaN. I can calculate the mean and standard deviations. However, I am attempting to plot the mean, standard deviations, along with the actual data on the bell curve. I found this code that at least plots the data. But I am not sure how to change the code to correctly represent my data on a bell curve. For instance, I don't think I need the randn function, given the amount of data I have. In short, I just want to plot my data, the mean, and standard deviations (to plus and minus 3 sigma) for all columns of data on the bell curve, similar to what this code produces.
x = .03*randn(10000,1)+.34;
[N,X] = hist(x,100);
hfig = figure;
bar(X,N)
hold on;
y = [0 1.2*max(N)];
center = mean(x);
std1 = std(x);
%center plot
plot([center center],y,'r-.')
%1 std
plot([center center]+std1,y,'g-.')
plot([center center]-std1,y,'g-.')
%2 std
plot([center center]+2*std1,y,'k-.')
plot([center center]-2*std1,y,'k-.')

10 Commenti

Are you unhappy with the way that this plot has been created?
If you like the curve and the plot and just want to substitute your data for x. For example, setting x = data(: ,1) would assign the first column of the data to x.
No, I'm not unhappy. The current code seeems to work great for the current data. I am not getting a bell curve with my data. And I'm not sure what I'm doing wrong when I include my data.
Are you wanting to plot a 'theoretical' bell curve to compare against the data? If so, your code that you posted doesn't have anything in it to add such a curve to the plot. Also, the plot in your last post doesn't match up with the plot that results from the code you posted.
The code that you posted generates a pretty good looking plot. Please explain what you don't like about it.
That makes sense that I may not get a bell curve, continuing to think about it. But I would like to see the mean and the standard deviations, along with each data point (to see where each data point falls according to mean and standard deviations. Here is the code. As you can see, I only added my data for x.
datap1 = S.SmartDeviceSurveyRL8August4201810.p1;
x = datap1;
[N,X] = hist(x,100);
hfig = figure;
bar(X,N)
hold on;
y = [0 1.2*max(N)];
center = mean(x);
std1 = std(x);
%center plot
plot([center center],y,'r-.')
%1 std
plot([center center]+std1,y,'g-.')
plot([center center]-std1,y,'g-.')
%2 std
plot([center center]+2*std1,y,'k-.')
plot([center center]-2*std1,y,'k-.')
You mentioned you have NaN's in your data. What are the values you obtain for 'center' and 'std1' in your code? If you have NaN's in your data, center and std1 are probably NaN, and therefore won't be plotted. Try using nanmean and nanstd instead.
Well I think my code below (in the official "Answers" section, not up here in the "Comments" section) should work. Did you try it?
If you still need help, save your x in a .mat file:
save('answers.mat', 'x');
then upload with the paper clip icon.
So still working on making this work. I went in another direction, given that I don't actually need a bell curve. I would like to display the data ( 1 2 3 4 5) on the y-axis, with the mu and standard deviations on the x-axis. I am including my data and current code.
x = [0 stdP1w1 meanP1 stdP1sig2];
y = [0 1 2 3 4 5];
plot(datap1)
xticks([0 stdP1w1 meanP1 stdP1sig2])
xticklabels({'0','\sigma', 'mu','2\sigma'})
Not sure what you want, but it it anything like this:
s = load('personality_cols.mat')
personality_cols = s.personality_cols
meanP1 = mean(personality_cols.p1, 'omitnan')
stdP1w1 = std(personality_cols.p1, 'omitnan')
stdP1sig2 = std(personality_cols.p2, 'omitnan')
% No idea what these next two lines are for.
% x = [0 stdP1w1 meanP1 stdP1sig2];
% y = [0 1 2 3 4 5];
% Plot the data.
x = 1 : length(personality_cols.p1);
plot(x, personality_cols.p1, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
% Put vertical line at the mean.
yline(meanP1, 'Color', 'r', 'LineWidth', 2);
% Put vertical line at the mean.
yline(meanP1 - stdP1w1, 'Color', 'm', 'LineWidth', 2);
yline(meanP1 + stdP1w1, 'Color', 'm', 'LineWidth', 2);
% No idea what these are for so I'll comment them out.
% xticks([0 stdP1w1 meanP1 stdP1sig2])
% xticklabels({'0','\sigma', 'mu','2\sigma'})
There is a red line at the mean and magenta lines at the mean plus or minus a standard deviation. But I did not understand a lot of your code.
Thanks a bunch! This really worked well for me. I was also able to modify the code to include additional standard deviations.
I am curious. I found code and modified it to what you see below. My goal is to calculate the percentages of the data for column p1. So for instance, I want to determine how much of the data in a particular column are 5s, 4s, 3s, etc.
Code
numberOfBins = max(personality_cols.p1(:));
countsPercentage = 100 * hist(personality_cols.p1(:), numberOfBins) / numel(personality_cols.p1)
Answer
countsPercentage =
9.0909 4.1322 13.2231 33.0579 24.7934
The countsPercentage does not equal 100. countsPercentage's total = 84.3. Is the remaining percentage NaN values? How do I get the percentage of the NaN values to know that the other percentages are correct? How do I exclude NaN values from the countsPercentage values so that 100 percent is only looking at data (5s, 4s, 3s, etc)? How do I know which values are being represented by which set of data (for example, how do I know 9.0909 are 1s or are they 5s from the p1 column data)?
You can use the isnan() function along with sum() to compute the number of nans in a vector.
numNans = sum(isnan(yourVector));
percentNans = 100 * numNans / numel(yourVector);

Accedi per commentare.

Risposte (1)

Then if it's not normally distributed data, why do you want to fit a bell curve to it?
Did you try fitdist():
load hospital
x = hospital.Weight;
pd = fitdist(x,'Normal')
x_values = 50:1:250;
y = pdf(pd,x_values);
plot(x_values,y,'LineWidth',2)

Categorie

Richiesto:

il 21 Mag 2020

Commentato:

il 19 Giu 2020

Community Treasure Hunt

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

Start Hunting!

Translated by