Customizing plots after the fact

| categories: plotting | View Comments

Customizing plots after the fact

Customizing plots after the fact

John Kitchin

Sometimes it is desirable to make a plot that shows the data you want to present, and to customize the details, e.g. font size/type and line thicknesses afterwards. It can be tedious to try to add the customization code to the existing code that makes the plot. Today, we look at a way to do the customization after the plot is created.

clear all; close all; clc
x = linspace(0,2);
y1 = x;
y2 = x.^2;
y3 = x.^3;

plot(x,y1,x,y2,x,y3)
xlabel('x')
ylabel('f(x)')
title('plots of y = x^n')
legend({'x' 'x^2' 'x^3'},'location','best')

Matlab uses handle graphics with the set command to change properties. We can use a set of find commands to locate the Matlab object containing the current figure, and then a findall command to get the handles to all the axes, lines, and text in the current figure. Then we use the set command to change the properties of those objects!

a=findobj(gcf);

Each of the numbers that come from these commands represent an object that we have to set the properties of. You can see there are a lot of them. That is why it is difficult to change all them when you make the plot.

allaxes=findall(a,'Type','axes')
alllines=findall(a,'Type','line')
alltext=findall(a,'Type','text')
allaxes =

  181.0081
  173.0081
  181.0081
  173.0081


alllines =

  191.0081
  190.0081
  188.0081
  187.0081
  185.0081
  184.0081
  176.0081
  175.0081
  174.0085
  191.0081
  190.0081
  188.0081
  187.0081
  185.0081
  184.0081
  176.0081
  175.0081
  174.0085
  191.0081
  190.0081
  188.0081
  187.0081
  185.0081
  184.0081
  176.0081
  175.0081
  174.0085


alltext =

  235.0081
  234.0081
  233.0081
  232.0081
  189.0081
  186.0081
  183.0081
  182.0081
  231.0081
  230.0081
  192.0081
  180.0081
  179.0081
  178.0081
  177.0081
  235.0081
  234.0081
  233.0081
  232.0081
  189.0081
  186.0081
  183.0081
  182.0081
  231.0081
  230.0081
  192.0081
  180.0081
  179.0081
  178.0081
  177.0081
  189.0081
  186.0081
  183.0081

Now we set the font to Arial, bold, 14 pt on the axes, and make the axes thicker.

set(allaxes,'FontName','Arial','FontWeight','Bold','LineWidth',2,'FontSize',14);

we set the plot lines to be thicker and dashed

set(alllines,'Linewidth',2,'Linestyle','--');

and set all other text (labels, and titles) to be 14pt italic Arial

set(alltext,'FontName','Arial','FontAngle','italic','FontSize',14);

we can even change the figure width and height

width = 3; height = 5;
set(gcf,'units','inches','position',[1 1 width height])

% categories: plotting
Read and Post Comments

Picasso's short lived blue period with Matlab

| categories: plotting | View Comments

picasso_blue

Contents

Picasso's short lived blue period with Matlab

John Kitchin

It is an unknown fact that Picasso had a brief blue plotting period with Matlab before moving on to his more famous paintings. It started from irritation with the default colors available in Matlab for plotting. After watching his friend van Gogh cut off his own ear out of frustration with the ugly default colors, Picasso had to do something different.

clear all; clc; close all

default colors

Picasso wanted a series of horizontal lines with an eye-pleasing sequence of colors.

figure
hold all

% this plots horizontal lines for each y value of m.
for m = 1:0.5:50
  plot([0 50], [m m]);
end

egads! that is rough on the eyes. Picasso figured out a way to customize the plot color order for a figure! He used the cmu.colors function to create an array of colors, blues in this case in the order he wanted them plotted, roughly light to dark. see http://en.wikipedia.org/wiki/List_of_colors for a list of colors available, and install version 1.7 of +cmu.

customizing the order colors are plotted in

It is convenient to define this function handle shortcut to avoid having to type cmu.colors('alice blue') for every color

c = @cmu.colors;

Now we make an array of the colors we want.

blues = [c('alice blue')
    c('light blue')
    c('baby blue')
    c('sky blue')
    c('maya blue')
    c('cornflower blue')
    c('bleu de france')
    c('azure')
    c('sapphire')
    c('cobalt')
    c('blue')
    c('navy blue')
    c('duke blue')]
blues =

    0.9400    0.9700    1.0000
    0.6800    0.8500    0.9000
    0.4700    0.8100    0.9400
    0.5300    0.8100    0.9200
    0.4500    0.7600    0.9800
    0.3900    0.5800    0.9300
    0.1900    0.5500    0.9100
         0    0.5000    1.0000
    0.0600    0.3200    0.7300
         0    0.2800    0.6700
         0         0    1.0000
         0         0    0.5000
         0         0    0.6100

Create a figure, and set the color order property. gca is a command for get current axes, and we set the ColorOrder on that axes.

figure
set(gca, 'ColorOrder', blues)

use the hold all command to preserve the ColorOrder on each subsequent plot

hold all

for m = 1:0.5:50
  plot([0 50], [m m]);
end

This is much nicer to look at!

Summary

You can specify the ColorOrder for plots as described above. Picasso eventually gave up on Matlab as an artform, and moved on to painting.

% categories: plotting
Read and Post Comments

Check out the new Fall colors!

| categories: plotting | View Comments

Check out the new Fall colors!

Check out the new Fall colors!

John Kitchin

Contents

The standard colors

the standard colors available in plotting are not that pretty:

b     blue
g     green
r     red
c     cyan
m     magenta
y     yellow
k     black
w     white

yellow is particularly hard to see.

clear all; close all; clc;

x = linspace(0,1);
y = sin(x);

figure
plot(x,y,'y-')

cyan is not that attractive

plot(x,y,'c-')

green is pretty attrocius.

plot(x,y,'g-')

Doing better with colors

A large list of colors can be found online. Matlab allows you to specify a color by the RGB (red green blue) values, for example, deep carrot orange is defined by the RGB tuple [ 0.9100 0.4100 0.1700], and it is easier to see than yellow.

You specify the color of the line like this.

plot(x,y,'color',[0.9100    0.4100    0.1700])

using cmu.colors

It is not that convenient to type in the RGB tuples, so we created the cmu.colors provides a function to get a lot of colors deep carrot orange is much easier to see than the default yellow. Note this feature is only available for +cmu.version >= 1.7.

c = @cmu.colors; % shortcut function handle
c('deep carrot orange') % an ok looking dark orange. this returns the RGB
ans =

    0.9100    0.4100    0.1700

Instead of

figure
plot(x,y,'color',c('deep carrot orange'),'linewidth',3)

List of available colors

c('list')

% categories: plotting
ans = 

    'air force blue'
    'alice blue'
    'alizarin crimson'
    'almond'
    'amaranth'
    'amber'
    'amber (sae/ece)'
    'american rose'
    'amethyst'
    'android green'
    'anti-flash white'
    'antique brass'
    'antique fuchsia'
    'antique white'
    'ao (english)'
    'apple green'
    'apricot'
    'aqua'
    'aquamarine'
    'army green'
    'arsenic'
    'arylide yellow'
    'ash grey'
    'asparagus'
    'atomic tangerine'
    'auburn'
    'aureolin'
    'aurometalsaurus'
    'awesome'
    'azure'
    'azure mist/web'
    'baby blue'
    'baby blue eyes'
    'baby pink'
    'ball blue'
    'banana mania'
    'banana yellow'
    'battleship grey'
    'bazaar'
    'bear'
    'beau blue'
    'beaver'
    'beige'
    'bisque'
    'bistre'
    'bittersweet'
    'black'
    'blanched almond'
    'bleu de france'
    'blizzard blue'
    'blond'
    'blue'
    'blue (munsell)'
    'blue (ncs)'
    'blue (pigment)'
    'blue (ryb)'
    'blue bell'
    'blue gray'
    'blue-green'
    'blue-violet'
    'blush'
    'bole'
    'bondi blue'
    'boston university red'
    'boysenberry'
    'brandeis blue'
    'brass'
    'brick red'
    'bright cerulean'
    'bright green'
    'bright lavender'
    'bright maroon'
    'bright pink'
    'bright turquoise'
    'bright ube'
    'brilliant lavender'
    'brilliant rose'
    'brink pink'
    'british racing green'
    'bronze'
    'brown (traditional)'
    'brown (web)'
    'bubble gum'
    'bubbles'
    'buff'
    'bulgarian rose'
    'burgundy'
    'burlywood'
    'burnt orange'
    'burnt sienna'
    'burnt umber'
    'byzantine'
    'byzantium'
    'cadet'
    'cadet blue'
    'cadet grey'
    'cadmium green'
    'cadmium orange'
    'cadmium red'
    'cadmium yellow'
    'café au lait'
    'café noir'
    'cal poly pomona green'
    'cambridge blue'
    'camel'
    'camouflage green'
    'canary yellow'
    'candy apple red'
    'candy pink'
    'capri'
    'caput mortuum'
    'cardinal'
    'caribbean green'
    'carmine'
    'carmine pink'
    'carmine red'
    'carnation pink'
    'carnelian'
    'carolina blue'
    'carrot orange'
    'ceil'
    'celadon'
    'celeste (colour)'
    'celestial blue'
    'cerise'
    'cerise pink'
    'cerulean'
    'cerulean blue'
    'cg blue'
    'cg red'
    'chamoisee'
    'champagne'
    'charcoal'
    'chartreuse (traditional)'
    'chartreuse (web)'
    'cherry blossom pink'
    'chestnut'
    'chocolate (traditional)'
    'chocolate (web)'
    'chrome yellow'
    'cinereous'
    'cinnabar'
    'cinnamon'
    'citrine'
    'classic rose'
    'cobalt'
    'cocoa brown'
    'coffee'
    'columbia blue'
    'cool black'
    'cool grey'
    'copper'
    'copper rose'
    'coquelicot'
    'coral'
    'coral pink'
    'coral red'
    'cordovan'
    'corn'
    'cornell red'
    'cornflower blue'
    'cornsilk'
    'cosmic latte'
    'cotton candy'
    'cream'
    'crimson'
    'crimson glory'
    'cyan'
    'cyan (process)'
    'daffodil'
    'dandelion'
    'dark blue'
    'dark brown'
    'dark byzantium'
    'dark candy apple red'
    'dark cerulean'
    'dark champagne'
    'dark chestnut'
    'dark coral'
    'dark cyan'
    'dark electric blue'
    'dark goldenrod'
    'dark gray'
    'dark green'
    'dark jungle green'
    'dark khaki'
    'dark lava'
    'dark lavender'
    'dark magenta'
    'dark midnight blue'
    'dark olive green'
    'dark orange'
    'dark orchid'
    'dark pastel blue'
    'dark pastel green'
    'dark pastel purple'
    'dark pastel red'
    'dark pink'
    'dark powder blue'
    'dark raspberry'
    'dark red'
    'dark salmon'
    'dark scarlet'
    'dark sea green'
    'dark sienna'
    'dark slate blue'
    'dark slate gray'
    'dark spring green'
    'dark tan'
    'dark tangerine'
    'dark taupe'
    'dark terra cotta'
    'dark turquoise'
    'dark violet'
    'dartmouth green'
    'davys grey'
    'debian red'
    'deep carmine'
    'deep carmine pink'
    'deep carrot orange'
    'deep cerise'
    'deep champagne'
    'deep chestnut'
    'deep coffee'
    'deep fuchsia'
    'deep jungle green'
    'deep lilac'
    'deep magenta'
    'deep peach'
    'deep pink'
    'deep saffron'
    'deep sky blue'
    'denim'
    'desert'
    'desert sand'
    'dim gray'
    'dodger blue'
    'dogwood rose'
    'dollar bill'
    'dolphin'
    'drab'
    'duke blue'
    'earth yellow'
    'ecru'
    'eggplant'
    'eggshell'
    'egyptian blue'
    'electric blue'
    'electric crimson'
    'electric cyan'
    'electric green'
    'electric indigo'
    'electric lavender'
    'electric lime'
    'electric purple'
    'electric ultramarine'
    'electric violet'
    'electric yellow'
    'emerald'
    'eton blue'
    'fallow'
    'falu red'
    'fandango'
    'fashion fuchsia'
    'fawn'
    'feldgrau'
    'fern green'
    'ferrari red'
    'field drab'
    'fire engine red'
    'firebrick'
    'flame'
    'flamingo pink'
    'flavescent'
    'flax'
    'floral white'
    'fluorescent orange'
    'fluorescent pink'
    'fluorescent yellow'
    'folly'
    'forest green (traditional)'
    'forest green (web)'
    'french beige'
    'french blue'
    'french lilac'
    'french rose'
    'fuchsia'
    'fuchsia pink'
    'fulvous'
    'fuzzy wuzzy'
    'gainsboro'
    'gamboge'
    'ghost white'
    'ginger'
    'glaucous'
    'glitter'
    'gold (metallic)'
    'gold (web) (golden)'
    'golden brown'
    'golden poppy'
    'golden yellow'
    'goldenrod'
    'granny smith apple'
    'gray'
    'gray (html/css gray)'
    'gray (x11 gray)'
    'gray-asparagus'
    'green (color wheel) (x11 green)'
    'green (html/css green)'
    'green (munsell)'
    'green (ncs)'
    'green (pigment)'
    'green (ryb)'
    'green-yellow'
    'grullo'
    'guppie green'
    'halaya ube'
    'han blue'
    'han purple'
    'hansa yellow'
    'harlequin'
    'harvard crimson'
    'harvest gold'
    'heart gold'
    'heliotrope'
    'hollywood cerise'
    'honeydew'
    'hookers green'
    'hot magenta'
    'hot pink'
    'hunter green'
    'iceberg'
    'icterine'
    'inchworm'
    'india green'
    'indian red'
    'indian yellow'
    'indigo (dye)'
    'indigo (web)'
    'international klein blue'
    'international orange'
    'iris'
    'isabelline'
    'islamic green'
    'ivory'
    'jade'
    'jasmine'
    'jasper'
    'jazzberry jam'
    'jonquil'
    'june bud'
    'jungle green'
    'kelly green'
    'khaki (html/css) (khaki)'
    'khaki (x11) (light khaki)'
    'ku crimson'
    'la salle green'
    'languid lavender'
    'lapis lazuli'
    'laser lemon'
    'laurel green'
    'lava'
    'lavender (floral)'
    'lavender (web)'
    'lavender blue'
    'lavender blush'
    'lavender gray'
    'lavender indigo'
    'lavender magenta'
    'lavender mist'
    'lavender pink'
    'lavender purple'
    'lavender rose'
    'lawn green'
    'lemon'
    'lemon chiffon'
    'light apricot'
    'light blue'
    'light brown'
    'light carmine pink'
    'light coral'
    'light cornflower blue'
    'light crimson'
    'light cyan'
    'light fuchsia pink'
    'light goldenrod yellow'
    'light gray'
    'light green'
    'light khaki'
    'light mauve'
    'light pastel purple'
    'light pink'
    'light salmon'
    'light salmon pink'
    'light sea green'
    'light sky blue'
    'light slate gray'
    'light taupe'
    'light thulian pink'
    'light timberwolf'
    'light yellow'
    'lilac'
    'lime (color wheel)'
    'lime (web) (x11 green)'
    'lime green'
    'lincoln green'
    'linen'
    'lion'
    'liver'
    'lust'
    'magenta'
    'magenta (dye)'
    'magenta (process)'
    'magic mint'
    'magnolia'
    'mahogany'
    'maize'
    'majorelle blue'
    'malachite'
    'manatee'
    'mango tango'
    'mantis'
    'maroon (html/css)'
    'maroon (x11)'
    'mauve'
    'mauve taupe'
    'mauvelous'
    'maya blue'
    'meat brown'
    'medium aquamarine'
    'medium blue'
    'medium candy apple red'
    'medium carmine'
    'medium champagne'
    'medium electric blue'
    'medium jungle green'
    'medium lavender magenta'
    'medium orchid'
    'medium persian blue'
    'medium purple'
    'medium red-violet'
    'medium sea green'
    'medium slate blue'
    'medium spring bud'
    'medium spring green'
    'medium taupe'
    'medium teal blue'
    'medium turquoise'
    'medium violet-red'
    'melon'
    'midnight blue'
    'midnight green (eagle green)'
    'mikado yellow'
    'mint'
    'mint cream'
    'mint green'
    'misty rose'
    'moccasin'
    'mode beige'
    'moonstone blue'
    'mordant red 19'
    'moss green'
    'mountain meadow'
    'mountbatten pink'
    'msu green'
    'mulberry'
    'munsell'
    'mustard'
    'myrtle'
    'nadeshiko pink'
    'napier green'
    'naples yellow'
    'navajo white'
    'navy blue'
    'neon carrot'
    'neon fuchsia'
    'neon green'
    'non-photo blue'
    'north texas green'
    'ocean boat blue'
    'ochre'
    'office green'
    'old gold'
    'old lace'
    'old lavender'
    'old mauve'
    'old rose'
    'olive'
    'olive drab #7'
    'olive drab (web) (olive drab #3)'
    'olivine'
    'onyx'
    'opera mauve'
    'orange (color wheel)'
    'orange (ryb)'
    'orange (web color)'
    'orange peel'
    'orange-red'
    'orchid'
    'otter brown'
    'ou crimson red'
    'outer space'
    'outrageous orange'
    'oxford blue'
    'pakistan green'
    'palatinate blue'
    'palatinate purple'
    'pale aqua'
    'pale blue'
    'pale brown'
    'pale carmine'
    'pale cerulean'
    'pale chestnut'
    'pale copper'
    'pale cornflower blue'
    'pale gold'
    'pale goldenrod'
    'pale green'
    'pale magenta'
    'pale pink'
    'pale plum'
    'pale red-violet'
    'pale robin egg blue'
    'pale silver'
    'pale spring bud'
    'pale taupe'
    'pale violet-red'
    'pansy purple'
    'papaya whip'
    'paris green'
    'pastel blue'
    'pastel brown'
    'pastel gray'
    'pastel green'
    'pastel magenta'
    'pastel orange'
    'pastel pink'
    'pastel purple'
    'pastel red'
    'pastel violet'
    'pastel yellow'
    'patriarch'
    'paynes grey'
    'peach'
    'peach puff'
    'peach-orange'
    'peach-yellow'
    'pear'
    'pearl'
    'pearl aqua'
    'peridot'
    'periwinkle'
    'persian blue'
    'persian green'
    'persian indigo'
    'persian orange'
    'persian pink'
    'persian plum'
    'persian red'
    'persian rose'
    'persimmon'
    'phlox'
    'phthalo blue'
    'phthalo green'
    'piggy pink'
    'pine green'
    'pink'
    'pink pearl'
    'pink sherbet'
    'pink-orange'
    'pistachio'
    'platinum'
    'plum (traditional)'
    'plum (web)'
    'polar bear'
    'portland orange'
    'powder blue (web)'
    'princeton orange'
    'prune'
    'prussian blue'
    'psychedelic purple'
    'puce'
    'pumpkin'
    'purple (html/css)'
    'purple (munsell)'
    'purple (x11)'
    'purple heart'
    'purple mountain majesty'
    'purple pizzazz'
    'purple taupe'
    'quartz'
    'radical red'
    'raspberry'
    'raspberry glace'
    'raspberry pink'
    'raspberry rose'
    'raw umber'
    'razzle dazzle rose'
    'razzmatazz'
    'red'
    'red (munsell)'
    'red (ncs)'
    'red (pigment)'
    'red (ryb)'
    'red-brown'
    'red-violet'
    'redwood'
    'regalia'
    'rich black'
    'rich brilliant lavender'
    'rich carmine'
    'rich electric blue'
    'rich lavender'
    'rich lilac'
    'rich maroon'
    'rifle green'
    'robin egg blue'
    'rose'
    'rose bonbon'
    'rose ebony'
    'rose gold'
    'rose madder'
    'rose pink'
    'rose quartz'
    'rose taupe'
    'rose vale'
    'rosewood'
    'rosso corsa'
    'rosy brown'
    'royal azure'
    'royal blue (traditional)'
    'royal blue (web)'
    'royal fuchsia'
    'royal purple'
    'ruby'
    'ruddy'
    'ruddy brown'
    'ruddy pink'
    'rufous'
    'russet'
    'rust'
    'sacramento state green'
    'saddle brown'
    'safety orange (blaze orange)'
    'saffron'
    'salmon'
    'salmon pink'
    'sand'
    'sand dune'
    'sandstorm'
    'sandy brown'
    'sandy taupe'
    'sangria'
    'sap green'
    'sapphire'
    'satin sheen gold'
    'scarlet'
    'school bus yellow'
    'screamin green'
    'sea green'
    'seal brown'
    'seashell'
    'selective yellow'
    'sepia'
    'shadow'
    'shamrock green'
    'shocking pink'
    'sienna'
    'silver'
    'sinopia'
    'skobeloff'
    'sky blue'
    'sky magenta'
    'slate blue'
    'slate gray'
    'smalt (dark powder blue)'
    'smokey topaz'
    'smoky black'
    'snow'
    'spiro disco ball'
    'splashed white'
    'spring bud'
    'spring green'
    'st. patricks blue'
    'steel blue'
    'stil de grain yellow'
    'stizza'
    'straw'
    'sunglow'
    'sunset'
    'tan'
    'tangelo'
    'tangerine'
    'tangerine yellow'
    'taupe'
    'taupe gray'
    'tea green'
    'tea rose (orange)'
    'tea rose (rose)'
    'teal'
    'teal blue'
    'teal green'
    'tenné (tawny)'
    'terra cotta'
    'thistle'
    'thulian pink'
    'tickle me pink'
    'tiffany blue'
    'tigers eye'
    'timberwolf'
    'titanium yellow'
    'tomato'
    'toolbox'
    'topaz'
    'tractor red'
    'trolley grey'
    'tropical rain forest'
    'true blue'
    'tufts blue'
    'tumbleweed'
    'turkish rose'
    'turquoise'
    'turquoise blue'
    'turquoise green'
    'tuscan red'
    'twilight lavender'
    'tyrian purple'
    'ua blue'
    'ua red'
    'ube'
    'ucla blue'
    'ucla gold'
    'ufo green'
    'ultra pink'
    'ultramarine'
    'ultramarine blue'
    'umber'
    'united nations blue'
    'university of california gold'
    'unmellow yellow'
    'up forest green'
    'up maroon'
    'upsdell red'
    'urobilin'
    'usc cardinal'
    'usc gold'
    'utah crimson'
    'vanilla'
    'vegas gold'
    'venetian red'
    'verdigris'
    'vermilion'
    'veronica'
    'violet'
    'violet (color wheel)'
    'violet (ryb)'
    'violet (web)'
    'viridian'
    'vivid auburn'
    'vivid burgundy'
    'vivid cerise'
    'vivid tangerine'
    'vivid violet'
    'warm black'
    'wenge'
    'wheat'
    'white'
    'white smoke'
    'wild blue yonder'
    'wild strawberry'
    'wild watermelon'
    'wine'
    'wisteria'
    'xanadu'
    'yale blue'
    'yellow'
    'yellow (munsell)'
    'yellow (ncs)'
    'yellow (process)'
    'yellow (ryb)'
    'yellow orange'
    'yellow-green'
    'zaffre'
    'zinnwaldite brown'

Read and Post Comments

plotting two datasets with very different scales

| categories: plotting | View Comments

subplots

Contents

plotting two datasets with very different scales

sometimes you will have two datasets you want to plot together, but the scales will be so different it is hard to seem them both in the same plot. Here we examine a few strategies to plotting this kind of data.

x = linspace(0,2*pi);
y1 = sin(x);
y2 = 0.01*cos(x);

plot(x,y1,x,y2)
legend('y1','y2')
% in this plot y2 looks almost flat!

make two plots!

this certainly solves the problem, but you have two full size plots, which can take up a lot of space in a presentation and report. Often your goal in plotting both data sets is to compare them, and it is easiest to compare plots when they are perfectly lined up. Doing that manually can be tedious.

plot(x,y1); legend('y1')
plot(x,y2); legend('y2')

Scaling the results

sometimes you can scale one dataset so it has a similar magnitude as the other data set. Here we could multiply y2 by 100, and then it will be similar in size to y1. Of course, you need to indicate that y2 has been scaled in the graph somehow. Here we use the legend.

plot(x,y1,x,100*y2)
legend('y1','100*y2','location','southwest')

double-y axis plot

using two separate y-axes can solve your scaling problem. Note that each y-axis is color coded to the data. It can be difficult to read these graphs when printed in black and white

