banner



How To Change Axis Range In Matlab

Plotting graphs¶

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

Easy plots¶

_images/ezplot_xsq43.png

The simplest buit-in part for plotting an explicit function is ezplot command. For example, we desire to plot a parabola on the interval [-1,1]

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

Nosotros can simply use the following command:

The start input of ezplot command is a string describing the role. The 2nd input (which is optional) is the interval of interest in the graph. Note that the function clarification is not necessarily a function in x variable. Nosotros can use whatever variable we like:

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

If the office definition involves some abiding parameter, write information technology explicitly:

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

              >>              y              =              2;              >>              ezplot              (              'x^2+y'              )            

MATLAB will interpret y as a variable (not a value of 2). Hence, it will create a contour plot of the function

x^2+y = 0.

_images/ezplot_xsq_equals_y43.png

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

Plot control¶

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

where x and y are each a vector. Both vector must have the same number of elements.

_images/plot_xy_exp39.png

For example:

              >>              x              =              0:0.i:5;              >>              y              =              exp              (              -              ten              );              >>              plot              (              10              ,              y              )            

Once the plot command is executed, the figure Window opens and the plot is displayed

The plot appears on the screen in blueish which is the default line colour.

The plot command has boosted arguments that tin be used to specify the color and way of the line and the color and type of markers, if whatever are desired. With these options the control has the form:

              plot              (              ten              ,              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 Way Specifier
solid (default) -
dashed --
dotted :
nuance-dot -.

The line color specifiers are:

Line Color Specifier
cherry r
green thousand
blue b
cyan c
magenta 1000
yellow y
black thousand
white w

The marker type specifiers are:

Marker Type Specifier
plus sign +
circle o
asterisk *
indicate .
cantankerous x
triangle (pointed up) ^
triangle (pointed downward) 5
square s
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 command equally strings. Inside the cord, the specifiers can be typed in any club and the specifiers are optional. For case:

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

This command creates a plot with solid carmine line and the mark is a circle.

Holding Name and PropertyValue¶

Backdrop 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 Belongings Name is typed as a string, followed by a comma and a value for the property, all inside the plot command.

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

_images/plot_xy_expsin_linspec36.png

For case, the command:

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

creates a plot with a blue dashed line and squares as markers. The linewidth is 2 points and the size of the square markers is 8 points. The marking has greenish filling.

Formatting a plot¶

Plots tin exist formatted by using MATLAB command that follow the plot or fplot commands, or interactively past 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 description on the ten- and y-axes, respectively after we have plotted a graph. The usages are:

                >>                xlabel                (                'text as string'                )              

and:

                >>                ylabel                (                'text as string'                )              

The title command¶

A title can exist added to the plot with the command:

                >>                championship                (                'text as string'                )              

The text will announced on the meridian of the figure equally a title.

The text command¶

A text characterization tin be placed in the plot with the text or gtext commands:

                >>                text                (                x                ,                y                ,                'text as cord'                )                >>                gtext                (                'text as string'                )              

The text command places the text in the figure such that the first character is positioned at the indicate with the coordinates 10,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 control places a legend on the plot. The legend shows a sample of the line type of each graph that is plotted, and places a characterization, specified past the user, abreast the line sample. The usage is:

                >>                legend                (                'string 1'                ,                'string two'                ,...                )              

_images/plot_xy_distance30.png

For case, the command:

                >>                t                =                linspace                (0,ten,100);                due south                =                t                .^2/two;                >>                plot                (                t                ,                s                );                >>                xlabel                (                'Time (sec)'                );                >>                ylabel                (                'distance (grand)'                );                >>                title                (                'Altitude as a function of t'                );                >>                legend                (                'distance'                )              

creates a plot shown on the right.

Formatting the texts¶

The texts in the xlabel, ylabel, title, text and legend commands can exist formatted to customize the font, size, style (italic, bold, etc.), and color. Formatting can be washed past adding optional PropertyName and PropertyValue arguments post-obit the string inside the control. For example:

                >>                text                (                x                ,                y                ,                'text as cord'                ,                'PropertyName'                ,                PropertyValue                )              

Some of the PropertyName are:

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

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

                >>                title                (                '\bf The values of X_{ij}'                ,                'FontSize'                ,xviii)              

The axis command¶

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

Commands Description
axis([xmin, xmax, ymin, ymax]) sets the limits of both x and y
axis equal sets the aforementioned calibration for both axes
axis square sets the axes region to be foursquare
axis tight sets the axis limits to the range of the information

The grid command¶

_images/plot_xsinx_gridaxis29.png

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

For instance:

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

Plotting multiple graphs¶

In may situations there is a need to make several graphs in the aforementioned plot. There are 2 methods to plot multiple graphs in one figure. Ane is by using the plot control, the other is past using the hold on, hold off commands.

i. Using the plot command¶

Two or more than graphs tin can be created in the same plot by typing pairs of vectors inside the plot control. For instance:

creates 3 graphs: y vs x , 5 vs u , and t vs s , all in the same plot. The vector of each pair must accept the same length. MATLAB automatically plots the graphs in dissimilar colors so that they tin be identified. It is likewise possible to add line specifiers post-obit each pair. For case:

                >>                plot                (                10                ,                y                ,                '-bo'                ,                u                ,                5                ,                '--rx'                ,                s                ,                t                ,                'k:s'                )              

plots y vs x with a solid blue line and circles, five vs u with a dashed cherry-red lines with cross signs, t vs s with a dotted greenish line and square markers.

_images/plot_multiple27.png

For instance, we plot the function y = 3x^3-26x+10, and its starting time 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 tin can be written equally:

                >>                x                =                linspace                (                -2,four,20);                >>                y                =                3*                ten                .^three-26*                10                +x;                >>                dy                =                9*                ten                .^two-26;                >>                ddy                =                xviii*                x                ;                >>                ddy                =                18*                x                ;                >>                plot                (                x                ,                y                ,                10                ,                dy                ,                x                ,                ddy                );                >>                legend                (                'y'                ,                'first derivative'                ,                '2nd derivative'                );              

The default colors of multiple graphs in MATLAB starting time from blue, ruddy, and green, respectively.

2. Using the hold on , hold off control¶

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

_images/plot_holdon26.png

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

                >>                plot                (                x                ,                y                ,                '-b'                );                >>                hold                on                >>                plot                (                x                ,                dy                ,                '--r'                );                >>                plot                (                ten                ,                ddy                ,                ':k'                );                >>                hold                off              

Logarithmic Scales¶

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

For example, detect the divergence betwixt the two commands:

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

(The logspace creates a vector of 100 elements in log scale with the first element x^0 and the terminal element ten^(3) .)

Polar plots¶

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

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

_images/polar20.png

where theta and radius are vectors whose elements define the coordinates of the points to be plotted. The line specifiers are the same as in the plot control. To plot a office r=f(\theta) in a sure domain, a vector for values of \theta is created start, and so a vector r with the corresponding values of f(\theta) is created using 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              =              3*              cos              (              theta              /2)              .^2+              theta              ;              >>              polar              (              theta              ,              r              )            

Multiple plots on the same window¶

Multiple plots on the aforementioned page can be created with the subplot command, which has the form:

_images/subplot20.png

The command divides the Figure Window into mxn rectangular subplots where plots will exist created. The subplots are bundled like elements in a mxn matrix where each element is a subplot. The subplots are numbered from i through mn . The number increases from left to right within a row, from the get-go row to the last. The command subplot(m,n,p) makes the subplot p current. This means that the adjacent plot command will create a plot in this subplot.

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

              >>              subplot              (2,2,one);              ezplot              (              'sin(x)'              );              >>              subplot              (2,two,two);              ezplot              (              'exp(-ten)'              );              >>              subplot              (2,ii,3);              ezplot              (              'x^2'              );              >>              subplot              (2,two,4);              ezplot              (              'sin(ten)/x'              );            

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 supercede the existing plot. Information technology is also possible to open additional Figure Window. This is done by typing the command effigy . Every fourth dimension the command effigy is entered, MATLAB opens a new Figure Window and afterward than nosotros can enter the plot command to generate a plot in the terminal agile Figure Window.

The figure control can as well have an input argument that is an integer figure(northward) . The number corresponds to the number of a corresponding Figure Window. Effigy Windows can be closed with the close command. Several forms of the commmand are:

  • close closes the active Effigy Window
  • close(due north) closes the nth Figure Window
  • close all closes all Effigy Windows that are open

Other types of plots (bar, histogram)¶

We illustrate various kinds of plots by examples.

1. Vertical bar plots¶

_images/vbar20.png

Function format: bar(ten,y) :

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

2. Horizontal bar plots¶

_images/hbar20.png

Function format: barh(x,y) :

                >>                twelvemonth                =                [1988:1994];                >>                sle                =                [8 12 twenty 22 eighteen 24 27];                >>                barh                (                year                ,                sle                ,                'r'                );                >>                xlabel                (                'Year'                );                ylabel                (                'Sales (Millions)'                );              

3. Stairs plots¶

_images/stairs20.png

Office format: stairs(ten,y) :

                >>                k                =i:five;                pmf                =                [i/four 1/4 1/4 1/8 1/viii];                >>                cdf                =                cumsum                (                pmf                );                >>                stairs                (                g                ,                cdf                );              

four. Stalk plots¶

_images/stem20.png

Role format: stem(x,y) :

                >>                m                =                -v:5;                Rk                =                0.5.^                abs                (                k                );                >>                stem                (                thousand                ,                Rk                );              

five. Pie plots¶

_images/pie20.png

Function format: pie(ten) :

                >>                grade                =[11 18 26 9 5];                >>                pie                (                grade                )              

6. Histograms¶

Histograms are plots that show the distribution of data. The overall range of a given set of information points is divided to smaller subranges (bins), and the histogram shows how many data points are in each bin. The elevation 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 M is the number of bins. For instance, we generate 10000 random numbers according to Gaussian distribution. Its histogram should look like a bell curve (due to e^{-x^2/2} term in the density part). 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 means 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 ten and y incorporate the x values and the y values, and the vector z contains the office values associated with every indicate of x and y .

MATLAB function meshgrid makes information technology easy to create the vector x,y arrays required for these plots. Then we evaluate the role values to plot at each of those (10,y) locations. Finally we call role mesh,surf or contour to create the plot. For case, we plot the ii-dimensional Gaussian density office

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

This is done by typing:

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

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

Posted by: holderbray1962.blogspot.com

0 Response to "How To Change Axis Range In Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel