# MATLAB Graphics

## 2D Graphics

Here is the UK population from 1960 to 2013 Time interval (years)

```year = 1960:2013;
% UK Population
uk_pop = [52.40 52.80 53.25 53.65 54.00 54.35 54.65 54.94 55.21 55.44 ...
55.55 55.90 56.09 56.19 56.23 56.23 56.21 56.19 56.22 56.35 ...
56.31 56.33 56.31 56.33 56.42 56.55 56.68 56.80 56.93 57.08 ...
57.25 57.42 57.58 57.72 57.87 58.02 58.87 58.32 58.94 58.68 ...
58.89 59.12 59.37 59.65 59.99 60.04 60.85 61.32 61.81 62.28 ...
62.77 63.26 63.7 64.10];
plot(year,uk_pop)
```

```axis([1960 2016 0 65])
```

## add color, style ("help plot")

```plot(year,uk_pop,'r-*')
axis([1990 2016 40 65])  % show part of the axis (range of data)
```

## plot trigonometric functions

```x = 0:0.01:10;        % for sin and cosine
% break the interval into pieces for tan
x1 = 0:0.01:pi/2;
% shift the starting point, because tahn(pi/2) is a large positive number
x2 = pi/2+1e-12:0.01:3*pi/2;
x3 = 3*pi/2+1e-12:0.01:5*pi/2;
plot(x,sin(x),x,cos(x),x1,tan(x1),x2,tan(x2),'r',x3,tan(x3),'r')
axis([0 10 -3 3])   % specify the limits of the axis
```

## bar plot

```x = -3.0:0.2:3.0;
bar(x,exp(-x.^2))
```

## stairs plot

```stairs(x,exp(-x.^2))
```

## Errorbar plot

```errp = rand(size(x))*0.1;
errm = rand(size(x))*0.1;
errorbar(x,exp(-x.^2),errp,errm)
```

## polar coordinate

```t = 0:0.01:2*pi;
rho = abs(sin(2*t).*cos(2*t));
polar(t,rho)
```

## histogram to check uniform random number

```x = rand(1,100000);
histogram(x,100)      % frequency in each bin is almost the same
```

## histogram to check normal random number

```x = randn(1,100000);
histogram(x,100)
```

## check the distribution of points for the logistic map x -> 4*x*(1-x)

```r = 4;
x = rand(1);
Niter = 40000;
X = zeros(1,Niter);
for k=1:Niter
x = r*x*(1-x);
X(k) = x;
end
bin = 100;
hist(X,bin)      % histogram of the distribution
% the frequency is proportional to 1/sqrt(y*(1-y))
```

## 3D graphics

```% generate the meshgrid
x = -2:0.1:2;  % one-d grid in x axis
y = -2:0.1:2;  % one-d grid in y axis
[X,Y]=meshgrid(x,y);  % square grid points

% data for inverted parabola
Z = 1-X.^2-Y.^2;

% surface plot
surf(x,y,Z)
```

## mesh plot

```mesh(x,y,Z)
```

## contour plot

```contour(x,y,Z,[0 0.4 0.5 0.6])  % specify the contour levels
```

## contour plot

```contour(peaks(100),[0 0])  % the zero contour
```

## more interesting peaks

```mesh(peaks(100))
```
```clf  % clear the settings for figure (which may not applicable to this plot)
surf(peaks(100),'EdgeColor','none')
```

## color shading of the surface

```shading interp
```

## add amera light, to make it more realistic

```camlight left
```

## contour plot

you can specify the value of the contours

``` contour(peaks(100),[-5:0.5:5]);
colorbar
```

## contour plot for heart curve

(x^2+y^2-1)^3 = x^2*y^3

```clear all
x = linspace(-1.5,1.5,501);
y = linspace(-1.5,1.5,501);
[X, Y] = meshgrid(x,y);
Z = (X.^2+Y.^2-1).^3 - X.^2.*Y.^3;
contour(x,y,Z,[0 0])
```

## Klein bottle (https://en.wikipedia.org/wiki/Klein_bottle)

``` xpklein     % rorate the bottle
```

## MATLAB logo

``` logo  % the surface of the eigenfunction of a L-shaped domain
```