MATLAB graphic drawing-create histogram

Create a histogram

For the vast majority of cases, the statistical object is a set of discrete data, we have to calculate their average, variance, standard deviation, etc. The data can be presented with a histogram. Now let ’s look at an example:
Suppose there are 36 students in a class, and the final exam results of the students are:
100 points-1 person
96 points-2 people
90 points-4 people
88 points-2 people
85 -3 person
84 min-1 person
82 minutes to 2 people
78 minutes - 7-
75 minutes -4 people
70 people divided -6
69 min-1 person
63 minutes to 2 people
55 people min-1
we did in MATLAB The first thing is to enter the data, enter the score x and the number of people who get the score y:

>> x = [55 63 69 70 75 78 82 84 85 88 90 96 100];
>> y = [1 2 1 6 4 7 2 1 3 2 4 2 1];

Then generate a histogram:

1
But this is not enough. We see that many data on the x-axis coincide. We want to see the number of people in a certain score interval, such as between 50 and 59 points, and we have to modify our image.
Now we need to divide the score range:
one student is between 50 and 59 points,
three students are between 60 and 69 points,
17 students are between 70 and 79 points,
8 students are between 80 and 89 points,
7 Students between 90 and 100 points
Next, we create two arrays, one to store the middle value of each score segment, and the other to store the number of people in each score segment.

>> x = [54.5 64.5 74.5 84.5 94.5];
>> y = [1 3 17 8 7];
>> bar(x,y),xlabel('分数'),ylabel('学生人数'),title('期末考试成绩')

More professional;
2
other commands:

  • barh: produces a horizontal histogram
    3

  • bar3: three-dimensional image
    4

  • bar3h: three-dimensional horizontal image
    5
    Example:
    There are three classes, each taught by three teachers, where there are different numbers of people in different grades, and a histogram with multiple data sets can be created to combine and pile up:

  • bar(x,y,‘grouped’)

     由于grouped是默认选项,所以我们还可以写成bar(x,y)
    
  • bar(x,y,‘stacked’)

The data we input is a multi-column array, the first column represents the distribution of class A results, and so on by analogy:

>> x = [54.5 64.5 74.5 84.5 94.5];
>> A = [0;3;18;13;10];...
B = [3;5;20;10;5];...
C = [1;2;15;17;8];
>> y = [A B C];
>> bar(x,y),xlabel('考试分数'),ylabel('学生人数'),legend('A','B','C');

Exercise

Published 84 original articles · won 18 · views 5805

Guess you like

Origin blog.csdn.net/qq_44486550/article/details/105307680