banner



How To Change The Plot Scale In Matlab

Plotting graphs¶

Plotting graphs is a very mutual tool for illustrating results in science. MATLAB has many commmands that tin can exist used for creating various kinds of plots.

Easy plots¶

_images/ezplot_xsq43.png

The simplest buit-in office for plotting an explicit function is ezplot control. For example, we want to plot a parabola on the interval [-1,ane]

f(x) = x^2,\quad  -1 \leq x \leq 1

We tin can just utilize the following command:

The first input of ezplot control is a string describing the function. The second input (which is optional) is the interval of interest in the graph. Notation that the function clarification is not necessarily a role in 10 variable. Nosotros can utilize any variable we like:

              >>              ezplot              (              'sin(b)'              ,[              -two three])            

If the function definition involves some constant parameter, write it explicitly:

In this case, if we assign a value ii to a variable first, says y=2 , when executing:

              >>              y              =              ii;              >>              ezplot              (              'ten^2+y'              )            

MATLAB will translate y as a variable (non a value of ii). Hence, information technology volition create a contour plot of the part

x^2+y = 0.

_images/ezplot_xsq_equals_y43.png

Another related command is fplot . It is used to plot a function with the form y = f(x) between specified limits.

Plot command¶

The plot command is used to create a 2-dimensional plot. The simplest class of the control is:

where ten and y are each a vector. Both vector must accept the same number of elements.

_images/plot_xy_exp39.png

For instance:

              >>              10              =              0:0.1:5;              >>              y              =              exp              (              -              x              );              >>              plot              (              x              ,              y              )            

In one case the plot control is executed, the figure Window opens and the plot is displayed

The plot appears on the screen in blue which is the default line color.

The plot control has additional arguments that can be used to specify the color and style of the line and the color and type of markers, if whatsoever are desired. With these options the command has the class:

              plot              (              10              ,              y              ,              'linespec'              ,              'PropertyName'              ,              PropertyValue              )            

