[go: up one dir, main page]

0% found this document useful (0 votes)
80 views11 pages

2D and 3D Plotting in MATLAB

The document provides an overview of 2D and 3D plotting in MATLAB, detailing the software's interface and basic commands for creating plots. It explains how to define variables, plot discrete data, and enhance graph presentations with labels and legends. Additionally, it covers generating both 2D and 3D pie charts, illustrating the process with examples and MATLAB commands.

Uploaded by

adeola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views11 pages

2D and 3D Plotting in MATLAB

The document provides an overview of 2D and 3D plotting in MATLAB, detailing the software's interface and basic commands for creating plots. It explains how to define variables, plot discrete data, and enhance graph presentations with labels and legends. Additionally, it covers generating both 2D and 3D pie charts, illustrating the process with examples and MATLAB commands.

Uploaded by

adeola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

2D and 3D Plotting in MATLAB

Introduction

MATLAB is a mathematical and graphical software package and has numerical, graphical,
and programming competence. There are built-in functions to perform many operations with
toolboxes that can be added to augment these functions (e.g., for signal processing).

MATLAB is started same way as any other Windows program. When the MATLAB software
is started, a window is opened (Figure 1). As shown in the figure, the screen is separated into
various elements:
1. File listing in the current folder
2. Workspace window
3. Command History
4. Command Window (the main part is the Command Window)

Figure 1: MATLAB WINDOW


The two greater than sign (>>) in the Command Window is called the MATLAB prompt.
MATLAB is used interactively in this Window. MATLAB respond with result immediately
to any command or expression entered at the prompt in this Window. Programs written in
MATLAB are contained in script files or M-files.

2
Variablename and Assignment Statements

In MATLAB session value are required to be stored and recall in future use. To accomplish
this task, we use a variablename. A variablename is created using what is described as an
assignment statement. The format for assignment statement creation is:

variablename = expression

MATLAB refers to the equality sign as the assignment operator not equating the left-hand
side the right-hand side. But that the left-hand side is assigned the value of the right-hand
side. The expression is evaluated and then that value is stored in the variablename.
variablename and corresponding value is displayed in the workspace window.
For instance,
>> format short
>> format compact
>> B = 2
B=
2
>> C = B + 5
C=
7
>>

Quick help commands relating to variables:


1. who: shows names of defined variables in the Command Window.
2. whos: shows more information about the names of defined variables in the Command
Window similar to Workspace Window content.
3. clear: clears all defined variables.
4. clear variablename: clears a particular defined variable in the Command Window.

3
2D and 3D Plotting Discrete Data

The steps involved in plotting in MATLAB are:


1. Define the function
2. Call the MATLAB plot (x , y ) function to plot a discrete set of data points connected
with a line.

Plotting of graphs with discrete values x and y is common place in practical analysis of data.
Example 1, consider the table below:

No. of Persons 1 2 3 4 5 6 7 8 9 10
Luggage No. 48 45 55 60 75 70 62 40 68 57

Function definition:
¿ x=[1 , 2 ,3 , 4 ,5 ,6 , 7 , 8 , 9 , 10];
¿ y=[48 , 45 , 55 , 60 ,75 , 70 , 62 , 40 ,68 ,57];
Call the MATLAB plot (x , y)
¿ plot (x , y )
75

70

65

60

55

50

45

40
1 2 3 4 5 6 7 8 9 10

Figure 2: 2D graph

The same example can be plotted using bar chart by calling the bar plot function (x ¯, y ) see
figure 3.
Readability is important when it comes to data presentation and MATLAB consist of some
functions and features to enhance its presentation.
There are four basic line types that can be used to define a plot. These are, along with the
character strings, used to define them in the plot command:
1. Solid line '−' (default)
2. Dashed line '−−'
3. Dash-dot line '−. '
4. Dotted line ' :'

4
80

70

60

50

40

30

20

10

0
1 2 3 4 5 6 7 8 9 10

Figure 3: 2D bar chart


Table 1 Plot colour selection indicators

Table 2 Plot symbols

Also, axis can be labeled using the xlabel and ylabel command, the plot can the titled using
the title command. The format for this commands are as follows:

5
1. xlabel (‘ label name ’)
2. ylabel (‘ label name ’)
3. title(‘ graph title ’)
The application of these features and functions to the current example we have:

¿ x=[1 , 2 ,3 , 4 ,5 ,6 , 7 , 8 , 9 , 10];
¿ y=[48 , 45 , 55 , 60 ,75 , 70 , 62 , 40 ,68 ,57];
¿ plot ( x , y , ' b−−') , xlabel (' No .of Persons '), ylabel (' Luggage No' ), title(' XYZ Transport ' )