plotyy(x,y1,x,y2)
legend('y1','y2','location','southwest')

Setting the ylabels on these graphs is trickier than usual.

[AX,H1,H2] = plotyy(x,y1,x,y2)
legend('y1','y2','location','southwest')
set(get(AX(1),'Ylabel'),'String','y1')
set(get(AX(2),'Ylabel'),'String','y2')
AX =

  409.0186  411.0195


H1 =

  410.0281


H2 =

  412.0215

note that y2 is cut off just a little. We need to modify the axes position to decrease the width.

p = get(AX(1),'Position') % [x y width height]
% The width is too large, so let's decrease it a little
p(3)=0.75;

set(AX(1),'Position',p) % it doesn't seem to matter which axes we modify
p =

    0.1300    0.1100    0.7750    0.8150

subplots

an alternative approach to double y axes is to use subplots. The subplot command has a syntax of subplot(m,n,p) which selects the p-th plot in an m by n array of plots. Then you use a regular plot command to put your figure there. Here we stack two plots in a 2 (row) by 1 (column).

subplot(2,1,1)
plot(x,y1); legend('y1')

subplot(2,1,2)
plot(x,y2); legend('y2')
'done'
% categories: plotting
ans =

done

Read and Post Comments

Basic plotting tutorial

| categories: basic matlab, plotting | View Comments

