Download presentation
Presentation is loading. Please wait.
Published byUte Arnold Modified over 6 years ago
1
Net 222: Communications and networks fundamentals (Practical Part)
Networks and Communication Department Lab 2 : potting to Matlab
2
Lecture Contents Graph in MatLab 2-D plotting
Creating and Type of graph Using Basic Plotting Functions Plotting Multiple graph in same plot Formatting plots Save ,Print ,Close and Export figure Networks and Communication Department
3
Creating Graph in MATLAB
Line Plot Pie Charts, Bar Plots, and Histograms Command Description bar, bar3 Bar graph 2-D, 3-D Pie, pie3 Pie graph 2-D, 3-D hist Histogram plot Command Description Plot, plot3 Create 2-D graph and 3-D plotyy 2-D line plots with y-axes on both left and right side Discrete Graph Command Description Stem, stem3 Plot discrete sequence data 2-D, 3-D stairs Stairstep graph Networks and Communication Department
4
Plotting Functions Networks and Communication Department
5
2-D Plotting Specify x-data and/or y-data
Specify color, line style and marker symbol (Default values used if ‘clm not specified) Syntax: Plotting single line: Plotting multiple lines: plot(xdata, ydata, 'color_linestyle_marker') If data is given, the information is entered as the elements of the vectors x and y. If the values of y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The PLOT functions is used to produce 2-dimensional curves, using x- and y-data matrices specified by the user. However, PLOT also accepts character-string arguments that designate various colors, line styles and marker symbols for the resulting line. For example, to produce a red dotted line with square markers at each data point, type: » plot(x, y, 'r:square') You can plot multiple lines at once, using pairs of x- and y-data. As with plotting single lines, the color, line style and marker information can be specified explicitly. However, if not specified, MATLAB automatically cycles through a predefined (but user settable) list of colors to identify each line. For example, the following statement plots three lines as a function of t (First = blue / Second = green / Third = red): » plot(t, y1, t, y2, t, y3) plot(x1, y1, 'clm1', x2, y2, 'clm2', ...)
6
Line Specification Meaning Color Specifiers Blue b Cyan c Green g
Three components can be specified in the string specifiers along with the plotting command. They are: Line style Marker symbol Color Meaning Color Specifiers Blue b Cyan c Green g Black k Magenta m Red r Yellow y Meaning Marker down v circle o triangle left < diamond d triangle right > hexagram h up triangle ^ pentagram p x -mark x plus + point . square s star * Line style Specifiers Meaning '-' Solid line (default) '--' Dashed line ':' Dotted line '-.' Dash-dot line Networks and Communication Department
7
2-D Plotting - example » x = 0:.1:2*pi; » y = sin(x); » plot(x,y)
Create a Blue Sine Wave » x = 0:.1:2*pi; » y = sin(x); » plot(x,y) In this example, the data plotted along the x-axis is defined as: » x=0:0.1:2*pi; The data plotted along the y-axis is defined as: » y=sin(x); Plot these data as follows (default = blue, solid line, no marker): » plot(x,y)
8
Basic Task: Plot the function sin(x) between 0≤x≤4π
Create an x-array of 100 samples between 0 and 4π. Calculate sin(.) of the x-array Plot the y-array >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(x,y)
9
Adding a Grid GRID ON creates a grid on the current figure
GRID OFF turns off the grid from the current figure GRID toggles the grid state grid on
10
Controlling the Axes Setting Axis Limits & Grids
The axis command lets you to specify your own limits: axis([xmin xmax ymin ymax]) You can use the axis command to make the axes visible or invisible: axis on / axis off The grid command toggles grid lines on and off: grid on / grid off
11
Using Basic Plotting Functions
The following commands are useful when plotting: Note that all text must be put within ' '. Graphing functions MATLAB command Label the horizontal axis. xlabel('text') Label the vertical axis. ylabel('text') Attach a title to the plot. title('text') Change the limits on the x and y axis. axis([xmin xmax ymin ymax]) "Keep plotting in the same window." hold on Turn off the "keep-plotting-in-the-same-window-command". hold off using the strings to label various curves legend(‘string1’,’string2’,’string3’) Places the string (text) on the plot at coordinate x,y relative to the plot axes. text(x,y,’string’) The title (), xlabel (), and ylabel () commands take a string argument in single quotes in order to label the top of the plot, the x-axis, and the y-axis, respectively. The axis () command takes a 2 or 4 element vector as its argument where the minimum and maximum x values are set or the minimum and maximum x and y values are set. Matlab will probably complain loudly if either xmax or ymax is less than xmin or ymin. The use of the axis () command allows the user to override the default setting that Matlab would otherwise pick. It can be use to look closely at a small window of data which would otherwise be unclear if a lot of points where available. It can be particularly useful when making an interactive plot that needs to resize itself. The legend () command allows us to place a legend on a plot. The legend () command takes as its arguments a list of strings enclosed in single quotes that are the explanatory information you would like displayed next to a short sample of the line and data point display for a given set of data. The order of the strings must correspond to the order of the graphs that were added to the plot. In addition to the string, the legend () command can take a position argument that dictates where the legend is to be placed on the plot. The default is the upper right corner of the plot. The text () command takes three arguments: an x and y location to begin writing a string and the text string that you would like displayed. The string must be enclosed in single quotes. The x and y location must be supplied in the same scale that is being used for the x and y axes. In other words, the location of the text string is relative to the plot. Fixed screen locations do not exist. Networks and Communication Department
12
Multiple graph Networks and Communication Department
13
Adding additional plots to a figure
PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT Plotting two (or more) graphs in one plot: Using the plot command. 2. Using the hold on, hold off commands. In addition to supplying multiple vector pairs to the plot () command, we can create graphs with multiple plots by using the hold on/hold off commands. Once a plot () command has been issued and hold on specified, all subsequent plot () commands will be plotted on the initial graph. Axis properties will be maintained for subsequent plots. If line specifiers are not specified in the subsequent plots, Matlab will use the default order and colors when drawing additional plots on the graph. To return to the default situation in which each plot () command causes a new plot to be drawn in the graph window, the hold off command must be issued.
14
Adding additional plots to a figure
HOLD ON holds the current plot HOLD OFF releases hold on current plot HOLD toggles the hold state » x = 0:.1:2*pi; y = sin(x); » plot(x,y,'b') » grid on » hold on » plot(x,exp(-x),'r:*') By default, PLOT deletes existing lines and resets all axis properties when a new line is drawn (HOLD OFF). To add lines to an existing plot turn HOLD ON. In this mode, MATLAB does not remove the existing graph but adds new lines to the current plot. If the new data falls outside the range of the original axes, MATLAB will rescale the axes accordingly. When you set HOLD OFF, the default mode is restored - PLOT command erases any existing plots and resets all axis properties before creating new plots. When you call HOLD, by itself, the hold state is toggled NOTE: If you do not want MATLAB to rescale the axes, issue the command >>axis manual before you plot your second set of data.
15
Subplots Networks and Communication Department
16
Formatting Networks and Communication Department
17
Engineering H192 Winter 2005 Formatting plots A plot can be formatted to have a required appearance. With formatting you can: Add title to the plot. Add labels to axes. Change range of the axes. Add legend. Add text blocks. Add grid. Instructor notes: Matlab provides us with a host of formatting capabilities so that we can add all of the elements to a plot that make it easy for someone to use. We can format plots either through commands entered in a script/function file or directly from the command window. Lecture 22A
18
Formatting plots Formatting commands.
Engineering H192 Winter 2005 Formatting plots There are two methods to format a plot: Formatting commands. In this method commands, that make changes or additions to the plot, are entered after the plot() command. This can be done in the Command Window, or as part of a program in a script file. Formatting the plot interactively in the Figure Window. In this method the plot is formatted by clicking on the plot and using the menu to make changes or add details. Instructor notes: To reiterate...we can format plots via commands in the Command Window or script/function files. Additionally, plots can be formatted interactively using the icons provided in the Figure Window. In cases where multiple plots are being generated, it is probably most efficient to format them via commands such that Matlab takes care of the individual actions. Given a list of what to do, Matlab will be able to do it much more quickly. Lecture 22A
19
FORMATTING A PLOT IN THE FIGURE WINDOW
Engineering H192 Winter 2005 FORMATTING A PLOT IN THE FIGURE WINDOW Once a figure window is open, the figure can be formatted interactively. Use the insert menu to Use Figure, Axes, and Current Object-Properties in the Edit menu Click here to start the plot edit mode. Instructor notes: It’s faster to format plots via the commands, especially if scripts are being run repeatedly or many plots are needed, however, it is possible to format plots interactively through the figure window. Lecture 22A
20
POLTTING IN MATLAB Line plot FUNCTIONs: Plot example:
Networks and Communication Department
21
POLTTING IN MATLAB Plot wit line specification example:
Linearly spread vector by default generate 100 points between x1 , x2 Networks and Communication Department
22
POLTTING IN MATLAB Plotyy ex: Networks and Communication Department
23
POLTTING IN MATLAB Plot3 3D ex: Networks and Communication Department
24
POLTTING IN MATLAB #Sw_eng Networks and Communication Department
25
Pie Charts, Bar Plots, and Histograms
Bar & bar3 ex: Networks and Communication Department
26
Bar3 function Networks and Communication Department
27
bar3 ex Networks and Communication Department
Detached : Display elements of each row in y as separate block behind another in x direction Networks and Communication Department
28
Pie Charts, Bar Plots, and Histograms
Pie & hist ex: This Matlab code creates a histogram with 3 bars. The first bar has two '1' values, the second bar has three '2' values, and the third bar has one '3' values. y = [ ] hist(y) The horizontal axis has the different values in the vector to be plotted. The vertical axis has the number of those values in the vector. Example 2: This Matlab code generates a histogram of 15 randomly distributed numbers between 0 and 1 (you can type 'help rand' on your command window to learn about random numbers). Note that each column includes a range of values, otherwise the histogram would contain 15 bars. You can type ' help hist ' or ' help bar ' on your command window to see more details of use. Networks and Communication Department
29
Histogram Networks and Communication Department
30
Histogram Networks and Communication Department
R = randn(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard normal distribution. randn(M,N) or randn([M,N]) returns an M-by-N matri Networks and Communication Department
31
Discrete Graph – stem function
Stem and stairs ex: stem(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value Networks and Communication Department
32
Stair function Networks and Communication Department
stairs(Y) draws a stairstep graph of the elements of vector Y. Networks and Communication Department
33
Close and Save fig. Networks and Communication Department
34
Close & Saving Plots You can close all the current plots using ‘close all’ Often you may want to save a plot to include in another document, for example a Word document for a project report. From the figure window, save the plot in a file using the jpeg format. The jpeg format is pretty universal and compatible with MicroSoft Word and Powerpoint applications. It’s easy to do, give it a try.
35
Close all the current plots
Command : >>Close all Networks and Communication Department
36
Saving Figures files extenstion: .fig
also you can save .jpeg .tif .bmp .pdf. »plot3d_soln
37
Printing Figures using the Dialog Box: from Command Line:
File Menu / Print... >>printdlg from Command Line: Controlling Page Layout: File Menu / Print Preview >>printpreview print print('argument1','argument2',...) printdlg Print dialog box. printdlg prints the current figure For a complete listing of devices and options, see the online help for the PRINT command.
38
Exporting Figures Copying to Clipboard: Options: (Edit / Copy Option)
Copying: (Edit / Copy Figure)
39
Example 1 Example 1: Plot sin(x) and cos(x) over [0,2π], on the same plot with different colours Method 1: >> x = linspace(0,2*pi,1000); >> y = sin(x); >> z = cos(x); >> hold on; >> plot(x,y,‘b’); >> plot(x,z,‘g’); >> xlabel (‘X values’); >> ylabel (‘Y values’); >> title (‘Sample Plot’); >> legend (‘Y data’,‘Z data’); >> hold off; Networks and Communication Department
40
Ex 2. t=0:0.1:10; y= sin(t); plot(t,y) plot(t,y,'--r','linewidth',2) plot(t,y,'r--','linewidth',2) plot(t,y,'r--o') grid on grid off grid Networks and Communication Department
41
Ex 2 con. title('my plot') xlabel('time (s)') ylabel('amplitude') axis
>> axis off >> axis on >> axis([ ]) >> text(5,0,'my text') Networks and Communication Department
42
Ex 2 con. y2=cos(t); plot(t,y2); plot(t,y,t,y2) legend('sin','cos') plot(t,y) hold on plot(t,y2,'m') hold off Networks and Communication Department
43
Ex 2 con. subplot(2,1,2) plot(t,y) subplot(2,1,1) plot(t,y2)
Networks and Communication Department
44
The End Any Questions ? Networks and Communication Department
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.