XYZ Transport
75

70

65
Luggage No

60

55

50

45

40
1 2 3 4 5 6 7 8 9 10
No. of Persons

Or use bar chart command


¿ (x ¯, y), xlabel(' No. of Persons ' ), ylabel(' Luggage No'), title(' XYZ Transport ')

XYZ Transport
80

70

60

50
Luggage No

40

30

20

10

0
1 2 3 4 5 6 7 8 9 10
No. of Persons

For easy analysis we might want to plot one data on the same graph.

6
For example, 2
No. of Persons 1 2 3 4 5 6 7 8 9 10
Luggage No. 48 45 55 60 75 70 62 40 68 57
Bus No. 18 22 34 28 19 16 38 12 42 50

¿ x=[1 , 2 ,3 , 4 ,5 ,6 , 7 , 8 , 9 , 10];
¿ y=[48 , 45 , 55 , 60 ,75 , 70 , 62 , 40 ,68 ,57];
¿ z=[18 , 22 , 34 , 28 ,19 , 16 , 38 ,12 , 42 , 50];
¿ plot ¿
XYZ Transport
80

70

60

50

40

30

20

10
1 2 3 4 5 6 7 8 9 10
No. of Persons

In the above figure we might want to add legend for clarity using the legend command. The
format is ¿ legend (' parametername .' , parameter name . ' );
Applying this to the above example is as follows:
¿ x=[1 , 2 ,3 , 4 ,5 ,6 , 7 , 8 , 9 , 10];
¿ y=[48 , 45 , 55 , 60 ,75 , 70 , 62 , 40 ,68 ,57];
¿ z=[18 , 22 , 34 , 28 ,19 , 16 , 38 ,12 , 42 , 50];
≫ plot ¿
¿ legend (' Luggage No. ' , ' Bus No . ')

7
XYZ Transport
80
Luggage No.
Bus No.
70

60

50

40

30

20

10
1 2 3 4 5 6 7 8 9 10
No. of Persons

To generate 3D plot in MATLAB is easy since the concept of 2D is already established. To


generate a 3D bar chart of example 1 is as follows:

¿ x=[1 , 2 ,3 , 4 ,5 ,6 , 7 , 8 , 9 , 10];
¿ y=[48 , 45 , 55 , 60 ,75 , 70 , 62 , 40 ,68 ,57];
3̄(x , y ,' y '), xlabel (' No . of Persons '), ylabel (' Luggage No '),title (' XYZ Transport ')
XYZ Transport
80

60

40

20

1
2
3
4
5
6
7
8
9
10
Luggage No
No. of Persons

The 3D plot of example 2 is generated as follows:


¿ x=[1 , 2 ,3 , 4 ,5 ,6 , 7 , 8 , 9 , 10];
¿ y=[48 , 45 , 55 , 60 ,75 , 70 , 62 , 40 ,68 ,57];
¿ z=[18 , 22 , 34 , 28 ,19 , 16 , 38 ,12 , 42 , 50];
¿ plot 3 (x , y , z , ' o '), xlabel(' No . of Persons '),title (' XYZ Transport '), grid on

8
XYZ Transport

50

40

30

20

10
80
70 10
8
60 6
50 4
2
40 0
No. of Persons

2D and 3D Pie chart can also be generated in can also be generated in MATLAB. Consider
example 3.

Materials Tons
Iron 80
Copper 20
Aluminum 17
Coal 60
Gold 30
Coal tar 55
The 2D pie chart can be generated by:
¿ x=[80 ,20 , 17 , 60 ,30 ,55];
¿ pie(x);
¿ labels={' Iron ' , ' Copper ' ,' Aluminium ' , ' Coal ' , ' Gold ' ,' Coal tar ' };
¿ legend (labels , ' location' , ' southoutside ' ,' orientation' ,' horizontal ')

9
21%

31%

11%

8%

6%
23%

Iron Copper Aluminium Coal Gold Coal tar

The 3D pie chart can be generated by:


¿ x=[80 ,20 , 17 , 60 ,30 ,55];
¿ pie3 (x);
¿ labels={' Iron ' , ' Copper ' ,' Aluminium ' , ' Coal ' , ' Gold ' ,' Coal tar ' };
¿ legend (labels , ' location' , ' southoutside ' ,' orientation' ,' horizontal ')

21%

11%

31%

23%

8% 6%

Iron Copper Aluminium Coal Gold Coal tar

10
Thank you

11

You might also like