This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:spring-2016:math445:lecture:scripts [2016/02/01 07:39] gibson |
gibson:teaching:spring-2016:math445:lecture:scripts [2016/02/03 13:54] (current) gibson [Logarithmic plots] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Math 445 lecture 3: scripts ====== | + | ====== Math 445 lecture 3: scripts and plotting ====== |
topics | topics | ||
<code> | <code> | ||
Matlab scripting | Matlab scripting | ||
- | plot, semilogy, semilogx, loglog | + | linestyles and plot modifiers: |
- | xlabel, ylabel, title, legend, axis, grid, clf, | + | xlabel, ylabel, title, legend, axis, grid, clf, |
- | linestyles | + | |
load, save | load, save | ||
- | what, pwd | + | plot, semilogy, semilogx, loglog |
+ | what, who, pwd, ls | ||
</code> | </code> | ||
Line 40: | Line 40: | ||
MATLAB Code files in the current folder /home/gibson/math445 | MATLAB Code files in the current folder /home/gibson/math445 | ||
- | plotsincos | + | </code> |
+ | When the script is run, Matlab opens a new figure window and draws the plot | ||
+ | |||
+ | <code matlab> | ||
>> plotsincos % execute the plotsincos script | >> plotsincos % execute the plotsincos script | ||
</code> | </code> | ||
- | When the script is run, Matlab opens a new figure window and draws the plot | ||
{{ :gibson:teaching:spring-2016:math445:sincos.png?direct&400 |}} | {{ :gibson:teaching:spring-2016:math445:sincos.png?direct&400 |}} | ||
+ | |||
+ | ===== Line styles and markers ===== | ||
+ | |||
+ | The above plotting script used the matlab **linestyle** codes '' 'b-' '' and '' 'r-' '' to generate blue and red curves for $\cos x$ and $\sin x$. Matlab's **help** function provides a pretty good description of how line and marker style codes work. | ||
+ | |||
+ | <code matlab> | ||
+ | |||
+ | >> help plot | ||
+ | |||
+ | % clip out irrelevant stuff here | ||
+ | |||
+ | Various line types, plot symbols and colors may be obtained with | ||
+ | plot(X,Y,S) where S is a character string made from one element | ||
+ | from any or all the following 3 columns: | ||
+ | |||
+ | b blue . point - solid | ||
+ | g green o circle : dotted | ||
+ | r red x x-mark -. dashdot | ||
+ | c cyan + plus -- dashed | ||
+ | m magenta * star (none) no line | ||
+ | y yellow s square | ||
+ | k black d diamond | ||
+ | w white v triangle (down) | ||
+ | ^ triangle (up) | ||
+ | < triangle (left) | ||
+ | > triangle (right) | ||
+ | p pentagram | ||
+ | h hexagram | ||
+ | | ||
+ | For example, plot(X,Y,'c+:') plots a cyan dotted line with a plus | ||
+ | at each data point; plot(X,Y,'bd') plots blue diamond at each data | ||
+ | point but does not draw any line. | ||
+ | </code> | ||
+ | |||
+ | ===== Plot modifiers ===== | ||
+ | |||
+ | Some helpful commands for modifying plots | ||
+ | |||
+ | <code matlab> | ||
+ | xlabel('foo') % add an x label | ||
+ | ylabel('bar') % add a y label | ||
+ | title('baz') % add a title | ||
+ | legend('first', 'second', 'third') % add a legend with these labels | ||
+ | axis on % turn on axes and ticks (default) | ||
+ | axis off % turn off axes and ticks | ||
+ | axis square % make the plot square | ||
+ | axis equal % scale the x and y axes the same | ||
+ | axis([xmin xmax ymin ymax]) % force these specific bounds on the axes | ||
+ | grid on % turn on grid markers | ||
+ | grid off % turn off grid markers | ||
+ | hold on % hold the current plot; the next plot will be plotted on top of this one | ||
+ | hold off % release the current plot; the next plot will erase this one | ||
+ | clf % clear figure (erase current plot) | ||
+ | figure(2) % open new figure window, numbered 2 | ||
+ | </code> | ||
+ | |||