Basic plotting tutorial

Basic plotting tutorial

Contents

it is a good idea to add these lines to the top of your m-files

close all  % close all figure windows that are open
clear all  % clear all the variables currently stored in memory
clc        % clear the commands in the command window

% Plotting functions in Matlab
% This m-file describes some very basic ways to plot functions in Matlab.

ezplot with functions described as strings

you can quickly plot a function of one variable with the ezplot command.

ezplot('x^3 - x^2 +x -1')
xlabel('x')
ylabel('f(x)')

plotting two functions with ezplot

if you try to plot another function, you will see the first function disappear. We have to tell Matlab what do with the hold command

figure
hold on
ezplot('x^3 - x^2 + x -1')
ezplot('-x^3 + x^2 - x + 1')
hold off

% Note that you cannot easily tell which graph is which because the two
% lines are the same color. Lets change the color and style of the first
% plot to be red and dashed, and add a legend so we can see which line is
% which. We do that by saving a reference to each figure in a variable so
% we can use the set function to modify each graph.

figure
hold on
h1 = ezplot('x^3 - x^2 + x -1');
h2 = ezplot('-x^3 + x^2 - x + 1');
hold off

set(h1,'color','r','linestyle','--')
legend('function 1','function2')

defining and plotting functions

strings are not always convenient to plot, especially if there are a lot of numbers in the equations, or if there are multiple variables. Let's plot the van der Waal's equation of state to show this.

$$f(V) = V^3 - \frac{pnb + nRT}{p}V^2 + \frac{n^2a}{p}V - \frac{n^3ab}{p} = 0$$

where a and b are constants. This equation cannot be plotted as a string like we did in the examples above. Instead, we need to define a function. Our goal is to find where this function is equal to zero. We can do that graphically.

% numerical values of the constants
a = 3.49e4;
b = 1.45;
p = 679.7;
T = 683;
n = 1.136;
R = 10.73;

% we define a function handle that is f(V)
f = @(V) V.^3 - (p*n*b+n*R*T)/p*V.^2 + n^2*a/p*V - n^3*a*b/p;

figure
ezplot(f)

%the large scale of the y-axis makes it difficult to see where the function
%is equal to zero, so we change the limits of each axis.
xlim([2 6])
ylim([-5 5])
xlabel('Volume')
ylabel('f(V)')
%it appears that f(V) = 0 around V=5
f(4.9)
f(5.1)
ans =

   -0.4486


ans =

    0.0145

we can add these two points to the figure with the plot command. Here we add them as red circles.

hold on
plot(4.9,f(4.9),'ro')
plot(5.1,f(5.1),'ro')
hold off

% The zero is definitely between V=4.9 and V = 5.1 because f changes sign
% between these two points.
%alternatively, we could tell ezplot to plot f from x=4 to x=6 like this
figure
ezplot(f,[4 6])
% categories: Basic Matlab, Plotting
% post_id = 625; %delete this line to force new post;
Read and Post Comments

« Previous Page -- Next Page »