MATLAB graphic drawing-discrete data drawing

Discrete data plotting

If we have five students ’grades now, we want to draw their grades and names on the map one by one. If necessary, we can connect them with broken lines. At this time, we must understand the following functions:

  • xtick
  • xticklabel
  • gca
  • set
  • axis

Among them, the gcapopular understanding is to return the nature of the current function image, such as length, width, height, resolution, coordinate axis index value, etc. A series of image attributes
can then be setmodified one by one by commands, just like Cthe structure of the language, such an interpretation can be It's very easy to understand.
If we have the results of the following five people:

A:50
B:98
C:75
D:80
E:98

We need to draw it out with polylines, and xthe labels of the coordinate axes are not ordinary numbers, 1,2,3....etc., but correspond to each person's name.
First we create an array with five elements:

>> x = [1:5];

Then we create an array of grades:

>> y = [50 98 75 80 98];

Next, we first draw the points corresponding to these results (note that it is not a polyline, this will be discussed immediately), you can use the following command:

>> plot(x,y,'o')

image:
Figure 1

The third parameter 'o'indicates that we use circles to represent the data, of course, we can also use other shapes:

  • s– Square
  • d-diamond
  • p–Pentagram
  • o– Circle
  • x-Cross
  • .– Dot number
    Another error-prone point is that we only draw some dots. If we want to connect them, we can use the following command:
>> plot(x,y,'o',x,y)

It means that after showing these points, we will connect them with a polyline. The image is as follows: It
figure 2
MATLABseems that the default color is red. We can also change the color and line style we like in the third parameter. one example:

>> plot(x,y,'or',x,y,'--')

This means:

  • The first one is x,yto draw the scatter first and show it with a red circle
  • The second is to x,ydraw a straight line, and the line type is a dashed line.
    Image:
    image 3
    We should note that the order of the parameters has a certain effect on the image. When I first started drawing, I wanted to put all the parameters about the line type and color in one place. , But later found that this order will have a certain impact on the image, such as:
>> plot(x,y,'or--',x,y)

Our original intention is to first describe these scattered points, represented by a red circle, and then connect these points with a dotted line, but the image drawn is as follows: the
Unsatisfactory image
result is drawn with a solid line, which does not meet our expectations, and later tried Finally understood:
we are drawing graphics based on scattered points, xand ythe relationship is one-to-one, so we need to draw these points first, we need some marks to draw these points, such as circles, diamonds, points, etc. Wait, we can add colors to it, but we ca n’t choose the line type, such as the solid line of the dashed line, because these are only independent points, we have not connected them, so the line type command MATLABwill be ignored here ; wait until we After these points are drawn,
we need to connect them. At this time, we can select the line parameters.
Just like:


>> plot(x,y,'.g',x,y,'k--')

Image:
Figure 5
We use green dots to connect with black dotted lines

So we need to pay attention to that, we need to fill in the corresponding parameters in the corresponding position of the discrete point drawing, otherwise MATLAB will automatically ignore it.

It seems that the question is off, now we go back to the question and draw an image of the student's score:

>> x = [1:5];
>> y = [50 98 75 80 98];
>> plot(x,y,'or',x,y,'--')
>> set(gca,'xticklabel',['A';'B';'C';'D';'E']);
>> set(gca,'xtick',[1:5]);
>> axis([1 5 0 100])

Generate:
Correct image

>> set(gca,'xticklabel',['A';'B';'C';'D';'E']);
>> set(gca,'xtick',[1:5]);

These two sentences are equivalent to replacing 1~5the coordinate value with the student's name and gcareturning the attributes of the current image.

Bar graph

The bar chart command is very simple, we can use the bar(x,y)command directly to draw:

>> bar(x,y)
>> xlabel('学生')
>> ylabel('分数')
>> title('期末考试')

Bar graph

Needle diagram

Another way to draw discrete data is a needle chart. Let's look at a function:
we assume that this function is the response of a spring under a certain force over time.
function
Which value
we now draw the image in the 200, we first define the range:

>> t = [0:0.1:200];

Next, define the function:

>> f = exp(-0.01*t).*sin(t/4);

Drawing:

>> plot(t,f),xlabel('时间(秒)'),ylabel('弹簧响应')

Image: The
Spring response
above picture takes data every 0.1, if we want to plot from discrete data, we take samples every 5

>> t = [0:5:200];
>> f = exp(-0.01*t).*sin(t/4);
>> plot(t,f),xlabel('时间(秒)'),ylabel('弹簧响应')

Image:
Discrete drawing
The image we get is a bit rough. In this case, engineers like to draw it as a needle map, just use the command stem(x,y):
Needle diagram
Many commands of plot can also be used on the stem, such as the color and shape of the point, Line color, shape:

  • s– Square
  • d-diamond
  • p–Pentagram
  • o– Circle
  • x-Cross
  • .–Diagram
    We can also use the fillcommand to fill the shape
    code:
>> stem(t,f,'p','fill'),xlabel('时间(秒)'),ylabel('弹簧响应')

Pentagram fill

>> stem(t,f,'dg--','fill'),xlabel('时间(秒)'),ylabel('弹簧响应')

Green diamond filled dotted connection

Published 84 original articles · won 18 · views 5805

Guess you like

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