Line Specifiers ( linespec

Line specifiers are optional and can ce used to define the style and colour of the line and the type of markers (if markers are desired). The line style specifiers are:

Line Manner Specifier
solid (default) -
dashed --
dotted :
dash-dot -.

The line colour specifiers are:

Line Colour Specifier
carmine r
green 1000
blue b
cyan c
magenta m
xanthous y
blackness k
white w

The marker type specifiers are:

Marker Type Specifier
plus sign +
circle o
asterisk *
point .
cantankerous x
triangle (pointed up) ^
triangle (pointed down) v
square southward
diamond d
five-pointed star p
sixed-pointed star h
triangle (pointed left) <
triangle (pointed right) >

_images/plot_xy_2tox_linspec37.png

The specifiers are typed inside the plot control equally strings. Within the string, the specifiers can be typed in whatever order and the specifiers are optional. For instance:

                >>                x                =                0:5;                y                =                two.^                x                ;                plot                (                10                ,                y                ,                '-or'                )              

This control creates a plot with solid ruddy line and the marker is a circumvolve.

Property Name and PropertyValue¶

Properties are optional and can be used to specify the thickness of the line, the size of the marker, and the colors of the marker'south edge line and fill. The Property Proper name is typed every bit a string, followed past a comma and a value for the property, all within the plot command.

Belongings Name Description
linewidth the width of the line (default 0.5, possible values are 1,2,three,...)
markersize the size of the marker (e.g., 5,6,... )
markeredgecolor the color of the edge line for filled marker (e.thousand., r , b )
markerfacecolor the color of the filling for filled markers (e.g, r , b )

_images/plot_xy_expsin_linspec36.png

For case, the command:

                >>                10                =                linspace                (0,                pi                ,50);                y                =                exp                (                -                x                )                .*                sin                (8*                x                );                >>                plot                (                x                ,                y                ,                '--sb'                ,                'linewidth'                ,ii,                'markersize'                ,8,                'markerfacecolor'                ,                'g'                )              

creates a plot with a blueish dashed line and squares equally markers. The linewidth is 2 points and the size of the foursquare markers is 8 points. The mark has green filling.

Formatting a plot¶

Plots can be formatted by using MATLAB control that follow the plot or fplot commands, or interactively by using the plot editor in the Figure Window. Here we will explain the the first method which is more useful since a formatted plot tin be created automatically every time the program is executed.

The xlabel and ylabel control¶

xlabel and ylabel create clarification on the x- and y-axes, respectively afterwards nosotros have plotted a graph. The usages are:

                >>                xlabel                (                'text as string'                )              

and:

                >>                ylabel                (                'text as string'                )              

The title command¶

A championship can be added to the plot with the command:

                >>                title                (                'text as string'                )              

The text will appear on the top of the figure every bit a title.

The text control¶

A text label can be placed in the plot with the text or gtext commands:

                >>                text                (                x                ,                y                ,                'text as string'                )                >>                gtext                (                'text every bit string'                )              

The text command places the text in the figure such that the first grapheme is positioned at the point with the coordinates x,y (according to the axes of the effigy). The gtext command places the text at a position specified by the user (with the mouse).

The legend control¶

The legend command places a fable on the plot. The legend shows a sample of the line type of each graph that is plotted, and places a label, specified past the user, beside the line sample. The usage is:

                >>                legend                (                'string one'                ,                'string 2'                ,...                )              

_images/plot_xy_distance30.png

For example, the control:

                >>                t                =                linspace                (0,10,100);                s                =                t                .^ii/2;                >>                plot                (                t                ,                southward                );                >>                xlabel                (                'Time (sec)'                );                >>                ylabel                (                'distance (g)'                );                >>                title                (                'Distance as a role of t'                );                >>                fable                (                'distance'                )              

creates a plot shown on the right.

Formatting the texts¶

The texts in the xlabel, ylabel, title, text and legend commands can be formatted to customize the font, size, way (italic, bold, etc.), and color. Formatting can exist done past adding optional PropertyName and PropertyValue arguments following the string inside the command. For instance:

                >>                text                (                x                ,                y                ,                'text every bit string'                ,                'PropertyName'                ,                PropertyValue                )              

Some of the PropertyName are:

Property Name Description
Rotation the orientation of the text (in degree)
FontSize the size of the font (in points)
FontWeight the weight of the characters ( light, normal, bold )
Color the color of the text (e.chiliad., r , b , etc.)

Some formatting tin can besides be done by adding modifiers within the string. For example, adding \bf , \it , or \rm , will create a text in bold font, italic mode, and with normal font, rexpectively. A single character tin can be displayed every bit a subscript or a superscript past typing _ (the underscore character) or ^ in front of the character, respectively. A long superscript or subscript tin can be typed inside { } following the _ or the ^ . For example:

                >>                championship                (                '\bf The values of X_{ij}'                ,                'FontSize'                ,18)              

The centrality command¶

When the plot(ten,y) command is executed, MATLAB creates axes with limits that are based on the minimum and maximum values of the elements of x and y . The axis command can be used to alter the range of the axes. Here are some possible forms of the centrality command:

Commands Description
axis([xmin, xmax, ymin, ymax]) sets the limits of both ten and y
axis equal sets the same scale for both axes
axis square sets the axes region to exist foursquare
axis tight sets the centrality limits to the range of the data

The grid control¶

_images/plot_xsinx_gridaxis29.png

grid on adds filigree lines to the plot. grid off removes filigree lines from the plot.

For example:

                >>                fplot                (                '10^ii+iv*sin(2*x)-1'                ,[                -3 3])                >>                filigree                on                >>                axis                ([                -2 ii 0 5])              

Plotting multiple graphs¶

In may situations there is a need to brand several graphs in the same plot. There are ii methods to plot multiple graphs in one figure. One is by using the plot command, the other is by using the hold on, agree off commands.

1. Using the plot command¶

Two or more than graphs can exist created in the same plot by typing pairs of vectors inside the plot command. For example:

creates 3 graphs: y vs 10 , 5 vs u , and t vs southward , all in the same plot. The vector of each pair must take the aforementioned length. MATLAB automatically plots the graphs in different colors so that they tin exist identified. It is also possible to add line specifiers following each pair. For example:

                >>                plot                (                ten                ,                y                ,                '-bo'                ,                u                ,                v                ,                '--rx'                ,                s                ,                t                ,                'thou:s'                )              

plots y vs x with a solid blue line and circles, five vs u with a dashed cerise lines with cross signs, t vs southward with a dotted green line and square markers.

_images/plot_multiple27.png

For case, we plot the function y = 3x^3-26x+10, and its first derivative y'=9x^2-26 and second derivatives y"= 18x , for -2 \leq x \leq 4 , all in the same plot. A script file that creates these graphs can be written as:

                >>                x                =                linspace                (                -2,iv,20);                >>                y                =                3*                x                .^3-26*                x                +10;                >>                dy                =                9*                x                .^2-26;                >>                ddy                =                eighteen*                ten                ;                >>                ddy                =                18*                x                ;                >>                plot                (                x                ,                y                ,                10                ,                dy                ,                ten                ,                ddy                );                >>                legend                (                'y'                ,                'first derivative'                ,                'second derivative'                );              

The default colors of multiple graphs in MATLAB start from blue, red, and greenish, respectively.

2. Using the hold on , concur off command¶

To plot several graphs using the agree on, hold off commands, one graph is plotted first with the plot command. Then the concord on command is typed. This keeps the Effigy Window with the first plot open, including the centrality properties and the formatting. Additional graphs can be added with plot commands that are typed next. The agree off command stops this process. It returns MATLAB to the default mode in which the plot command erases the previous plot and resets the centrality properties.

_images/plot_holdon26.png

With the information from the previous example, we can plot y and its derivatives by typing commands shown in the script:

                >>                plot                (                10                ,                y                ,                '-b'                );                >>                concur                on                >>                plot                (                x                ,                dy                ,                '--r'                );                >>                plot                (                x                ,                ddy                ,                ':one thousand'                );                >>                hold                off              

Logarithmic Scales¶

The semilogy function plots x information on linear axes and y information on logarithmic axes. The semilogx function plots 10 data on logarithmic axes and y information on linear axes. The loglog part plots both 10 and y information on logarithmic axes.

For example, notice the departure between the ii commands:

              >>              x              =              logspace              (0,3,100);              >>              y              =              x              .^2;              >>              semilogy              (              ten              ,              y              )              >>              loglog              (              x              ,              y              )            

(The logspace creates a vector of 100 elements in log calibration with the first element 10^0 and the concluding chemical element 10^(3) .)

Polar plots¶

The polar command is used to plot functions in polar coordinates. The control has the class:

              >>              polor              (              theta              ,              radius              ,              'linespec'              )            

_images/polar20.png

where theta and radius are vectors whose elements ascertain the coordinates of the points to be plotted. The line specifiers are the same as in the plot command. To plot a function r=f(\theta) in a certain domain, a vector for values of \theta is created first, and then a vector r with the corresponding values of f(\theta) is created using chemical element-wise calculation.

For example, a plot of the function

r = 3 \cos^2(\theta/2)+\theta, \quad \mbox{for} \quad  0 \leq \theta \leq 2 \pi

is done by:

              >>              theta              =              linspace              (0,2*              pi              ,200);              >>              r              =              three*              cos              (              theta              /2)              .^2+              theta              ;              >>              polar              (              theta              ,              r              )            

Multiple plots on the same window¶

Multiple plots on the same page can exist created with the subplot control, which has the grade:

_images/subplot20.png

The command divides the Effigy Window into mxn rectangular subplots where plots volition be created. The subplots are arranged similar elements in a mxn matrix where each chemical element is a subplot. The subplots are numbered from 1 through mn . The number increases from left to right within a row, from the commencement row to the last. The control subplot(m,n,p) makes the subplot p current. This ways that the adjacent plot command will create a plot in this subplot.

For instance, we create a plot that has 2 rows and 2 columns:

              >>              subplot              (2,2,1);              ezplot              (              'sin(x)'              );              >>              subplot              (two,two,2);              ezplot              (              'exp(-x)'              );              >>              subplot              (2,2,iii);              ezplot              (              'x^2'              );              >>              subplot              (2,2,four);              ezplot              (              'sin(x)/ten'              );            

Multiple Figure Windows¶

When the plot or any other commands that generates a plot is executed, the Figure Window opens and displays the plot. If a Effigy Window is already open up and the plot control is executed, a new plot will replace the existing plot. Information technology is as well possible to open up additional Figure Window. This is washed by typing the command figure . Every fourth dimension the command figure is entered, MATLAB opens a new Figure Window and subsequently than we can enter the plot command to generate a plot in the last agile Figure Window.

The figure control can also accept an input argument that is an integer effigy(n) . The number corresponds to the number of a corresponding Figure Window. Figure Windows can be closed with the close command. Several forms of the commmand are:

  • close closes the active Effigy Window
  • close(northward) closes the nth Effigy Window
  • shut all closes all Effigy Windows that are open up

Other types of plots (bar, histogram)¶

We illustrate diverse kinds of plots by examples.

1. Vertical bar plots¶

_images/vbar20.png

Office format: bar(x,y) :

                >>                yr                =                [1988:1994];                >>                sle                =                [8 12 20 22 xviii 24 27];                >>                bar                (                yr                ,                sle                );                xlabel                (                'Year'                );                >>                ylabel                (                'Sales (Millions)'                );              

ii. Horizontal bar plots¶

_images/hbar20.png

Function format: barh(10,y) :

                >>                twelvemonth                =                [1988:1994];                >>                sle                =                [viii 12 20 22 xviii 24 27];                >>                barh                (                yr                ,                sle                ,                'r'                );                >>                xlabel                (                'Twelvemonth'                );                ylabel                (                'Sales (Millions)'                );              

iii. Stairs plots¶

_images/stairs20.png

Function format: stairs(10,y) :

                >>                m                =1:five;                pmf                =                [1/4 1/4 1/iv 1/viii ane/8];                >>                cdf                =                cumsum                (                pmf                );                >>                stairs                (                grand                ,                cdf                );              

4. Stalk plots¶

_images/stem20.png

Function format: stem(x,y) :

                >>                k                =                -v:v;                Rk                =                0.five.^                abs                (                k                );                >>                stem                (                g                ,                Rk                );              

5. Pie plots¶

_images/pie20.png

Role format: pie(ten) :

                >>                grade                =[11 xviii 26 nine 5];                >>                pie                (                grade                )              

half dozen. Histograms¶

Histograms are plots that prove the distribution of data. The overall range of a given set of data points is divided to smaller subranges (bins), and the histogram shows how many information points are in each bin. The summit of the bar corresponds to the number of data points in the bin. The simplest form of the command is:

_images/hist20.png

where y is a vector with the data points, and G is the number of bins. For example, nosotros generate 10000 random numbers according to Gaussian distribution. Its histogram should await like a bell bend (due to e^{-x^2/2} term in the density function). This is done by typing:

                >>                y                =                randn                (10000,1);                >>                hist                (                y                ,100)              

3-D plots¶

_images/gaussian8.png

Surface, mesh, and contour plots are convenient ways to represent data that is a function of two independent variables, says z = f(x,y). To plot data, a user must create iii equal-sized arrays 10,y , and z . The vector x and y contain the ten values and the y values, and the vector z contains the function values associated with every point of ten and y .

MATLAB function meshgrid makes it easy to create the vector x,y arrays required for these plots. And so nosotros evaluate the function values to plot at each of those (x,y) locations. Finally we call role mesh,surf or contour to create the plot. For example, we plot the 2-dimensional Gaussian density function

z = f(x,y) = e^{-(x^2+y^2)/2}

This is washed by typing:

              >>              [              x              ,              y              ]=              meshgrid              (              -3:0.05:3);              >>              z              =              exp              (              -0.5*              (              x              .^ii+              y              .^2));              >>              mesh              (              ten              ,              y              ,              z              )            

Source: http://jitkomut.eng.chula.ac.th/matlab/graphic.html

Posted by: mcdanielmorly1947.blogspot.com

0 Response to "How To Change The Plot Scale In Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel